blob: 51dc603b96a763b17bb8232944668a135f01486d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
Hanno Beckercfe45792019-07-03 16:13:00 +0100115#if defined(MBEDTLS_SSL_RECORD_CHECKING)
116int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
117 unsigned char *buf,
118 size_t buflen )
119{
120 ((void) ssl);
121 ((void) buf);
122 ((void) buflen);
123 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
124}
125#endif /* MBEDTLS_SSL_RECORD_CHECKING */
126
Hanno Becker67bc7c32018-08-06 11:33:50 +0100127#define SSL_DONT_FORCE_FLUSH 0
128#define SSL_FORCE_FLUSH 1
129
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100131
Hanno Beckera0e20d02019-05-15 14:03:01 +0100132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100133/* Top-level Connection ID API */
134
Hanno Becker8367ccc2019-05-14 11:30:10 +0100135int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
136 size_t len,
137 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100138{
139 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
141
Hanno Becker611ac772019-05-14 11:45:26 +0100142 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
143 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
144 {
145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
146 }
147
148 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100149 conf->cid_len = len;
150 return( 0 );
151}
152
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
154 int enable,
155 unsigned char const *own_cid,
156 size_t own_cid_len )
157{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100158 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160
Hanno Beckerca092242019-04-25 16:01:49 +0100161 ssl->negotiate_cid = enable;
162 if( enable == MBEDTLS_SSL_CID_DISABLED )
163 {
164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
165 return( 0 );
166 }
167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100168 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100169
Hanno Beckerad4a1372019-05-03 13:06:44 +0100170 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100171 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
173 (unsigned) own_cid_len,
174 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
176 }
177
178 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100179 /* Truncation is not an issue here because
180 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
181 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100182
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183 return( 0 );
184}
185
186int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
187 int *enabled,
188 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
189 size_t *peer_cid_len )
190{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100191 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
Hanno Becker76a79ab2019-05-03 14:38:32 +0100193 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
194 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
195 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100197 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100198
Hanno Beckerc5f24222019-05-03 12:54:52 +0100199 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
200 * were used, but client and server requested the empty CID.
201 * This is indistinguishable from not using the CID extension
202 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203 if( ssl->transform_in->in_cid_len == 0 &&
204 ssl->transform_in->out_cid_len == 0 )
205 {
206 return( 0 );
207 }
208
Hanno Becker615ef172019-05-22 16:50:35 +0100209 if( peer_cid_len != NULL )
210 {
211 *peer_cid_len = ssl->transform_in->out_cid_len;
212 if( peer_cid != NULL )
213 {
214 memcpy( peer_cid, ssl->transform_in->out_cid,
215 ssl->transform_in->out_cid_len );
216 }
217 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100218
219 *enabled = MBEDTLS_SSL_CID_ENABLED;
220
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100221 return( 0 );
222}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100223#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100224
Hanno Beckerd5847772018-08-28 10:09:23 +0100225/* Forward declarations for functions related to message buffering. */
226static void ssl_buffering_free( mbedtls_ssl_context *ssl );
227static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
228 uint8_t slot );
229static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
230static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
231static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
232static int ssl_buffer_message( mbedtls_ssl_context *ssl );
233static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100234static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100235
Hanno Beckera67dee22018-08-22 10:05:20 +0100236static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100237static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100238{
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100240
241 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100242 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100243
244 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
245}
246
Hanno Becker67bc7c32018-08-06 11:33:50 +0100247static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
248{
Hanno Becker11682cc2018-08-22 14:41:02 +0100249 size_t const bytes_written = ssl->out_left;
250 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251
252 /* Double-check that the write-index hasn't gone
253 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100254 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100255 {
256 /* Should never happen... */
257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
258 }
259
260 return( (int) ( mtu - bytes_written ) );
261}
262
263static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
264{
265 int ret;
266 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400267 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100268
269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
270 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
271
272 if( max_len > mfl )
273 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100274
275 /* By the standard (RFC 6066 Sect. 4), the MFL extension
276 * only limits the maximum record payload size, so in theory
277 * we would be allowed to pack multiple records of payload size
278 * MFL into a single datagram. However, this would mean that there's
279 * no way to explicitly communicate MTU restrictions to the peer.
280 *
281 * The following reduction of max_len makes sure that we never
282 * write datagrams larger than MFL + Record Expansion Overhead.
283 */
284 if( max_len <= ssl->out_left )
285 return( 0 );
286
287 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100288#endif
289
290 ret = ssl_get_remaining_space_in_datagram( ssl );
291 if( ret < 0 )
292 return( ret );
293 remaining = (size_t) ret;
294
295 ret = mbedtls_ssl_get_record_expansion( ssl );
296 if( ret < 0 )
297 return( ret );
298 expansion = (size_t) ret;
299
300 if( remaining <= expansion )
301 return( 0 );
302
303 remaining -= expansion;
304 if( remaining >= max_len )
305 remaining = max_len;
306
307 return( (int) remaining );
308}
309
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200310/*
311 * Double the retransmit timeout value, within the allowed range,
312 * returning -1 if the maximum value has already been reached.
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315{
316 uint32_t new_timeout;
317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200318 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319 return( -1 );
320
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200321 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
322 * in the following way: after the initial transmission and a first
323 * retransmission, back off to a temporary estimated MTU of 508 bytes.
324 * This value is guaranteed to be deliverable (if not guaranteed to be
325 * delivered) of any compliant IPv4 (and IPv6) network, and should work
326 * on most non-IP stacks too. */
327 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400328 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200329 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
331 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200332
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333 new_timeout = 2 * ssl->handshake->retransmit_timeout;
334
335 /* Avoid arithmetic overflow and range overflow */
336 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200337 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200339 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200340 }
341
342 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200344 ssl->handshake->retransmit_timeout ) );
345
346 return( 0 );
347}
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200350{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200351 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200353 ssl->handshake->retransmit_timeout ) );
354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200358/*
359 * Convert max_fragment_length codes to length.
360 * RFC 6066 says:
361 * enum{
362 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
363 * } MaxFragmentLength;
364 * and we add 0 -> extension unused
365 */
Angus Grattond8213d02016-05-25 20:56:48 +1000366static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200367{
Angus Grattond8213d02016-05-25 20:56:48 +1000368 switch( mfl )
369 {
370 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
371 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
372 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
373 return 512;
374 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
375 return 1024;
376 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
377 return 2048;
378 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
379 return 4096;
380 default:
381 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
382 }
383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200385
Hanno Becker52055ae2019-02-06 14:30:46 +0000386int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
387 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ssl_session_free( dst );
390 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000393
394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395 if( src->peer_cert != NULL )
396 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200397 int ret;
398
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200399 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200400 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200401 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200406 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409 dst->peer_cert = NULL;
410 return( ret );
411 }
412 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000413#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000414 if( src->peer_cert_digest != NULL )
415 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000416 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000417 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000418 if( dst->peer_cert_digest == NULL )
419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
420
421 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
422 src->peer_cert_digest_len );
423 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000424 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000425 }
426#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200429
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200430#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200431 if( src->ticket != NULL )
432 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200433 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200434 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200435 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200436
437 memcpy( dst->ticket, src->ticket, src->ticket_len );
438 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200439#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200440
441 return( 0 );
442}
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
445int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200446 const unsigned char *key_enc, const unsigned char *key_dec,
447 size_t keylen,
448 const unsigned char *iv_enc, const unsigned char *iv_dec,
449 size_t ivlen,
450 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200451 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
453int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
454int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
455int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
456int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
457#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459/*
460 * Key material generation
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200463static int ssl3_prf( const unsigned char *secret, size_t slen,
464 const char *label,
465 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000466 unsigned char *dstbuf, size_t dlen )
467{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100468 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000469 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200470 mbedtls_md5_context md5;
471 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000472 unsigned char padding[16];
473 unsigned char sha1sum[20];
474 ((void)label);
475
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_init( &md5 );
477 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200478
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 /*
480 * SSLv3:
481 * block =
482 * MD5( secret + SHA1( 'A' + secret + random ) ) +
483 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
484 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
485 * ...
486 */
487 for( i = 0; i < dlen / 16; i++ )
488 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200489 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000490
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100495 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100497 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100498 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100499 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100500 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000501
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100504 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100505 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100506 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100507 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100508 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100509 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000510 }
511
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100512exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200513 mbedtls_md5_free( &md5 );
514 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500516 mbedtls_platform_zeroize( padding, sizeof( padding ) );
517 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000518
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100519 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200524static int tls1_prf( const unsigned char *secret, size_t slen,
525 const char *label,
526 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000527 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528{
Paul Bakker23986e52011-04-24 08:57:21 +0000529 size_t nb, hs;
530 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200531 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300532 unsigned char *tmp;
533 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 const mbedtls_md_info_t *md_info;
536 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100537 int ret;
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Ron Eldor3b350852019-05-07 18:31:49 +0300541 tmp_len = 20 + strlen( label ) + rlen;
542 tmp = mbedtls_calloc( 1, tmp_len );
543 if( tmp == NULL )
544 {
545 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
546 goto exit;
547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 hs = ( slen + 1 ) / 2;
550 S1 = secret;
551 S2 = secret + slen - hs;
552
553 nb = strlen( label );
554 memcpy( tmp + 20, label, nb );
555 memcpy( tmp + 20 + nb, random, rlen );
556 nb += rlen;
557
558 /*
559 * First compute P_md5(secret,label+random)[0..dlen]
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300562 {
563 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
564 goto exit;
565 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300568 {
569 goto exit;
570 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
573 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
574 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 for( i = 0; i < dlen; i += 16 )
577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_hmac_reset ( &md_ctx );
579 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
580 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
584 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
587
588 for( j = 0; j < k; j++ )
589 dstbuf[i + j] = h_i[j];
590 }
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 /*
595 * XOR out with P_sha1(secret,label+random)[0..dlen]
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300598 {
599 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
600 goto exit;
601 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300604 {
605 goto exit;
606 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
609 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
610 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612 for( i = 0; i < dlen; i += 20 )
613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_md_hmac_reset ( &md_ctx );
615 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
616 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_md_hmac_reset ( &md_ctx );
619 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
620 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
623
624 for( j = 0; j < k; j++ )
625 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
626 }
627
Ron Eldor3b350852019-05-07 18:31:49 +0300628exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100630
Ron Eldor3b350852019-05-07 18:31:49 +0300631 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500632 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Ron Eldor3b350852019-05-07 18:31:49 +0300634 mbedtls_free( tmp );
635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
641static int tls_prf_generic( mbedtls_md_type_t md_type,
642 const unsigned char *secret, size_t slen,
643 const char *label,
644 const unsigned char *random, size_t rlen,
645 unsigned char *dstbuf, size_t dlen )
646{
647 psa_status_t status;
648 psa_algorithm_t alg;
649 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500650 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500651 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
652
Andrzej Kurek2f760752019-01-28 08:08:15 -0500653 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500655
Andrzej Kurekc929a822019-01-14 03:51:11 -0500656 if( md_type == MBEDTLS_MD_SHA384 )
657 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
658 else
659 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
660
Andrzej Kurek2f760752019-01-28 08:08:15 -0500661 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500662 psa_key_policy_set_usage( &policy,
663 PSA_KEY_USAGE_DERIVE,
664 alg );
665 status = psa_set_key_policy( master_slot, &policy );
666 if( status != PSA_SUCCESS )
667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
668
669 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
670 if( status != PSA_SUCCESS )
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672
673 status = psa_key_derivation( &generator,
674 master_slot, alg,
675 random, rlen,
676 (unsigned char const *) label,
677 (size_t) strlen( label ),
678 dlen );
679 if( status != PSA_SUCCESS )
680 {
681 psa_generator_abort( &generator );
682 psa_destroy_key( master_slot );
683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
684 }
685
686 status = psa_generator_read( &generator, dstbuf, dlen );
687 if( status != PSA_SUCCESS )
688 {
689 psa_generator_abort( &generator );
690 psa_destroy_key( master_slot );
691 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
692 }
693
694 status = psa_generator_abort( &generator );
695 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500696 {
697 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500698 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500699 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500700
701 status = psa_destroy_key( master_slot );
702 if( status != PSA_SUCCESS )
703 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
704
Andrzej Kurek33171262019-01-15 03:25:18 -0500705 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500706}
707
708#else /* MBEDTLS_USE_PSA_CRYPTO */
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100711 const unsigned char *secret, size_t slen,
712 const char *label,
713 const unsigned char *random, size_t rlen,
714 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000715{
716 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100717 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300718 unsigned char *tmp;
719 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
721 const mbedtls_md_info_t *md_info;
722 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100723 int ret;
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100731
Ron Eldor3b350852019-05-07 18:31:49 +0300732 tmp_len = md_len + strlen( label ) + rlen;
733 tmp = mbedtls_calloc( 1, tmp_len );
734 if( tmp == NULL )
735 {
736 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
737 goto exit;
738 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739
740 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100741 memcpy( tmp + md_len, label, nb );
742 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743 nb += rlen;
744
745 /*
746 * Compute P_<hash>(secret, label + random)[0..dlen]
747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300749 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
752 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
753 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100754
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100755 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_md_hmac_reset ( &md_ctx );
758 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
759 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_md_hmac_reset ( &md_ctx );
762 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
763 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000766
767 for( j = 0; j < k; j++ )
768 dstbuf[i + j] = h_i[j];
769 }
770
Ron Eldor3b350852019-05-07 18:31:49 +0300771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100773
Ron Eldor3b350852019-05-07 18:31:49 +0300774 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500775 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 mbedtls_free( tmp );
778
779 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500781#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783static int tls_prf_sha256( const unsigned char *secret, size_t slen,
784 const char *label,
785 const unsigned char *random, size_t rlen,
786 unsigned char *dstbuf, size_t dlen )
787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100789 label, random, rlen, dstbuf, dlen ) );
790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200794static int tls_prf_sha384( const unsigned char *secret, size_t slen,
795 const char *label,
796 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000797 unsigned char *dstbuf, size_t dlen )
798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_SHA512_C */
803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
808 defined(MBEDTLS_SSL_PROTO_TLS1_1)
809static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif
Paul Bakker380da532012-04-18 16:10:25 +0000811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_PROTO_SSL3)
813static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
814static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
818static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
819static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
823#if defined(MBEDTLS_SHA256_C)
824static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
825static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
826static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SHA512_C)
830static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
831static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
832static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000835
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100836#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100837 defined(MBEDTLS_USE_PSA_CRYPTO)
838static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
839{
840 if( ssl->conf->f_psk != NULL )
841 {
842 /* If we've used a callback to select the PSK,
843 * the static configuration is irrelevant. */
844 if( ssl->handshake->psk_opaque != 0 )
845 return( 1 );
846
847 return( 0 );
848 }
849
850 if( ssl->conf->psk_opaque != 0 )
851 return( 1 );
852
853 return( 0 );
854}
855#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100856 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100857
Ron Eldorcf280092019-05-14 20:19:13 +0300858#if defined(MBEDTLS_SSL_EXPORT_KEYS)
859static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
860{
861#if defined(MBEDTLS_SSL_PROTO_SSL3)
862 if( tls_prf == ssl3_prf )
863 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300864 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300865 }
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
869 if( tls_prf == tls1_prf )
870 {
871 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
872 }
873 else
874#endif
875#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
876#if defined(MBEDTLS_SHA512_C)
877 if( tls_prf == tls_prf_sha384 )
878 {
879 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
880 }
881 else
882#endif
883#if defined(MBEDTLS_SHA256_C)
884 if( tls_prf == tls_prf_sha256 )
885 {
886 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
887 }
888 else
889#endif
890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
891 return( MBEDTLS_SSL_TLS_PRF_NONE );
892}
893#endif /* MBEDTLS_SSL_EXPORT_KEYS */
894
Ron Eldor51d3ab52019-05-12 14:54:30 +0300895int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
896 const unsigned char *secret, size_t slen,
897 const char *label,
898 const unsigned char *random, size_t rlen,
899 unsigned char *dstbuf, size_t dlen )
900{
901 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
902
903 switch( prf )
904 {
905#if defined(MBEDTLS_SSL_PROTO_SSL3)
906 case MBEDTLS_SSL_TLS_PRF_SSL3:
907 tls_prf = ssl3_prf;
908 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300909#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300910#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
911 case MBEDTLS_SSL_TLS_PRF_TLS1:
912 tls_prf = tls1_prf;
913 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300914#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
915
916#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300917#if defined(MBEDTLS_SHA512_C)
918 case MBEDTLS_SSL_TLS_PRF_SHA384:
919 tls_prf = tls_prf_sha384;
920 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300921#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300922#if defined(MBEDTLS_SHA256_C)
923 case MBEDTLS_SSL_TLS_PRF_SHA256:
924 tls_prf = tls_prf_sha256;
925 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300926#endif /* MBEDTLS_SHA256_C */
927#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300928 default:
929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
930 }
931
932 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
933}
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000936{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200937 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000938#if defined(MBEDTLS_USE_PSA_CRYPTO)
939 int psa_fallthrough;
940#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 unsigned char keyblk[256];
943 unsigned char *key1;
944 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100945 unsigned char *mac_enc;
946 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000947 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200948 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000949 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000950 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 const mbedtls_cipher_info_t *cipher_info;
952 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100953
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000954 /* cf. RFC 5246, Section 8.1:
955 * "The master secret is always exactly 48 bytes in length." */
956 size_t const master_secret_len = 48;
957
Hanno Becker35b23c72018-10-23 12:10:41 +0100958#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
959 unsigned char session_hash[48];
960#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_ssl_session *session = ssl->session_negotiate;
963 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
964 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Jaeden Amero2de07f12019-06-05 13:32:08 +0100968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
969 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000970 transform->encrypt_then_mac = session->encrypt_then_mac;
971#endif
972 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000973
974 ciphersuite_info = handshake->ciphersuite_info;
975 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100976 if( cipher_info == NULL )
977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000979 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100981 }
982
Hanno Beckere694c3e2017-12-27 21:34:08 +0000983 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100984 if( md_info == NULL )
985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000987 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100989 }
990
Hanno Beckera0e20d02019-05-15 14:03:01 +0100991#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100992 /* Copy own and peer's CID if the use of the CID
993 * extension has been negotiated. */
994 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
995 {
996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100997
Hanno Becker05154c32019-05-03 15:23:51 +0100998 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100999 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001000 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001001 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001002
1003 transform->out_cid_len = ssl->handshake->peer_cid_len;
1004 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1005 ssl->handshake->peer_cid_len );
1006 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1007 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001008 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001009#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001010
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_SSL_PROTO_SSL3)
1015 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 {
Paul Bakker48916f92012-09-16 19:57:18 +00001017 handshake->tls_prf = ssl3_prf;
1018 handshake->calc_verify = ssl_calc_verify_ssl;
1019 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021 else
1022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1024 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001025 {
Paul Bakker48916f92012-09-16 19:57:18 +00001026 handshake->tls_prf = tls1_prf;
1027 handshake->calc_verify = ssl_calc_verify_tls;
1028 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001030 else
1031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1033#if defined(MBEDTLS_SHA512_C)
1034 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001035 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001036 {
Paul Bakker48916f92012-09-16 19:57:18 +00001037 handshake->tls_prf = tls_prf_sha384;
1038 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1039 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001040 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001041 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_SHA256_C)
1044 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001045 {
Paul Bakker48916f92012-09-16 19:57:18 +00001046 handshake->tls_prf = tls_prf_sha256;
1047 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1048 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001049 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001050 else
1051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001056 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001057
1058 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 * SSLv3:
1060 * master =
1061 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1062 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1063 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001064 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 * master = PRF( premaster, "master secret", randbytes )[0..47]
1067 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001068 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1071 }
1072 else
1073 {
1074 /* The label for the KDF used for key expansion.
1075 * This is either "master secret" or "extended master secret"
1076 * depending on whether the Extended Master Secret extension
1077 * is used. */
1078 char const *lbl = "master secret";
1079
1080 /* The salt for the KDF used for key expansion.
1081 * - If the Extended Master Secret extension is not used,
1082 * this is ClientHello.Random + ServerHello.Random
1083 * (see Sect. 8.1 in RFC 5246).
1084 * - If the Extended Master Secret extension is used,
1085 * this is the transcript of the handshake so far.
1086 * (see Sect. 4 in RFC 7627). */
1087 unsigned char const *salt = handshake->randbytes;
1088 size_t salt_len = 64;
1089
1090#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094
Hanno Becker35b23c72018-10-23 12:10:41 +01001095 lbl = "extended master secret";
1096 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001097 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1099 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001102 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1103 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001104 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001105 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001106 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001107#endif /* MBEDTLS_SHA512_C */
1108 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001109 }
1110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001112 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001113
Hanno Becker35b23c72018-10-23 12:10:41 +01001114 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001115 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001116#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1117
Hanno Becker7d0a5692018-10-23 15:26:22 +01001118#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1119 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1120 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1121 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1122 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001123 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 /* Perform PSK-to-MS expansion in a single step. */
1125 psa_status_t status;
1126 psa_algorithm_t alg;
1127 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001128 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001129
1130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1131
1132 psk = ssl->conf->psk_opaque;
1133 if( ssl->handshake->psk_opaque != 0 )
1134 psk = ssl->handshake->psk_opaque;
1135
Hanno Becker22bf1452019-04-05 11:21:08 +01001136 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001137 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1138 else
1139 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1140
1141 status = psa_key_derivation( &generator, psk, alg,
1142 salt, salt_len,
1143 (unsigned char const *) lbl,
1144 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001145 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001146 if( status != PSA_SUCCESS )
1147 {
1148 psa_generator_abort( &generator );
1149 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1150 }
1151
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001152 status = psa_generator_read( &generator, session->master,
1153 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 if( status != PSA_SUCCESS )
1155 {
1156 psa_generator_abort( &generator );
1157 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1158 }
1159
1160 status = psa_generator_abort( &generator );
1161 if( status != PSA_SUCCESS )
1162 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001163 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001164 else
1165#endif
1166 {
1167 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1168 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001169 session->master,
1170 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001171 if( ret != 0 )
1172 {
1173 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1174 return( ret );
1175 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001176
Hanno Becker7d0a5692018-10-23 15:26:22 +01001177 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1178 handshake->premaster,
1179 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001180
Hanno Becker7d0a5692018-10-23 15:26:22 +01001181 mbedtls_platform_zeroize( handshake->premaster,
1182 sizeof(handshake->premaster) );
1183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
1186 /*
1187 * Swap the client and server random values.
1188 */
Paul Bakker48916f92012-09-16 19:57:18 +00001189 memcpy( tmp, handshake->randbytes, 64 );
1190 memcpy( handshake->randbytes, tmp + 32, 32 );
1191 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001192 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
1194 /*
1195 * SSLv3:
1196 * key block =
1197 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1198 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1199 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1200 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1201 * ...
1202 *
1203 * TLSv1:
1204 * key block = PRF( master, "key expansion", randbytes )
1205 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001206 ret = handshake->tls_prf( session->master, 48, "key expansion",
1207 handshake->randbytes, 64, keyblk, 256 );
1208 if( ret != 0 )
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001211 return( ret );
1212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1215 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1216 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1217 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1218 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 /*
1221 * Determine the appropriate key, IV and MAC length.
1222 */
Paul Bakker68884e32013-01-07 18:20:04 +01001223
Hanno Becker88aaf652017-12-27 08:17:40 +00001224 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001225
Hanno Becker8031d062018-01-03 15:32:31 +00001226#if defined(MBEDTLS_GCM_C) || \
1227 defined(MBEDTLS_CCM_C) || \
1228 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001230 cipher_info->mode == MBEDTLS_MODE_CCM ||
1231 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001233 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001234
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001236 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001237 transform->taglen =
1238 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001239
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001240 /* All modes haves 96-bit IVs;
1241 * GCM and CCM has 4 implicit and 8 explicit bytes
1242 * ChachaPoly has all 12 bytes implicit
1243 */
Paul Bakker68884e32013-01-07 18:20:04 +01001244 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001245 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1246 transform->fixed_ivlen = 12;
1247 else
1248 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001249
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001250 /* Minimum length of encrypted record */
1251 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001252 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 }
1254 else
Hanno Becker8031d062018-01-03 15:32:31 +00001255#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1256#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1257 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1258 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001259 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001260 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1262 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001265 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001268 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001269 mac_key_len = mbedtls_md_get_size( md_info );
1270 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001273 /*
1274 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1275 * (rfc 6066 page 13 or rfc 2104 section 4),
1276 * so we only need to adjust the length here.
1277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001281
1282#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1283 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001284 * HMAC implementation which also truncates the key
1285 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001286 mac_key_len = transform->maclen;
1287#endif
1288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001290
1291 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001292 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001294 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001296 transform->minlen = transform->maclen;
1297 else
Paul Bakker68884e32013-01-07 18:20:04 +01001298 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299 /*
1300 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001301 * 1. if EtM is in use: one block plus MAC
1302 * otherwise: * first multiple of blocklen greater than maclen
1303 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1306 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001307 {
1308 transform->minlen = transform->maclen
1309 + cipher_info->block_size;
1310 }
1311 else
1312#endif
1313 {
1314 transform->minlen = transform->maclen
1315 + cipher_info->block_size
1316 - transform->maclen % cipher_info->block_size;
1317 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1320 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1321 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001322 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001323 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1327 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001328 {
1329 transform->minlen += transform->ivlen;
1330 }
1331 else
1332#endif
1333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001335 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1336 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001337 }
Paul Bakker68884e32013-01-07 18:20:04 +01001338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Hanno Becker8031d062018-01-03 15:32:31 +00001340 else
1341#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1342 {
1343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Hanno Becker88aaf652017-12-27 08:17:40 +00001347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1348 (unsigned) keylen,
1349 (unsigned) transform->minlen,
1350 (unsigned) transform->ivlen,
1351 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
1353 /*
1354 * Finally setup the cipher contexts, IVs and MAC secrets.
1355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001359 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001360 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Paul Bakker68884e32013-01-07 18:20:04 +01001362 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001363 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001365 /*
1366 * This is not used in TLS v1.1.
1367 */
Paul Bakker48916f92012-09-16 19:57:18 +00001368 iv_copy_len = ( transform->fixed_ivlen ) ?
1369 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001370 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1371 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001372 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 }
1374 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_SSL_CLI_C */
1376#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001379 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Hanno Becker81c7b182017-11-09 18:39:33 +00001382 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001383 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001385 /*
1386 * This is not used in TLS v1.1.
1387 */
Paul Bakker48916f92012-09-16 19:57:18 +00001388 iv_copy_len = ( transform->fixed_ivlen ) ?
1389 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001390 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1391 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001394 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1399 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Hanno Beckerd56ed242018-01-03 15:32:51 +00001402#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_PROTO_SSL3)
1404 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001405 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001406 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001409 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1410 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001411 }
1412
Hanno Becker81c7b182017-11-09 18:39:33 +00001413 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1414 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001415 }
1416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1419 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1420 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001421 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001422 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1423 For AEAD-based ciphersuites, there is nothing to do here. */
1424 if( mac_key_len != 0 )
1425 {
1426 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1427 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1428 }
Paul Bakker68884e32013-01-07 18:20:04 +01001429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001430 else
1431#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001434 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1435 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001436 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001437#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1440 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001441 {
1442 int ret = 0;
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001445
Hanno Becker88aaf652017-12-27 08:17:40 +00001446 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001447 transform->iv_enc, transform->iv_dec,
1448 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001449 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001450 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001453 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1454 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001455 }
1456 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001457#else
1458 ((void) mac_dec);
1459 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001461
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001462#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1463 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001464 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001465 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1466 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001467 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001468 iv_copy_len );
1469 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001470
1471 if( ssl->conf->f_export_keys_ext != NULL )
1472 {
1473 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1474 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001475 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001476 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001477 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001478 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001479 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001480 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001481#endif
1482
Hanno Beckerf704bef2018-11-16 15:21:18 +00001483#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001484
1485 /* Only use PSA-based ciphers for TLS-1.2.
1486 * That's relevant at least for TLS-1.0, where
1487 * we assume that mbedtls_cipher_crypt() updates
1488 * the structure field for the IV, which the PSA-based
1489 * implementation currently doesn't. */
1490#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1491 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001493 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001494 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001495 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1496 {
1497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001498 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 }
1500
1501 if( ret == 0 )
1502 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001504 psa_fallthrough = 0;
1505 }
1506 else
1507 {
1508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1509 psa_fallthrough = 1;
1510 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001511 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001512 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001513 psa_fallthrough = 1;
1514#else
1515 psa_fallthrough = 1;
1516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001517
Hanno Beckercb1cc802018-11-17 22:27:38 +00001518 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001519#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001520 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001521 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001524 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001525 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526
Hanno Beckerf704bef2018-11-16 15:21:18 +00001527#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001528 /* Only use PSA-based ciphers for TLS-1.2.
1529 * That's relevant at least for TLS-1.0, where
1530 * we assume that mbedtls_cipher_crypt() updates
1531 * the structure field for the IV, which the PSA-based
1532 * implementation currently doesn't. */
1533#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1534 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001535 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001536 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001537 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001538 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1539 {
1540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001541 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 }
1543
1544 if( ret == 0 )
1545 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001547 psa_fallthrough = 0;
1548 }
1549 else
1550 {
1551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1552 psa_fallthrough = 1;
1553 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001554 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001555 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001556 psa_fallthrough = 1;
1557#else
1558 psa_fallthrough = 1;
1559#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001560
Hanno Beckercb1cc802018-11-17 22:27:38 +00001561 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001562#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001563 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 cipher_info ) ) != 0 )
1565 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001567 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001571 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001575 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001579 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001583 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_CIPHER_MODE_CBC)
1587 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1590 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001593 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001594 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1597 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001600 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001607 // Initialize compression
1608 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001610 {
Paul Bakker16770332013-10-11 09:59:44 +02001611 if( ssl->compress_buf == NULL )
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001614 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001615 if( ssl->compress_buf == NULL )
1616 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001618 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001619 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1620 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001621 }
1622 }
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625
Paul Bakker48916f92012-09-16 19:57:18 +00001626 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1627 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001628
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001629 if( deflateInit( &transform->ctx_deflate,
1630 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001631 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001634 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1635 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001636 }
1637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001641end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001642 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1643 mbedtls_platform_zeroize( handshake->randbytes,
1644 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001645 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646}
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_SSL_PROTO_SSL3)
1649void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001651 mbedtls_md5_context md5;
1652 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 unsigned char pad_1[48];
1654 unsigned char pad_2[48];
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001658 mbedtls_md5_init( &md5 );
1659 mbedtls_sha1_init( &sha1 );
1660
1661 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1662 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Paul Bakker380da532012-04-18 16:10:25 +00001664 memset( pad_1, 0x36, 48 );
1665 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001667 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1668 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1669 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001671 mbedtls_md5_starts_ret( &md5 );
1672 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1673 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1674 mbedtls_md5_update_ret( &md5, hash, 16 );
1675 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001677 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1678 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1679 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001681 mbedtls_sha1_starts_ret( &sha1 );
1682 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1683 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1684 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1685 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001690 mbedtls_md5_free( &md5 );
1691 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001692
Paul Bakker380da532012-04-18 16:10:25 +00001693 return;
1694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1698void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001699{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001700 mbedtls_md5_context md5;
1701 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001704
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001705 mbedtls_md5_init( &md5 );
1706 mbedtls_sha1_init( &sha1 );
1707
1708 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1709 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001710
Andrzej Kurekeb342242019-01-29 09:14:33 -05001711 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001712 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001716
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001717 mbedtls_md5_free( &md5 );
1718 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001719
Paul Bakker380da532012-04-18 16:10:25 +00001720 return;
1721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1725#if defined(MBEDTLS_SHA256_C)
1726void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001727{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001728#if defined(MBEDTLS_USE_PSA_CRYPTO)
1729 size_t hash_size;
1730 psa_status_t status;
1731 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1732
1733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1734 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1735 if( status != PSA_SUCCESS )
1736 {
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1738 return;
1739 }
1740
1741 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1742 if( status != PSA_SUCCESS )
1743 {
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1745 return;
1746 }
1747 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1749#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001750 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001751
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001752 mbedtls_sha256_init( &sha256 );
1753
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001755
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001756 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001757 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001761
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001762 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001764 return;
1765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_SHA512_C)
1769void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001770{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001771#if defined(MBEDTLS_USE_PSA_CRYPTO)
1772 size_t hash_size;
1773 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001774 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001775
1776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001777 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001778 if( status != PSA_SUCCESS )
1779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1781 return;
1782 }
1783
Andrzej Kurek972fba52019-01-30 03:29:12 -05001784 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001785 if( status != PSA_SUCCESS )
1786 {
1787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1788 return;
1789 }
1790 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1792#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001793 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001794
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001795 mbedtls_sha512_init( &sha512 );
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001798
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001799 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001800 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001805 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001806#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 return;
1808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#endif /* MBEDTLS_SHA512_C */
1810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1813int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001814{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001815 unsigned char *p = ssl->handshake->premaster;
1816 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001817 const unsigned char *psk = ssl->conf->psk;
1818 size_t psk_len = ssl->conf->psk_len;
1819
1820 /* If the psk callback was called, use its result */
1821 if( ssl->handshake->psk != NULL )
1822 {
1823 psk = ssl->handshake->psk;
1824 psk_len = ssl->handshake->psk_len;
1825 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001826
1827 /*
1828 * PMS = struct {
1829 * opaque other_secret<0..2^16-1>;
1830 * opaque psk<0..2^16-1>;
1831 * };
1832 * with "other_secret" depending on the particular key exchange
1833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1835 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001836 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001837 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001839
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001840 *(p++) = (unsigned char)( psk_len >> 8 );
1841 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001842
1843 if( end < p || (size_t)( end - p ) < psk_len )
1844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1845
1846 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001847 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001848 }
1849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1851#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1852 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001853 {
1854 /*
1855 * other_secret already set by the ClientKeyExchange message,
1856 * and is 48 bytes long
1857 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001858 if( end - p < 2 )
1859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1860
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001861 *p++ = 0;
1862 *p++ = 48;
1863 p += 48;
1864 }
1865 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1867#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1868 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001870 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001871 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001873 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001875 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001876 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 return( ret );
1880 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001881 *(p++) = (unsigned char)( len >> 8 );
1882 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001883 p += len;
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 }
1887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1889#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1890 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001891 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001892 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 size_t zlen;
1894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001896 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001897 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001900 return( ret );
1901 }
1902
1903 *(p++) = (unsigned char)( zlen >> 8 );
1904 *(p++) = (unsigned char)( zlen );
1905 p += zlen;
1906
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001907 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1908 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001909 }
1910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001915 }
1916
1917 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001918 if( end - p < 2 )
1919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001920
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001921 *(p++) = (unsigned char)( psk_len >> 8 );
1922 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001923
1924 if( end < p || (size_t)( end - p ) < psk_len )
1925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1926
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001927 memcpy( p, psk, psk_len );
1928 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001929
1930 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1931
1932 return( 0 );
1933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001937/*
1938 * SSLv3.0 MAC functions
1939 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001940#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001941static void ssl_mac( mbedtls_md_context_t *md_ctx,
1942 const unsigned char *secret,
1943 const unsigned char *buf, size_t len,
1944 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001945 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946{
1947 unsigned char header[11];
1948 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001949 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1951 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001952
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001953 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001955 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001956 else
Paul Bakker68884e32013-01-07 18:20:04 +01001957 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
1959 memcpy( header, ctr, 8 );
1960 header[ 8] = (unsigned char) type;
1961 header[ 9] = (unsigned char)( len >> 8 );
1962 header[10] = (unsigned char)( len );
1963
Paul Bakker68884e32013-01-07 18:20:04 +01001964 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 mbedtls_md_starts( md_ctx );
1966 mbedtls_md_update( md_ctx, secret, md_size );
1967 mbedtls_md_update( md_ctx, padding, padlen );
1968 mbedtls_md_update( md_ctx, header, 11 );
1969 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001970 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Paul Bakker68884e32013-01-07 18:20:04 +01001972 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_md_starts( md_ctx );
1974 mbedtls_md_update( md_ctx, secret, md_size );
1975 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001976 mbedtls_md_update( md_ctx, out, md_size );
1977 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001980
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001981/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001982 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001983#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001984 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1985 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1986 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1987/* This function makes sure every byte in the memory region is accessed
1988 * (in ascending addresses order) */
1989static void ssl_read_memory( unsigned char *p, size_t len )
1990{
1991 unsigned char acc = 0;
1992 volatile unsigned char force;
1993
1994 for( ; len != 0; p++, len-- )
1995 acc ^= *p;
1996
1997 force = acc;
1998 (void) force;
1999}
2000#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2001
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002002/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002004 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002005
Hanno Beckera0e20d02019-05-15 14:03:01 +01002006#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002007/* This functions transforms a DTLS plaintext fragment and a record content
2008 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002009 *
2010 * struct {
2011 * opaque content[DTLSPlaintext.length];
2012 * ContentType real_type;
2013 * uint8 zeros[length_of_padding];
2014 * } DTLSInnerPlaintext;
2015 *
2016 * Input:
2017 * - `content`: The beginning of the buffer holding the
2018 * plaintext to be wrapped.
2019 * - `*content_size`: The length of the plaintext in Bytes.
2020 * - `max_len`: The number of Bytes available starting from
2021 * `content`. This must be `>= *content_size`.
2022 * - `rec_type`: The desired record content type.
2023 *
2024 * Output:
2025 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2026 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2027 *
2028 * Returns:
2029 * - `0` on success.
2030 * - A negative error code if `max_len` didn't offer enough space
2031 * for the expansion.
2032 */
2033static int ssl_cid_build_inner_plaintext( unsigned char *content,
2034 size_t *content_size,
2035 size_t remaining,
2036 uint8_t rec_type )
2037{
2038 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002039 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2040 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2041 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002042
2043 /* Write real content type */
2044 if( remaining == 0 )
2045 return( -1 );
2046 content[ len ] = rec_type;
2047 len++;
2048 remaining--;
2049
2050 if( remaining < pad )
2051 return( -1 );
2052 memset( content + len, 0, pad );
2053 len += pad;
2054 remaining -= pad;
2055
2056 *content_size = len;
2057 return( 0 );
2058}
2059
Hanno Becker07dc97d2019-05-20 15:08:01 +01002060/* This function parses a DTLSInnerPlaintext structure.
2061 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002062static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2063 size_t *content_size,
2064 uint8_t *rec_type )
2065{
2066 size_t remaining = *content_size;
2067
2068 /* Determine length of padding by skipping zeroes from the back. */
2069 do
2070 {
2071 if( remaining == 0 )
2072 return( -1 );
2073 remaining--;
2074 } while( content[ remaining ] == 0 );
2075
2076 *content_size = remaining;
2077 *rec_type = content[ remaining ];
2078
2079 return( 0 );
2080}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002081#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002082
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002083/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002084 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002086 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087 mbedtls_record *rec )
2088{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002089 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002090 *
2091 * additional_data = seq_num + TLSCompressed.type +
2092 * TLSCompressed.version + TLSCompressed.length;
2093 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002094 * For the CID extension, this is extended as follows
2095 * (quoting draft-ietf-tls-dtls-connection-id-05,
2096 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002097 *
2098 * additional_data = seq_num + DTLSPlaintext.type +
2099 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002100 * cid +
2101 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002102 * length_of_DTLSInnerPlaintext;
2103 */
2104
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2106 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002107 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002108
Hanno Beckera0e20d02019-05-15 14:03:01 +01002109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002110 if( rec->cid_len != 0 )
2111 {
2112 memcpy( add_data + 11, rec->cid, rec->cid_len );
2113 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2114 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2115 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2116 *add_data_len = 13 + 1 + rec->cid_len;
2117 }
2118 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002119#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002120 {
2121 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2122 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2123 *add_data_len = 13;
2124 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125}
2126
Hanno Beckera18d1322018-01-03 14:27:32 +00002127int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2128 mbedtls_ssl_transform *transform,
2129 mbedtls_record *rec,
2130 int (*f_rng)(void *, unsigned char *, size_t),
2131 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002134 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002136 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002137 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 size_t post_avail;
2139
2140 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002141#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002142 ((void) ssl);
2143#endif
2144
2145 /* The PRNG is used for dynamic IV generation that's used
2146 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2147#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2148 ( defined(MBEDTLS_AES_C) || \
2149 defined(MBEDTLS_ARIA_C) || \
2150 defined(MBEDTLS_CAMELLIA_C) ) && \
2151 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2152 ((void) f_rng);
2153 ((void) p_rng);
2154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002159 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002163 if( rec == NULL
2164 || rec->buf == NULL
2165 || rec->buf_len < rec->data_offset
2166 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002167#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002168 || rec->cid_len != 0
2169#endif
2170 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002174 }
2175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002176 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002177 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179 data, rec->data_len );
2180
2181 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2182
2183 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2184 {
2185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2186 (unsigned) rec->data_len,
2187 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2189 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002190
Hanno Beckera0e20d02019-05-15 14:03:01 +01002191#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002192 /*
2193 * Add CID information
2194 */
2195 rec->cid_len = transform->out_cid_len;
2196 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2197 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002198
2199 if( rec->cid_len != 0 )
2200 {
2201 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002202 * Wrap plaintext into DTLSInnerPlaintext structure.
2203 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002204 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002205 * Note that this changes `rec->data_len`, and hence
2206 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002207 */
2208 if( ssl_cid_build_inner_plaintext( data,
2209 &rec->data_len,
2210 post_avail,
2211 rec->type ) != 0 )
2212 {
2213 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2214 }
2215
2216 rec->type = MBEDTLS_SSL_MSG_CID;
2217 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002218#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002219
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002220 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2221
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002223 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 */
Hanno Becker52344c22018-01-03 15:24:20 +00002225#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( mode == MBEDTLS_MODE_STREAM ||
2227 ( mode == MBEDTLS_MODE_CBC
2228#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002230#endif
2231 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 if( post_avail < transform->maclen )
2234 {
2235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2236 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2237 }
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002242 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2244 data, rec->data_len, rec->ctr, rec->type, mac );
2245 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 }
2247 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002251 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 {
Hanno Becker992b6872017-11-09 18:57:39 +00002253 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2254
Hanno Beckercab87e62019-04-29 13:52:53 +01002255 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002256
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002258 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2260 data, rec->data_len );
2261 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2262 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2263
2264 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002265 }
2266 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002267#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002271 }
2272
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2274 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002275
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 rec->data_len += transform->maclen;
2277 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002278 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002279 }
Hanno Becker52344c22018-01-03 15:24:20 +00002280#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002282 /*
2283 * Encrypt
2284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2286 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002288 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 "including %d bytes of padding",
2292 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2295 transform->iv_enc, transform->ivlen,
2296 data, rec->data_len,
2297 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002300 return( ret );
2301 }
2302
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
Paul Bakker68884e32013-01-07 18:20:04 +01002309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002311
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002312#if defined(MBEDTLS_GCM_C) || \
2313 defined(MBEDTLS_CCM_C) || \
2314 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002316 mode == MBEDTLS_MODE_CCM ||
2317 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002318 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002319 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002321 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002322
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 /* Check that there's space for both the authentication tag
2324 * and the explicit IV before and after the record content. */
2325 if( post_avail < transform->taglen ||
2326 rec->data_offset < explicit_iv_len )
2327 {
2328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2329 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2330 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002331
Paul Bakker68884e32013-01-07 18:20:04 +01002332 /*
2333 * Generate IV
2334 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2336 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002337 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2340 explicit_iv_len );
2341 /* Prefix record content with explicit IV. */
2342 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002343 }
2344 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2345 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002346 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 unsigned char i;
2348
2349 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2350
2351 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 }
2354 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002355 {
2356 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002359 }
2360
Hanno Beckercab87e62019-04-29 13:52:53 +01002361 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002362
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2364 iv, transform->ivlen );
2365 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 data - explicit_iv_len, explicit_iv_len );
2367 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002368 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002372
Paul Bakker68884e32013-01-07 18:20:04 +01002373 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002374 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002375 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002377 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002379 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 data, rec->data_len, /* source */
2381 data, &rec->data_len, /* destination */
2382 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002385 return( ret );
2386 }
2387
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2389 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002390
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 rec->data_len += transform->taglen + explicit_iv_len;
2392 rec->data_offset -= explicit_iv_len;
2393 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002394 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2398#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002399 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002402 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002403 size_t padlen, i;
2404 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002405
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 /* Currently we're always using minimal padding
2407 * (up to 255 bytes would be allowed). */
2408 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2409 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 padlen = 0;
2411
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002412 /* Check there's enough space in the buffer for the padding. */
2413 if( post_avail < padlen + 1 )
2414 {
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2416 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2417 }
2418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002422 rec->data_len += padlen + 1;
2423 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002427 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2428 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 if( f_rng == NULL )
2433 {
2434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2436 }
2437
2438 if( rec->data_offset < transform->ivlen )
2439 {
2440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2441 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2442 }
2443
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444 /*
2445 * Generate IV
2446 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002448 if( ret != 0 )
2449 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 memcpy( data - transform->ivlen, transform->iv_enc,
2452 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002458 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002460 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2463 transform->iv_enc,
2464 transform->ivlen,
2465 data, rec->data_len,
2466 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002469 return( ret );
2470 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002471
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002472 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002476 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002480 {
2481 /*
2482 * Save IV in SSL3 and TLS1
2483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2485 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002488#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 {
2490 data -= transform->ivlen;
2491 rec->data_offset -= transform->ivlen;
2492 rec->data_len += transform->ivlen;
2493 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002498 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2499
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 /*
2501 * MAC(MAC_write_key, seq_num +
2502 * TLSCipherText.type +
2503 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002504 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 * IV + // except for TLS 1.0
2506 * ENC(content + padding + padding_length));
2507 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508
2509 if( post_avail < transform->maclen)
2510 {
2511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2513 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Beckercab87e62019-04-29 13:52:53 +01002515 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002518 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002519 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002520
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002522 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002523 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2524 data, rec->data_len );
2525 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2526 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002527
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002530 rec->data_len += transform->maclen;
2531 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002532 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002538 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 /* Make extra sure authentication was performed, exactly once */
2545 if( auth_done != 1 )
2546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002549 }
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 return( 0 );
2554}
2555
Hanno Beckera18d1322018-01-03 14:27:32 +00002556int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2557 mbedtls_ssl_transform *transform,
2558 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002560 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002563#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002564 size_t padlen = 0, correct = 1;
2565#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002566 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002567 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002568 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002569
Hanno Beckera18d1322018-01-03 14:27:32 +00002570#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 ((void) ssl);
2572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002575 if( rec == NULL ||
2576 rec->buf == NULL ||
2577 rec->buf_len < rec->data_offset ||
2578 rec->buf_len - rec->data_offset < rec->data_len )
2579 {
2580 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002581 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002582 }
2583
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002584 data = rec->buf + rec->data_offset;
2585 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002586
Hanno Beckera0e20d02019-05-15 14:03:01 +01002587#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002588 /*
2589 * Match record's CID with incoming CID.
2590 */
Hanno Becker938489a2019-05-08 13:02:22 +01002591 if( rec->cid_len != transform->in_cid_len ||
2592 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2593 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002594 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002595 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002596#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002598#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2599 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002600 {
2601 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002602 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2603 transform->iv_dec,
2604 transform->ivlen,
2605 data, rec->data_len,
2606 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002607 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002609 return( ret );
2610 }
2611
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002612 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2615 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002616 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002617 }
Paul Bakker68884e32013-01-07 18:20:04 +01002618 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620#if defined(MBEDTLS_GCM_C) || \
2621 defined(MBEDTLS_CCM_C) || \
2622 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002624 mode == MBEDTLS_MODE_CCM ||
2625 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002626 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002628 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002629
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002630 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002632 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 "+ taglen (%d)", rec->data_len,
2634 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002635 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002636 }
Paul Bakker68884e32013-01-07 18:20:04 +01002637
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 /*
2639 * Prepare IV
2640 */
2641 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2642 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002643 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002644 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002645 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002646
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002647 }
2648 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2649 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002650 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002651 unsigned char i;
2652
2653 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2654
2655 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002656 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002657 }
2658 else
2659 {
2660 /* Reminder if we ever add an AEAD mode with a different size */
2661 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2662 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2663 }
2664
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 data += explicit_iv_len;
2666 rec->data_offset += explicit_iv_len;
2667 rec->data_len -= explicit_iv_len + transform->taglen;
2668
Hanno Beckercab87e62019-04-29 13:52:53 +01002669 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002670 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002671 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672
2673 memcpy( transform->iv_dec + transform->fixed_ivlen,
2674 data - explicit_iv_len, explicit_iv_len );
2675
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002676 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002677 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002678 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002679
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002681 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002682 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002683 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002684 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2685 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002686 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002687 data, rec->data_len,
2688 data, &olen,
2689 data + rec->data_len,
2690 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002694 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2695 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002696
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002697 return( ret );
2698 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002699 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002700
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002701 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002702 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2704 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002705 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002706 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002708#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2709#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002710 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002713 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002714
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 /*
Paul Bakker45829992013-01-03 14:52:21 +01002716 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002718#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002719 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2720 {
2721 /* The ciphertext is prefixed with the CBC IV. */
2722 minlen += transform->ivlen;
2723 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002724#endif
Paul Bakker45829992013-01-03 14:52:21 +01002725
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002726 /* Size considerations:
2727 *
2728 * - The CBC cipher text must not be empty and hence
2729 * at least of size transform->ivlen.
2730 *
2731 * Together with the potential IV-prefix, this explains
2732 * the first of the two checks below.
2733 *
2734 * - The record must contain a MAC, either in plain or
2735 * encrypted, depending on whether Encrypt-then-MAC
2736 * is used or not.
2737 * - If it is, the message contains the IV-prefix,
2738 * the CBC ciphertext, and the MAC.
2739 * - If it is not, the padded plaintext, and hence
2740 * the CBC ciphertext, has at least length maclen + 1
2741 * because there is at least the padding length byte.
2742 *
2743 * As the CBC ciphertext is not empty, both cases give the
2744 * lower bound minlen + maclen + 1 on the record size, which
2745 * we test for in the second check below.
2746 */
2747 if( rec->data_len < minlen + transform->ivlen ||
2748 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002751 "+ 1 ) ( + expl IV )", rec->data_len,
2752 transform->ivlen,
2753 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002755 }
2756
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002757 /*
2758 * Authenticate before decrypt if enabled
2759 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002762 {
Hanno Becker992b6872017-11-09 18:57:39 +00002763 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002765 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002766
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002767 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2768 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002769
Hanno Beckercab87e62019-04-29 13:52:53 +01002770 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002771
Hanno Beckercab87e62019-04-29 13:52:53 +01002772 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2773 add_data_len );
2774 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2775 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2777 data, rec->data_len );
2778 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2779 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002780
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002781 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2782 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002783 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002786 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2787 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002791 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002792 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002794#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002795
2796 /*
2797 * Check length sanity
2798 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002799 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002800 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002803 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002804 }
2805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002806#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002807 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002808 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002809 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002812 /* This is safe because data_len >= minlen + maclen + 1 initially,
2813 * and at this point we have at most subtracted maclen (note that
2814 * minlen == transform->ivlen here). */
2815 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002816
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002817 data += transform->ivlen;
2818 rec->data_offset += transform->ivlen;
2819 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002820 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002822
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002823 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2824 transform->iv_dec, transform->ivlen,
2825 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002828 return( ret );
2829 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002830
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002832 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2834 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002835 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002837#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002839 {
2840 /*
2841 * Save IV in SSL3 and TLS1
2842 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2844 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002845 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002847
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 /* Safe since data_len >= minlen + maclen + 1, so after having
2849 * subtracted at most minlen and maclen up to this point,
2850 * data_len > 0. */
2851 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002852
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853 if( auth_done == 1 )
2854 {
2855 correct *= ( rec->data_len >= padlen + 1 );
2856 padlen *= ( rec->data_len >= padlen + 1 );
2857 }
2858 else
Paul Bakker45829992013-01-03 14:52:21 +01002859 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002860#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002861 if( rec->data_len < transform->maclen + padlen + 1 )
2862 {
2863 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2864 rec->data_len,
2865 transform->maclen,
2866 padlen + 1 ) );
2867 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002868#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869
2870 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2871 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002872 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002873
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 padlen++;
2875
2876 /* Regardless of the validity of the padding,
2877 * we have data_len >= padlen here. */
2878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002881 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002883 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002884#if defined(MBEDTLS_SSL_DEBUG_ALL)
2885 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002886 "should be no more than %d",
2887 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002888#endif
Paul Bakker45829992013-01-03 14:52:21 +01002889 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002890 }
2891 }
2892 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002893#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2894#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2895 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002896 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002897 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002898 /* The padding check involves a series of up to 256
2899 * consecutive memory reads at the end of the record
2900 * plaintext buffer. In order to hide the length and
2901 * validity of the padding, always perform exactly
2902 * `min(256,plaintext_len)` reads (but take into account
2903 * only the last `padlen` bytes for the padding check). */
2904 size_t pad_count = 0;
2905 size_t real_count = 0;
2906 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002907
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002908 /* Index of first padding byte; it has been ensured above
2909 * that the subtraction is safe. */
2910 size_t const padding_idx = rec->data_len - padlen;
2911 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2912 size_t const start_idx = rec->data_len - num_checks;
2913 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002914
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002916 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002917 real_count |= ( idx >= padding_idx );
2918 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002919 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002920 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002922#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002923 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002925#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002926 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002927 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002928 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002929#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2930 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002934 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002935
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002936 /* If the padding was found to be invalid, padlen == 0
2937 * and the subtraction is safe. If the padding was found valid,
2938 * padlen hasn't been changed and the previous assertion
2939 * data_len >= padlen still holds. */
2940 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002941 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002942 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002944 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2947 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002948 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002950#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002952 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002953#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002954
2955 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002956 * Authenticate if not done yet.
2957 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002958 */
Hanno Becker52344c22018-01-03 15:24:20 +00002959#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002960 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002961 {
Hanno Becker992b6872017-11-09 18:57:39 +00002962 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002963
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002964 /* If the initial value of padlen was such that
2965 * data_len < maclen + padlen + 1, then padlen
2966 * got reset to 1, and the initial check
2967 * data_len >= minlen + maclen + 1
2968 * guarantees that at this point we still
2969 * have at least data_len >= maclen.
2970 *
2971 * If the initial value of padlen was such that
2972 * data_len >= maclen + padlen + 1, then we have
2973 * subtracted either padlen + 1 (if the padding was correct)
2974 * or 0 (if the padding was incorrect) since then,
2975 * hence data_len >= maclen in any case.
2976 */
2977 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002978
Hanno Beckercab87e62019-04-29 13:52:53 +01002979 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002981#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002982 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002983 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002984 ssl_mac( &transform->md_ctx_dec,
2985 transform->mac_dec,
2986 data, rec->data_len,
2987 rec->ctr, rec->type,
2988 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002989 }
2990 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002991#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2992#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2993 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002994 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002995 {
2996 /*
2997 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002998 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002999 *
3000 * Known timing attacks:
3001 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3002 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003003 * To compensate for different timings for the MAC calculation
3004 * depending on how much padding was removed (which is determined
3005 * by padlen), process extra_run more blocks through the hash
3006 * function.
3007 *
3008 * The formula in the paper is
3009 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3010 * where L1 is the size of the header plus the decrypted message
3011 * plus CBC padding and L2 is the size of the header plus the
3012 * decrypted message. This is for an underlying hash function
3013 * with 64-byte blocks.
3014 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3015 * correctly. We round down instead of up, so -56 is the correct
3016 * value for our calculations instead of -55.
3017 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003018 * Repeat the formula rather than defining a block_size variable.
3019 * This avoids requiring division by a variable at runtime
3020 * (which would be marginally less efficient and would require
3021 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003022 */
3023 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003024 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003025
3026 /*
3027 * The next two sizes are the minimum and maximum values of
3028 * in_msglen over all padlen values.
3029 *
3030 * They're independent of padlen, since we previously did
3031 * in_msglen -= padlen.
3032 *
3033 * Note that max_len + maclen is never more than the buffer
3034 * length, as we previously did in_msglen -= maclen too.
3035 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003036 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003037 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3038
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003039 memset( tmp, 0, sizeof( tmp ) );
3040
3041 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003043#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3044 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003045 case MBEDTLS_MD_MD5:
3046 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003049 extra_run =
3050 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3051 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003052 break;
3053#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003054#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003055 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003056 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003057 extra_run =
3058 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3059 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003060 break;
3061#endif
3062 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003064 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3065 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003066
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003067 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003068
Hanno Beckercab87e62019-04-29 13:52:53 +01003069 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3070 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3072 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003073 /* Make sure we access everything even when padlen > 0. This
3074 * makes the synchronisation requirements for just-in-time
3075 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 ssl_read_memory( data + rec->data_len, padlen );
3077 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003078
3079 /* Call mbedtls_md_process at least once due to cache attacks
3080 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003081 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003083
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003085
3086 /* Make sure we access all the memory that could contain the MAC,
3087 * before we check it in the next code block. This makes the
3088 * synchronisation requirements for just-in-time Prime+Probe
3089 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 ssl_read_memory( data + min_len,
3091 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003092 }
3093 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3095 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3098 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003099 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003101#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003102 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3103 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003104#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003105
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003106 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3107 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003108 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_DEBUG_ALL)
3110 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003111#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003112 correct = 0;
3113 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003114 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003115 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003116
3117 /*
3118 * Finally check the correct flag
3119 */
3120 if( correct == 0 )
3121 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003122#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003123
3124 /* Make extra sure authentication was performed, exactly once */
3125 if( auth_done != 1 )
3126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3128 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003129 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003130
Hanno Beckera0e20d02019-05-15 14:03:01 +01003131#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003132 if( rec->cid_len != 0 )
3133 {
3134 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3135 &rec->type );
3136 if( ret != 0 )
3137 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3138 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003139#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003142
3143 return( 0 );
3144}
3145
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003146#undef MAC_NONE
3147#undef MAC_PLAINTEXT
3148#undef MAC_CIPHERTEXT
3149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003151/*
3152 * Compression/decompression functions
3153 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155{
3156 int ret;
3157 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003158 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003160 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003164 if( len_pre == 0 )
3165 return( 0 );
3166
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 memcpy( msg_pre, ssl->out_msg, len_pre );
3168
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003169 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003170 ssl->out_msglen ) );
3171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173 ssl->out_msg, ssl->out_msglen );
3174
Paul Bakker48916f92012-09-16 19:57:18 +00003175 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3176 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3177 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003178 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179
Paul Bakker48916f92012-09-16 19:57:18 +00003180 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181 if( ret != Z_OK )
3182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3184 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185 }
3186
Angus Grattond8213d02016-05-25 20:56:48 +10003187 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003188 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191 ssl->out_msglen ) );
3192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194 ssl->out_msg, ssl->out_msglen );
3195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197
3198 return( 0 );
3199}
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202{
3203 int ret;
3204 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003205 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003206 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003207 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003211 if( len_pre == 0 )
3212 return( 0 );
3213
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214 memcpy( msg_pre, ssl->in_msg, len_pre );
3215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003217 ssl->in_msglen ) );
3218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003219 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003220 ssl->in_msg, ssl->in_msglen );
3221
Paul Bakker48916f92012-09-16 19:57:18 +00003222 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3223 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3224 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003225 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003226 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227
Paul Bakker48916f92012-09-16 19:57:18 +00003228 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003229 if( ret != Z_OK )
3230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3232 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233 }
3234
Angus Grattond8213d02016-05-25 20:56:48 +10003235 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003236 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003239 ssl->in_msglen ) );
3240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003241 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003242 ssl->in_msg, ssl->in_msglen );
3243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003245
3246 return( 0 );
3247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003250#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3251static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003253#if defined(MBEDTLS_SSL_PROTO_DTLS)
3254static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003255{
3256 /* If renegotiation is not enforced, retransmit until we would reach max
3257 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003258 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003259 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003260 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003261 unsigned char doublings = 1;
3262
3263 while( ratio != 0 )
3264 {
3265 ++doublings;
3266 ratio >>= 1;
3267 }
3268
3269 if( ++ssl->renego_records_seen > doublings )
3270 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003272 return( 0 );
3273 }
3274 }
3275
3276 return( ssl_write_hello_request( ssl ) );
3277}
3278#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003279#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003280
Paul Bakker5121ce52009-01-03 21:22:43 +00003281/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003282 * Fill the input message buffer by appending data to it.
3283 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003284 *
3285 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3286 * available (from this read and/or a previous one). Otherwise, an error code
3287 * is returned (possibly EOF or WANT_READ).
3288 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003289 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3290 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3291 * since we always read a whole datagram at once.
3292 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003293 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003294 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003297{
Paul Bakker23986e52011-04-24 08:57:21 +00003298 int ret;
3299 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003302
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003303 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003306 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003308 }
3309
Angus Grattond8213d02016-05-25 20:56:48 +10003310 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3313 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003314 }
3315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003316#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003317 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003318 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003319 uint32_t timeout;
3320
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003321 /* Just to be sure */
3322 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3323 {
3324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3325 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3326 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3327 }
3328
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003329 /*
3330 * The point is, we need to always read a full datagram at once, so we
3331 * sometimes read more then requested, and handle the additional data.
3332 * It could be the rest of the current record (while fetching the
3333 * header) and/or some other records in the same datagram.
3334 */
3335
3336 /*
3337 * Move to the next record in the already read datagram if applicable
3338 */
3339 if( ssl->next_record_offset != 0 )
3340 {
3341 if( ssl->in_left < ssl->next_record_offset )
3342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003345 }
3346
3347 ssl->in_left -= ssl->next_record_offset;
3348
3349 if( ssl->in_left != 0 )
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003352 ssl->next_record_offset ) );
3353 memmove( ssl->in_hdr,
3354 ssl->in_hdr + ssl->next_record_offset,
3355 ssl->in_left );
3356 }
3357
3358 ssl->next_record_offset = 0;
3359 }
3360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003362 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003363
3364 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003365 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003366 */
3367 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003370 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003371 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003372
3373 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003374 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003375 * are not at the beginning of a new record, the caller did something
3376 * wrong.
3377 */
3378 if( ssl->in_left != 0 )
3379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3381 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003382 }
3383
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003384 /*
3385 * Don't even try to read if time's out already.
3386 * This avoids by-passing the timer when repeatedly receiving messages
3387 * that will end up being dropped.
3388 */
3389 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003390 {
3391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003392 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003393 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003394 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003395 {
Angus Grattond8213d02016-05-25 20:56:48 +10003396 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399 timeout = ssl->handshake->retransmit_timeout;
3400 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003401 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003404
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003405 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003406 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3407 timeout );
3408 else
3409 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003412
3413 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003415 }
3416
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003417 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003420 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003423 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003424 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003427 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003428 }
3429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003431 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003433 return( ret );
3434 }
3435
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003436 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003437 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003439 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003441 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003442 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003445 return( ret );
3446 }
3447
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003448 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003449 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003451 }
3452
Paul Bakker5121ce52009-01-03 21:22:43 +00003453 if( ret < 0 )
3454 return( ret );
3455
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003456 ssl->in_left = ret;
3457 }
3458 else
3459#endif
3460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003462 ssl->in_left, nb_want ) );
3463
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003464 while( ssl->in_left < nb_want )
3465 {
3466 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003467
3468 if( ssl_check_timer( ssl ) != 0 )
3469 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3470 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003471 {
3472 if( ssl->f_recv_timeout != NULL )
3473 {
3474 ret = ssl->f_recv_timeout( ssl->p_bio,
3475 ssl->in_hdr + ssl->in_left, len,
3476 ssl->conf->read_timeout );
3477 }
3478 else
3479 {
3480 ret = ssl->f_recv( ssl->p_bio,
3481 ssl->in_hdr + ssl->in_left, len );
3482 }
3483 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003486 ssl->in_left, nb_want ) );
3487 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003488
3489 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003490 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003491
3492 if( ret < 0 )
3493 return( ret );
3494
mohammad160352aecb92018-03-28 23:41:40 -07003495 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003496 {
Darryl Green11999bb2018-03-13 15:22:58 +00003497 MBEDTLS_SSL_DEBUG_MSG( 1,
3498 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003499 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3501 }
3502
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003503 ssl->in_left += ret;
3504 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003505 }
3506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
3509 return( 0 );
3510}
3511
3512/*
3513 * Flush any data not yet written
3514 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003516{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003517 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003518 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003521
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003522 if( ssl->f_send == NULL )
3523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003525 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003527 }
3528
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003529 /* Avoid incrementing counter if data is flushed */
3530 if( ssl->out_left == 0 )
3531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003533 return( 0 );
3534 }
3535
Paul Bakker5121ce52009-01-03 21:22:43 +00003536 while( ssl->out_left > 0 )
3537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003539 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
Hanno Becker2b1e3542018-08-06 11:19:13 +01003541 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003542 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003543
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003544 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003545
3546 if( ret <= 0 )
3547 return( ret );
3548
mohammad160352aecb92018-03-28 23:41:40 -07003549 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003550 {
Darryl Green11999bb2018-03-13 15:22:58 +00003551 MBEDTLS_SSL_DEBUG_MSG( 1,
3552 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003553 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3555 }
3556
Paul Bakker5121ce52009-01-03 21:22:43 +00003557 ssl->out_left -= ret;
3558 }
3559
Hanno Becker2b1e3542018-08-06 11:19:13 +01003560#if defined(MBEDTLS_SSL_PROTO_DTLS)
3561 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003562 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003563 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003564 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003565 else
3566#endif
3567 {
3568 ssl->out_hdr = ssl->out_buf + 8;
3569 }
3570 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003573
3574 return( 0 );
3575}
3576
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003577/*
3578 * Functions to handle the DTLS retransmission state machine
3579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581/*
3582 * Append current handshake message to current outgoing flight
3583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003586 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003587 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3588 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3589 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003590
3591 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003592 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003594 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003595 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003596 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003597 }
3598
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003599 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003600 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003602 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003603 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 }
3605
3606 /* Copy current handshake message with headers */
3607 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3608 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003610 msg->next = NULL;
3611
3612 /* Append to the current flight */
3613 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003614 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003615 else
3616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003617 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618 while( cur->next != NULL )
3619 cur = cur->next;
3620 cur->next = msg;
3621 }
3622
Hanno Becker3b235902018-08-06 09:54:53 +01003623 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003624 return( 0 );
3625}
3626
3627/*
3628 * Free the current flight of handshake messages
3629 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003630static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003631{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003632 mbedtls_ssl_flight_item *cur = flight;
3633 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003634
3635 while( cur != NULL )
3636 {
3637 next = cur->next;
3638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003639 mbedtls_free( cur->p );
3640 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003641
3642 cur = next;
3643 }
3644}
3645
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003646#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3647static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003648#endif
3649
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003650/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003651 * Swap transform_out and out_ctr with the alternative ones
3652 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003654{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656 unsigned char tmp_out_ctr[8];
3657
3658 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661 return;
3662 }
3663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003664 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003665
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003667 tmp_transform = ssl->transform_out;
3668 ssl->transform_out = ssl->handshake->alt_transform_out;
3669 ssl->handshake->alt_transform_out = tmp_transform;
3670
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003671 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003672 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3673 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003674 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003675
3676 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003677 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3680 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003682 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3685 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003686 }
3687 }
3688#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003689}
3690
3691/*
3692 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003693 */
3694int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3695{
3696 int ret = 0;
3697
3698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3699
3700 ret = mbedtls_ssl_flight_transmit( ssl );
3701
3702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3703
3704 return( ret );
3705}
3706
3707/*
3708 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003709 *
3710 * Need to remember the current message in case flush_output returns
3711 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003712 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003713 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003714int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003715{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003716 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003720 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003722
3723 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003724 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003725 ssl_swap_epochs( ssl );
3726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003728 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003729
3730 while( ssl->handshake->cur_msg != NULL )
3731 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003732 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003733 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003734
Hanno Beckere1dcb032018-08-17 16:47:58 +01003735 int const is_finished =
3736 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3737 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3738
Hanno Becker04da1892018-08-14 13:22:10 +01003739 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3740 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3741
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003742 /* Swap epochs before sending Finished: we can't do it after
3743 * sending ChangeCipherSpec, in case write returns WANT_READ.
3744 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003745 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003746 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003748 ssl_swap_epochs( ssl );
3749 }
3750
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 ret = ssl_get_remaining_payload_in_datagram( ssl );
3752 if( ret < 0 )
3753 return( ret );
3754 max_frag_len = (size_t) ret;
3755
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003756 /* CCS is copied as is, while HS messages may need fragmentation */
3757 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3758 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003759 if( max_frag_len == 0 )
3760 {
3761 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3762 return( ret );
3763
3764 continue;
3765 }
3766
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003767 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003768 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003769 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003770
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003771 /* Update position inside current message */
3772 ssl->handshake->cur_msg_p += cur->len;
3773 }
3774 else
3775 {
3776 const unsigned char * const p = ssl->handshake->cur_msg_p;
3777 const size_t hs_len = cur->len - 12;
3778 const size_t frag_off = p - ( cur->p + 12 );
3779 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003780 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781
Hanno Beckere1dcb032018-08-17 16:47:58 +01003782 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003783 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003784 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003785 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003786
Hanno Becker67bc7c32018-08-06 11:33:50 +01003787 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3788 return( ret );
3789
3790 continue;
3791 }
3792 max_hs_frag_len = max_frag_len - 12;
3793
3794 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3795 max_hs_frag_len : rem_len;
3796
3797 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003798 {
3799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003800 (unsigned) cur_hs_frag_len,
3801 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003802 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003803
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804 /* Messages are stored with handshake headers as if not fragmented,
3805 * copy beginning of headers then fill fragmentation fields.
3806 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3807 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003808
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003809 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3810 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3811 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3812
Hanno Becker67bc7c32018-08-06 11:33:50 +01003813 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3814 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3815 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003816
3817 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3818
Hanno Becker3f7b9732018-08-28 09:53:25 +01003819 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3821 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003822 ssl->out_msgtype = cur->type;
3823
3824 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003825 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003826 }
3827
3828 /* If done with the current message move to the next one if any */
3829 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3830 {
3831 if( cur->next != NULL )
3832 {
3833 ssl->handshake->cur_msg = cur->next;
3834 ssl->handshake->cur_msg_p = cur->next->p + 12;
3835 }
3836 else
3837 {
3838 ssl->handshake->cur_msg = NULL;
3839 ssl->handshake->cur_msg_p = NULL;
3840 }
3841 }
3842
3843 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003844 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003846 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003847 return( ret );
3848 }
3849 }
3850
Hanno Becker67bc7c32018-08-06 11:33:50 +01003851 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3852 return( ret );
3853
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003854 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3856 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003857 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003860 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3861 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003862
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003863 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003864
3865 return( 0 );
3866}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867
3868/*
3869 * To be called when the last message of an incoming flight is received.
3870 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003871void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003872{
3873 /* We won't need to resend that one any more */
3874 ssl_flight_free( ssl->handshake->flight );
3875 ssl->handshake->flight = NULL;
3876 ssl->handshake->cur_msg = NULL;
3877
3878 /* The next incoming flight will start with this msg_seq */
3879 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3880
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003881 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003882 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003883
Hanno Becker0271f962018-08-16 13:23:47 +01003884 /* Clear future message buffering structure. */
3885 ssl_buffering_free( ssl );
3886
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003887 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003888 ssl_set_timer( ssl, 0 );
3889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3891 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003892 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003894 }
3895 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003897}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898
3899/*
3900 * To be called when the last message of an outgoing flight is send.
3901 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003903{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003904 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003905 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3908 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003911 }
3912 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003913 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003916
Paul Bakker5121ce52009-01-03 21:22:43 +00003917/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003918 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003919 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003920
3921/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003922 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003923 *
3924 * - fill in handshake headers
3925 * - update handshake checksum
3926 * - DTLS: save message for resending
3927 * - then pass to the record layer
3928 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003929 * DTLS: except for HelloRequest, messages are only queued, and will only be
3930 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003931 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003932 * Inputs:
3933 * - ssl->out_msglen: 4 + actual handshake message len
3934 * (4 is the size of handshake headers for TLS)
3935 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3936 * - ssl->out_msg + 4: the handshake message body
3937 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003938 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003939 * - ssl->out_msglen: the length of the record contents
3940 * (including handshake headers but excluding record headers)
3941 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003942 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003943int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003944{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003945 int ret;
3946 const size_t hs_len = ssl->out_msglen - 4;
3947 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003948
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3950
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003951 /*
3952 * Sanity checks
3953 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003954 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003955 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3956 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003957 /* In SSLv3, the client might send a NoCertificate alert. */
3958#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3959 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3960 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3961 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3962#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3963 {
3964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3966 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003967 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003968
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003969 /* Whenever we send anything different from a
3970 * HelloRequest we should be in a handshake - double check. */
3971 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3972 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003973 ssl->handshake == NULL )
3974 {
3975 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3976 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3977 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003980 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003983 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3985 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003986 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003987#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003988
Hanno Beckerb50a2532018-08-06 11:52:54 +01003989 /* Double-check that we did not exceed the bounds
3990 * of the outgoing record buffer.
3991 * This should never fail as the various message
3992 * writing functions must obey the bounds of the
3993 * outgoing record buffer, but better be safe.
3994 *
3995 * Note: We deliberately do not check for the MTU or MFL here.
3996 */
3997 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3998 {
3999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4000 "size %u, maximum %u",
4001 (unsigned) ssl->out_msglen,
4002 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4003 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4004 }
4005
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004006 /*
4007 * Fill handshake headers
4008 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004010 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004011 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4012 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4013 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004014
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004015 /*
4016 * DTLS has additional fields in the Handshake layer,
4017 * between the length field and the actual payload:
4018 * uint16 message_seq;
4019 * uint24 fragment_offset;
4020 * uint24 fragment_length;
4021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004024 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004025 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004026 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004027 {
4028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4029 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004030 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004031 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004032 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4033 }
4034
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004035 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004036 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004037
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004038 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004039 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004040 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004041 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4042 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4043 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004044 }
4045 else
4046 {
4047 ssl->out_msg[4] = 0;
4048 ssl->out_msg[5] = 0;
4049 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004050
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004051 /* Handshake hashes are computed without fragmentation,
4052 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004053 memset( ssl->out_msg + 6, 0x00, 3 );
4054 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004057
Hanno Becker0207e532018-08-28 10:28:28 +01004058 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004059 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4060 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004061 }
4062
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004063 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004065 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004066 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4067 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068 {
4069 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004071 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004072 return( ret );
4073 }
4074 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004075 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004076#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004077 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004078 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004079 {
4080 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4081 return( ret );
4082 }
4083 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004084
4085 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4086
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004087 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004088}
4089
4090/*
4091 * Record layer functions
4092 */
4093
4094/*
4095 * Write current record.
4096 *
4097 * Uses:
4098 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4099 * - ssl->out_msglen: length of the record content (excl headers)
4100 * - ssl->out_msg: record content
4101 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004102int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004103{
4104 int ret, done = 0;
4105 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004106 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004107
4108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004111 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004113 {
4114 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004117 return( ret );
4118 }
4119
4120 len = ssl->out_msglen;
4121 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4125 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004129 ret = mbedtls_ssl_hw_record_write( ssl );
4130 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4133 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004134 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004135
4136 if( ret == 0 )
4137 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004138 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004139#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004140 if( !done )
4141 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004142 unsigned i;
4143 size_t protected_record_size;
4144
Hanno Becker6430faf2019-05-08 11:57:13 +01004145 /* Skip writing the record content type to after the encryption,
4146 * as it may change when using the CID extension. */
4147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004148 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004149 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004150
Hanno Becker19859472018-08-06 09:40:20 +01004151 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004152 ssl->out_len[0] = (unsigned char)( len >> 8 );
4153 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004154
Paul Bakker48916f92012-09-16 19:57:18 +00004155 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004156 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004157 mbedtls_record rec;
4158
4159 rec.buf = ssl->out_iv;
4160 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4161 ( ssl->out_iv - ssl->out_buf );
4162 rec.data_len = ssl->out_msglen;
4163 rec.data_offset = ssl->out_msg - rec.buf;
4164
4165 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4166 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4167 ssl->conf->transport, rec.ver );
4168 rec.type = ssl->out_msgtype;
4169
Hanno Beckera0e20d02019-05-15 14:03:01 +01004170#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004171 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004172 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004173#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004174
Hanno Beckera18d1322018-01-03 14:27:32 +00004175 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004176 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004178 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004179 return( ret );
4180 }
4181
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004182 if( rec.data_offset != 0 )
4183 {
4184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4185 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4186 }
4187
Hanno Becker6430faf2019-05-08 11:57:13 +01004188 /* Update the record content type and CID. */
4189 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004190#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004191 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004192#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004193 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004194 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4195 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004196 }
4197
Hanno Becker5903de42019-05-03 14:46:38 +01004198 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004199
4200#if defined(MBEDTLS_SSL_PROTO_DTLS)
4201 /* In case of DTLS, double-check that we don't exceed
4202 * the remaining space in the datagram. */
4203 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4204 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004205 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004206 if( ret < 0 )
4207 return( ret );
4208
4209 if( protected_record_size > (size_t) ret )
4210 {
4211 /* Should never happen */
4212 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4213 }
4214 }
4215#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004216
Hanno Becker6430faf2019-05-08 11:57:13 +01004217 /* Now write the potentially updated record content type. */
4218 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004221 "version = [%d:%d], msglen = %d",
4222 ssl->out_hdr[0], ssl->out_hdr[1],
4223 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004226 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004227
4228 ssl->out_left += protected_record_size;
4229 ssl->out_hdr += protected_record_size;
4230 ssl_update_out_pointers( ssl, ssl->transform_out );
4231
Hanno Becker04484622018-08-06 09:49:38 +01004232 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4233 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4234 break;
4235
4236 /* The loop goes to its end iff the counter is wrapping */
4237 if( i == ssl_ep_len( ssl ) )
4238 {
4239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4240 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4241 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004242 }
4243
Hanno Becker67bc7c32018-08-06 11:33:50 +01004244#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4246 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004248 size_t remaining;
4249 ret = ssl_get_remaining_payload_in_datagram( ssl );
4250 if( ret < 0 )
4251 {
4252 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4253 ret );
4254 return( ret );
4255 }
4256
4257 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004258 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004259 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004260 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004261 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004262 else
4263 {
Hanno Becker513815a2018-08-20 11:56:09 +01004264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004265 }
4266 }
4267#endif /* MBEDTLS_SSL_PROTO_DTLS */
4268
4269 if( ( flush == SSL_FORCE_FLUSH ) &&
4270 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004273 return( ret );
4274 }
4275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004277
4278 return( 0 );
4279}
4280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004281#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004282
4283static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4284{
4285 if( ssl->in_msglen < ssl->in_hslen ||
4286 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4287 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4288 {
4289 return( 1 );
4290 }
4291 return( 0 );
4292}
Hanno Becker44650b72018-08-16 12:51:11 +01004293
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004294static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004295{
4296 return( ( ssl->in_msg[9] << 16 ) |
4297 ( ssl->in_msg[10] << 8 ) |
4298 ssl->in_msg[11] );
4299}
4300
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004301static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004302{
4303 return( ( ssl->in_msg[6] << 16 ) |
4304 ( ssl->in_msg[7] << 8 ) |
4305 ssl->in_msg[8] );
4306}
4307
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004308static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004309{
4310 uint32_t msg_len, frag_off, frag_len;
4311
4312 msg_len = ssl_get_hs_total_len( ssl );
4313 frag_off = ssl_get_hs_frag_off( ssl );
4314 frag_len = ssl_get_hs_frag_len( ssl );
4315
4316 if( frag_off > msg_len )
4317 return( -1 );
4318
4319 if( frag_len > msg_len - frag_off )
4320 return( -1 );
4321
4322 if( frag_len + 12 > ssl->in_msglen )
4323 return( -1 );
4324
4325 return( 0 );
4326}
4327
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004328/*
4329 * Mark bits in bitmask (used for DTLS HS reassembly)
4330 */
4331static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4332{
4333 unsigned int start_bits, end_bits;
4334
4335 start_bits = 8 - ( offset % 8 );
4336 if( start_bits != 8 )
4337 {
4338 size_t first_byte_idx = offset / 8;
4339
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004340 /* Special case */
4341 if( len <= start_bits )
4342 {
4343 for( ; len != 0; len-- )
4344 mask[first_byte_idx] |= 1 << ( start_bits - len );
4345
4346 /* Avoid potential issues with offset or len becoming invalid */
4347 return;
4348 }
4349
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004350 offset += start_bits; /* Now offset % 8 == 0 */
4351 len -= start_bits;
4352
4353 for( ; start_bits != 0; start_bits-- )
4354 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4355 }
4356
4357 end_bits = len % 8;
4358 if( end_bits != 0 )
4359 {
4360 size_t last_byte_idx = ( offset + len ) / 8;
4361
4362 len -= end_bits; /* Now len % 8 == 0 */
4363
4364 for( ; end_bits != 0; end_bits-- )
4365 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4366 }
4367
4368 memset( mask + offset / 8, 0xFF, len / 8 );
4369}
4370
4371/*
4372 * Check that bitmask is full
4373 */
4374static int ssl_bitmask_check( unsigned char *mask, size_t len )
4375{
4376 size_t i;
4377
4378 for( i = 0; i < len / 8; i++ )
4379 if( mask[i] != 0xFF )
4380 return( -1 );
4381
4382 for( i = 0; i < len % 8; i++ )
4383 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4384 return( -1 );
4385
4386 return( 0 );
4387}
4388
Hanno Becker56e205e2018-08-16 09:06:12 +01004389/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004390static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004391 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004392{
Hanno Becker56e205e2018-08-16 09:06:12 +01004393 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004394
Hanno Becker56e205e2018-08-16 09:06:12 +01004395 alloc_len = 12; /* Handshake header */
4396 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004397
Hanno Beckerd07df862018-08-16 09:14:58 +01004398 if( add_bitmap )
4399 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004400
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004401 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004402}
Hanno Becker56e205e2018-08-16 09:06:12 +01004403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004405
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004406static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004407{
4408 return( ( ssl->in_msg[1] << 16 ) |
4409 ( ssl->in_msg[2] << 8 ) |
4410 ssl->in_msg[3] );
4411}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004412
Simon Butcher99000142016-10-13 17:21:01 +01004413int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004418 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004420 }
4421
Hanno Becker12555c62018-08-16 12:47:53 +01004422 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004425 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004426 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004428#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004429 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004430 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004431 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004432 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004433
Hanno Becker44650b72018-08-16 12:51:11 +01004434 if( ssl_check_hs_header( ssl ) != 0 )
4435 {
4436 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4437 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4438 }
4439
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004440 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004441 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4442 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4443 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4444 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004445 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004446 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4447 {
4448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4449 recv_msg_seq,
4450 ssl->handshake->in_msg_seq ) );
4451 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4452 }
4453
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004454 /* Retransmit only on last message from previous flight, to avoid
4455 * too many retransmissions.
4456 * Besides, No sane server ever retransmits HelloVerifyRequest */
4457 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004458 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004461 "message_seq = %d, start_of_flight = %d",
4462 recv_msg_seq,
4463 ssl->handshake->in_flight_start_seq ) );
4464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004468 return( ret );
4469 }
4470 }
4471 else
4472 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004474 "message_seq = %d, expected = %d",
4475 recv_msg_seq,
4476 ssl->handshake->in_msg_seq ) );
4477 }
4478
Hanno Becker90333da2017-10-10 11:27:13 +01004479 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004480 }
4481 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004482
Hanno Becker6d97ef52018-08-16 13:09:04 +01004483 /* Message reassembly is handled alongside buffering of future
4484 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004485 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004486 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004487 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004488 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004490 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004491 }
4492 }
4493 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004495 /* With TLS we don't handle fragmentation (for now) */
4496 if( ssl->in_msglen < ssl->in_hslen )
4497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4499 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004500 }
4501
Simon Butcher99000142016-10-13 17:21:01 +01004502 return( 0 );
4503}
4504
4505void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4506{
Hanno Becker0271f962018-08-16 13:23:47 +01004507 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004508
Hanno Becker0271f962018-08-16 13:23:47 +01004509 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004510 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004511 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004512 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004513
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004514 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004515#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004516 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004517 ssl->handshake != NULL )
4518 {
Hanno Becker0271f962018-08-16 13:23:47 +01004519 unsigned offset;
4520 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004521
Hanno Becker0271f962018-08-16 13:23:47 +01004522 /* Increment handshake sequence number */
4523 hs->in_msg_seq++;
4524
4525 /*
4526 * Clear up handshake buffering and reassembly structure.
4527 */
4528
4529 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004530 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004531
4532 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004533 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4534 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004535 offset++, hs_buf++ )
4536 {
4537 *hs_buf = *(hs_buf + 1);
4538 }
4539
4540 /* Create a fresh last entry */
4541 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004542 }
4543#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004544}
4545
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004546/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004547 * DTLS anti-replay: RFC 6347 4.1.2.6
4548 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004549 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4550 * Bit n is set iff record number in_window_top - n has been seen.
4551 *
4552 * Usually, in_window_top is the last record number seen and the lsb of
4553 * in_window is set. The only exception is the initial state (record number 0
4554 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004555 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004556#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4557static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004558{
4559 ssl->in_window_top = 0;
4560 ssl->in_window = 0;
4561}
4562
4563static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4564{
4565 return( ( (uint64_t) buf[0] << 40 ) |
4566 ( (uint64_t) buf[1] << 32 ) |
4567 ( (uint64_t) buf[2] << 24 ) |
4568 ( (uint64_t) buf[3] << 16 ) |
4569 ( (uint64_t) buf[4] << 8 ) |
4570 ( (uint64_t) buf[5] ) );
4571}
4572
4573/*
4574 * Return 0 if sequence number is acceptable, -1 otherwise
4575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004576int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004577{
4578 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4579 uint64_t bit;
4580
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004581 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004582 return( 0 );
4583
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004584 if( rec_seqnum > ssl->in_window_top )
4585 return( 0 );
4586
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004587 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004588
4589 if( bit >= 64 )
4590 return( -1 );
4591
4592 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4593 return( -1 );
4594
4595 return( 0 );
4596}
4597
4598/*
4599 * Update replay window on new validated record
4600 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004601void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602{
4603 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4604
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004605 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004606 return;
4607
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004608 if( rec_seqnum > ssl->in_window_top )
4609 {
4610 /* Update window_top and the contents of the window */
4611 uint64_t shift = rec_seqnum - ssl->in_window_top;
4612
4613 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004614 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004615 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004616 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004617 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004618 ssl->in_window |= 1;
4619 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004620
4621 ssl->in_window_top = rec_seqnum;
4622 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004623 else
4624 {
4625 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004626 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004627
4628 if( bit < 64 ) /* Always true, but be extra sure */
4629 ssl->in_window |= (uint64_t) 1 << bit;
4630 }
4631}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004632#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004633
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004634#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004635/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004636static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4637
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004638/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004639 * Without any SSL context, check if a datagram looks like a ClientHello with
4640 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004641 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004642 *
4643 * - if cookie is valid, return 0
4644 * - if ClientHello looks superficially valid but cookie is not,
4645 * fill obuf and set olen, then
4646 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4647 * - otherwise return a specific error code
4648 */
4649static int ssl_check_dtls_clihlo_cookie(
4650 mbedtls_ssl_cookie_write_t *f_cookie_write,
4651 mbedtls_ssl_cookie_check_t *f_cookie_check,
4652 void *p_cookie,
4653 const unsigned char *cli_id, size_t cli_id_len,
4654 const unsigned char *in, size_t in_len,
4655 unsigned char *obuf, size_t buf_len, size_t *olen )
4656{
4657 size_t sid_len, cookie_len;
4658 unsigned char *p;
4659
4660 if( f_cookie_write == NULL || f_cookie_check == NULL )
4661 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4662
4663 /*
4664 * Structure of ClientHello with record and handshake headers,
4665 * and expected values. We don't need to check a lot, more checks will be
4666 * done when actually parsing the ClientHello - skipping those checks
4667 * avoids code duplication and does not make cookie forging any easier.
4668 *
4669 * 0-0 ContentType type; copied, must be handshake
4670 * 1-2 ProtocolVersion version; copied
4671 * 3-4 uint16 epoch; copied, must be 0
4672 * 5-10 uint48 sequence_number; copied
4673 * 11-12 uint16 length; (ignored)
4674 *
4675 * 13-13 HandshakeType msg_type; (ignored)
4676 * 14-16 uint24 length; (ignored)
4677 * 17-18 uint16 message_seq; copied
4678 * 19-21 uint24 fragment_offset; copied, must be 0
4679 * 22-24 uint24 fragment_length; (ignored)
4680 *
4681 * 25-26 ProtocolVersion client_version; (ignored)
4682 * 27-58 Random random; (ignored)
4683 * 59-xx SessionID session_id; 1 byte len + sid_len content
4684 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4685 * ...
4686 *
4687 * Minimum length is 61 bytes.
4688 */
4689 if( in_len < 61 ||
4690 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4691 in[3] != 0 || in[4] != 0 ||
4692 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4693 {
4694 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4695 }
4696
4697 sid_len = in[59];
4698 if( sid_len > in_len - 61 )
4699 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4700
4701 cookie_len = in[60 + sid_len];
4702 if( cookie_len > in_len - 60 )
4703 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4704
4705 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4706 cli_id, cli_id_len ) == 0 )
4707 {
4708 /* Valid cookie */
4709 return( 0 );
4710 }
4711
4712 /*
4713 * If we get here, we've got an invalid cookie, let's prepare HVR.
4714 *
4715 * 0-0 ContentType type; copied
4716 * 1-2 ProtocolVersion version; copied
4717 * 3-4 uint16 epoch; copied
4718 * 5-10 uint48 sequence_number; copied
4719 * 11-12 uint16 length; olen - 13
4720 *
4721 * 13-13 HandshakeType msg_type; hello_verify_request
4722 * 14-16 uint24 length; olen - 25
4723 * 17-18 uint16 message_seq; copied
4724 * 19-21 uint24 fragment_offset; copied
4725 * 22-24 uint24 fragment_length; olen - 25
4726 *
4727 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4728 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4729 *
4730 * Minimum length is 28.
4731 */
4732 if( buf_len < 28 )
4733 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4734
4735 /* Copy most fields and adapt others */
4736 memcpy( obuf, in, 25 );
4737 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4738 obuf[25] = 0xfe;
4739 obuf[26] = 0xff;
4740
4741 /* Generate and write actual cookie */
4742 p = obuf + 28;
4743 if( f_cookie_write( p_cookie,
4744 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4745 {
4746 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4747 }
4748
4749 *olen = p - obuf;
4750
4751 /* Go back and fill length fields */
4752 obuf[27] = (unsigned char)( *olen - 28 );
4753
4754 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4755 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4756 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4757
4758 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4759 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4760
4761 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4762}
4763
4764/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765 * Handle possible client reconnect with the same UDP quadruplet
4766 * (RFC 6347 Section 4.2.8).
4767 *
4768 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4769 * that looks like a ClientHello.
4770 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004771 * - if the input looks like a ClientHello without cookies,
4772 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004774 * - if the input looks like a ClientHello with a valid cookie,
4775 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004776 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004777 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004778 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004779 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004780 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4781 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782 */
4783static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4784{
4785 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004786 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004787
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004788 ret = ssl_check_dtls_clihlo_cookie(
4789 ssl->conf->f_cookie_write,
4790 ssl->conf->f_cookie_check,
4791 ssl->conf->p_cookie,
4792 ssl->cli_id, ssl->cli_id_len,
4793 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004794 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004795
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004796 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4797
4798 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004799 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004800 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004801 * If the error is permanent we'll catch it later,
4802 * if it's not, then hopefully it'll work next time. */
4803 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4804
4805 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004806 }
4807
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004808 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004809 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004810 /* Got a valid cookie, partially reset context */
4811 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4812 {
4813 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4814 return( ret );
4815 }
4816
4817 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004818 }
4819
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004820 return( ret );
4821}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004822#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004823
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004824static int ssl_check_record_type( uint8_t record_type )
4825{
4826 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4827 record_type != MBEDTLS_SSL_MSG_ALERT &&
4828 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4829 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4830 {
4831 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4832 }
4833
4834 return( 0 );
4835}
4836
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004837/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004838 * ContentType type;
4839 * ProtocolVersion version;
4840 * uint16 epoch; // DTLS only
4841 * uint48 sequence_number; // DTLS only
4842 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004843 *
4844 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004845 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004846 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4847 *
4848 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004849 * 1. proceed with the record if this function returns 0
4850 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4851 * 3. return CLIENT_RECONNECT if this function return that value
4852 * 4. drop the whole datagram if this function returns anything else.
4853 * Point 2 is needed when the peer is resending, and we have already received
4854 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004855 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004856static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004857{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004858 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004859 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004860
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004861 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004862
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004864 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004865
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004866 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004867#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004868 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4869 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4870 ssl->conf->cid_len != 0 )
4871 {
4872 /* Shift pointers to account for record header including CID
4873 * struct {
4874 * ContentType special_type = tls12_cid;
4875 * ProtocolVersion version;
4876 * uint16 epoch;
4877 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004878 * opaque cid[cid_length]; // Additional field compared to
4879 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004880 * uint16 length;
4881 * opaque enc_content[DTLSCiphertext.length];
4882 * } DTLSCiphertext;
4883 */
4884
4885 /* So far, we only support static CID lengths
4886 * fixed in the configuration. */
4887 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4888 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4889 }
4890 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004891#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004892 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004895
4896#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004897 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4898 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004899 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4900#endif /* MBEDTLS_SSL_PROTO_DTLS */
4901 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4902 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004905 }
4906
4907 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004908 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4911 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 }
4913
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004914 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4917 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004918 }
4919
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004920 /* Now that the total length of the record header is known, ensure
4921 * that the current datagram is large enough to hold it.
4922 * This would fail, for example, if we received a datagram of
4923 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4924 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4925 if( ret != 0 )
4926 {
4927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4928 return( ret );
4929 }
4930 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4931
4932 /* Parse and validate record length
4933 * This must happen after the CID parsing because
4934 * its position in the record header depends on
4935 * the presence of a CID. */
4936
4937 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004938 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004939 "version = [%d:%d], msglen = %d",
4940 ssl->in_msgtype,
4941 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004942
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004943 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004944 * DTLS-related tests.
4945 * Check epoch before checking length constraint because
4946 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4947 * message gets duplicated before the corresponding Finished message,
4948 * the second ChangeCipherSpec should be discarded because it belongs
4949 * to an old epoch, but not because its length is shorter than
4950 * the minimum record length for packets using the new record transform.
4951 * Note that these two kinds of failures are handled differently,
4952 * as an unexpected record is silently skipped but an invalid
4953 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004954 */
4955#if defined(MBEDTLS_SSL_PROTO_DTLS)
4956 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4957 {
4958 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4959
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004960 /* Check epoch (and sequence number) with DTLS */
4961 if( rec_epoch != ssl->in_epoch )
4962 {
4963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4964 "expected %d, received %d",
4965 ssl->in_epoch, rec_epoch ) );
4966
4967#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4968 /*
4969 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4970 * access the first byte of record content (handshake type), as we
4971 * have an active transform (possibly iv_len != 0), so use the
4972 * fact that the record header len is 13 instead.
4973 */
4974 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4975 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4976 rec_epoch == 0 &&
4977 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4978 ssl->in_left > 13 &&
4979 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4980 {
4981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4982 "from the same port" ) );
4983 return( ssl_handle_possible_reconnect( ssl ) );
4984 }
4985 else
4986#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004987 {
4988 /* Consider buffering the record. */
4989 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4990 {
4991 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4992 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4993 }
4994
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004995 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004996 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004997 }
4998
4999#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5000 /* Replay detection only works for the current epoch */
5001 if( rec_epoch == ssl->in_epoch &&
5002 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5003 {
5004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5005 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5006 }
5007#endif
5008 }
5009#endif /* MBEDTLS_SSL_PROTO_DTLS */
5010
Hanno Becker52c6dc62017-05-26 16:07:36 +01005011
5012 /* Check length against bounds of the current transform and version */
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005013 if( ssl->transform_in != NULL )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005014 {
5015 if( ssl->in_msglen < ssl->transform_in->minlen )
5016 {
5017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5019 }
Hanno Becker52c6dc62017-05-26 16:07:36 +01005020 }
5021
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005022 return( 0 );
5023}
Paul Bakker5121ce52009-01-03 21:22:43 +00005024
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005025/*
5026 * If applicable, decrypt (and decompress) record content
5027 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005028static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005029{
5030 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005032 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005033 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005035#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5036 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005038 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005040 ret = mbedtls_ssl_hw_record_read( ssl );
5041 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5044 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005045 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005046
5047 if( ret == 0 )
5048 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005051 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005052 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005053 mbedtls_record rec;
5054
5055 rec.buf = ssl->in_iv;
5056 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5057 - ( ssl->in_iv - ssl->in_buf );
5058 rec.data_len = ssl->in_msglen;
5059 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005060#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005061 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005062 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005063#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005064
5065 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5066 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5067 ssl->conf->transport, rec.ver );
5068 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005069 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5070 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005073
Hanno Beckera0e20d02019-05-15 14:03:01 +01005074#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005075 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5076 ssl->conf->ignore_unexpected_cid
5077 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5078 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005079 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005080 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005081 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005082#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005083
Paul Bakker5121ce52009-01-03 21:22:43 +00005084 return( ret );
5085 }
5086
Hanno Becker6430faf2019-05-08 11:57:13 +01005087 if( ssl->in_msgtype != rec.type )
5088 {
5089 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5090 ssl->in_msgtype, rec.type ) );
5091 }
5092
5093 /* The record content type may change during decryption,
5094 * so re-read it. */
5095 ssl->in_msgtype = rec.type;
5096 /* Also update the input buffer, because unfortunately
5097 * the server-side ssl_parse_client_hello() reparses the
5098 * record header when receiving a ClientHello initiating
5099 * a renegotiation. */
5100 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005101 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005102 ssl->in_msglen = rec.data_len;
5103 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5104 ssl->in_len[1] = (unsigned char)( rec.data_len );
5105
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005106 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5107 ssl->in_msg, ssl->in_msglen );
5108
Hanno Beckera0e20d02019-05-15 14:03:01 +01005109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005110 /* We have already checked the record content type
5111 * in ssl_parse_record_header(), failing or silently
5112 * dropping the record in the case of an unknown type.
5113 *
5114 * Since with the use of CIDs, the record content type
5115 * might change during decryption, re-check the record
5116 * content type, but treat a failure as fatal this time. */
5117 if( ssl_check_record_type( ssl->in_msgtype ) )
5118 {
5119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5121 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005122#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005123
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005124 if( ssl->in_msglen == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005125 {
5126#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5127 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5128 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5129 {
5130 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5132 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5133 }
5134#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5135
5136 ssl->nb_zero++;
5137
5138 /*
5139 * Three or more empty messages may be a DoS attack
5140 * (excessive CPU consumption).
5141 */
5142 if( ssl->nb_zero > 3 )
5143 {
5144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005145 "messages, possible DoS attack" ) );
5146 /* Treat the records as if they were not properly authenticated,
5147 * thereby failing the connection if we see more than allowed
5148 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005149 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5150 }
5151 }
5152 else
5153 ssl->nb_zero = 0;
5154
5155#if defined(MBEDTLS_SSL_PROTO_DTLS)
5156 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5157 {
5158 ; /* in_ctr read from peer, not maintained internally */
5159 }
5160 else
5161#endif
5162 {
5163 unsigned i;
5164 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5165 if( ++ssl->in_ctr[i - 1] != 0 )
5166 break;
5167
5168 /* The loop goes to its end iff the counter is wrapping */
5169 if( i == ssl_ep_len( ssl ) )
5170 {
5171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5172 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5173 }
5174 }
5175
Paul Bakker5121ce52009-01-03 21:22:43 +00005176 }
5177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005178#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005179 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005180 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005181 {
5182 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5183 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005184 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005185 return( ret );
5186 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005187 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005188#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005190#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005192 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005194 }
5195#endif
5196
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005197 /* Check actual (decrypted) record content length against
5198 * configured maximum. */
5199 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5200 {
5201 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5202 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5203 }
5204
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005205 return( 0 );
5206}
5207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005209
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005210/*
5211 * Read a record.
5212 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005213 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5214 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5215 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005216 */
Hanno Becker1097b342018-08-15 14:09:41 +01005217
5218/* Helper functions for mbedtls_ssl_read_record(). */
5219static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005220static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5221static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005222
Hanno Becker327c93b2018-08-15 13:56:18 +01005223int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005224 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005225{
5226 int ret;
5227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005229
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005230 if( ssl->keep_current_message == 0 )
5231 {
5232 do {
Simon Butcher99000142016-10-13 17:21:01 +01005233
Hanno Becker26994592018-08-15 14:14:59 +01005234 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005235 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005236 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005237
Hanno Beckere74d5562018-08-15 14:26:08 +01005238 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005239 {
Hanno Becker40f50842018-08-15 14:48:01 +01005240#if defined(MBEDTLS_SSL_PROTO_DTLS)
5241 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005242
Hanno Becker40f50842018-08-15 14:48:01 +01005243 /* We only check for buffered messages if the
5244 * current datagram is fully consumed. */
5245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005246 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005247 {
Hanno Becker40f50842018-08-15 14:48:01 +01005248 if( ssl_load_buffered_message( ssl ) == 0 )
5249 have_buffered = 1;
5250 }
5251
5252 if( have_buffered == 0 )
5253#endif /* MBEDTLS_SSL_PROTO_DTLS */
5254 {
5255 ret = ssl_get_next_record( ssl );
5256 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5257 continue;
5258
5259 if( ret != 0 )
5260 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005261 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005262 return( ret );
5263 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005264 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005265 }
5266
5267 ret = mbedtls_ssl_handle_message_type( ssl );
5268
Hanno Becker40f50842018-08-15 14:48:01 +01005269#if defined(MBEDTLS_SSL_PROTO_DTLS)
5270 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5271 {
5272 /* Buffer future message */
5273 ret = ssl_buffer_message( ssl );
5274 if( ret != 0 )
5275 return( ret );
5276
5277 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5278 }
5279#endif /* MBEDTLS_SSL_PROTO_DTLS */
5280
Hanno Becker90333da2017-10-10 11:27:13 +01005281 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5282 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005283
5284 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005285 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005286 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005287 return( ret );
5288 }
5289
Hanno Becker327c93b2018-08-15 13:56:18 +01005290 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005291 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005292 {
5293 mbedtls_ssl_update_handshake_status( ssl );
5294 }
Simon Butcher99000142016-10-13 17:21:01 +01005295 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005296 else
Simon Butcher99000142016-10-13 17:21:01 +01005297 {
Hanno Becker02f59072018-08-15 14:00:24 +01005298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005299 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005300 }
5301
5302 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5303
5304 return( 0 );
5305}
5306
Hanno Becker40f50842018-08-15 14:48:01 +01005307#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005308static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005309{
Hanno Becker40f50842018-08-15 14:48:01 +01005310 if( ssl->in_left > ssl->next_record_offset )
5311 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005312
Hanno Becker40f50842018-08-15 14:48:01 +01005313 return( 0 );
5314}
5315
5316static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5317{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005318 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005319 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005320 int ret = 0;
5321
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005322 if( hs == NULL )
5323 return( -1 );
5324
Hanno Beckere00ae372018-08-20 09:39:42 +01005325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5326
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005327 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5328 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5329 {
5330 /* Check if we have seen a ChangeCipherSpec before.
5331 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005332 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005333 {
5334 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5335 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005336 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005337 }
5338
Hanno Becker39b8bc92018-08-28 17:17:13 +01005339 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005340 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5341 ssl->in_msglen = 1;
5342 ssl->in_msg[0] = 1;
5343
5344 /* As long as they are equal, the exact value doesn't matter. */
5345 ssl->in_left = 0;
5346 ssl->next_record_offset = 0;
5347
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005348 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005349 goto exit;
5350 }
Hanno Becker37f95322018-08-16 13:55:32 +01005351
Hanno Beckerb8f50142018-08-28 10:01:34 +01005352#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005353 /* Debug only */
5354 {
5355 unsigned offset;
5356 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5357 {
5358 hs_buf = &hs->buffering.hs[offset];
5359 if( hs_buf->is_valid == 1 )
5360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5362 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005363 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005364 }
5365 }
5366 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005367#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005368
5369 /* Check if we have buffered and/or fully reassembled the
5370 * next handshake message. */
5371 hs_buf = &hs->buffering.hs[0];
5372 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5373 {
5374 /* Synthesize a record containing the buffered HS message. */
5375 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5376 ( hs_buf->data[2] << 8 ) |
5377 hs_buf->data[3];
5378
5379 /* Double-check that we haven't accidentally buffered
5380 * a message that doesn't fit into the input buffer. */
5381 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5382 {
5383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5384 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5385 }
5386
5387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5388 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5389 hs_buf->data, msg_len + 12 );
5390
5391 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5392 ssl->in_hslen = msg_len + 12;
5393 ssl->in_msglen = msg_len + 12;
5394 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5395
5396 ret = 0;
5397 goto exit;
5398 }
5399 else
5400 {
5401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5402 hs->in_msg_seq ) );
5403 }
5404
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005405 ret = -1;
5406
5407exit:
5408
5409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5410 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005411}
5412
Hanno Beckera02b0b42018-08-21 17:20:27 +01005413static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5414 size_t desired )
5415{
5416 int offset;
5417 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5419 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005420
Hanno Becker01315ea2018-08-21 17:22:17 +01005421 /* Get rid of future records epoch first, if such exist. */
5422 ssl_free_buffered_record( ssl );
5423
5424 /* Check if we have enough space available now. */
5425 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5426 hs->buffering.total_bytes_buffered ) )
5427 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005429 return( 0 );
5430 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005431
Hanno Becker4f432ad2018-08-28 10:02:32 +01005432 /* We don't have enough space to buffer the next expected handshake
5433 * message. Remove buffers used for future messages to gain space,
5434 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005435 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5436 offset >= 0; offset-- )
5437 {
5438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5439 offset ) );
5440
Hanno Beckerb309b922018-08-23 13:18:05 +01005441 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005442
5443 /* Check if we have enough space available now. */
5444 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5445 hs->buffering.total_bytes_buffered ) )
5446 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005448 return( 0 );
5449 }
5450 }
5451
5452 return( -1 );
5453}
5454
Hanno Becker40f50842018-08-15 14:48:01 +01005455static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5456{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005457 int ret = 0;
5458 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5459
5460 if( hs == NULL )
5461 return( 0 );
5462
5463 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5464
5465 switch( ssl->in_msgtype )
5466 {
5467 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005469
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005470 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005471 break;
5472
5473 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005474 {
5475 unsigned recv_msg_seq_offset;
5476 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5477 mbedtls_ssl_hs_buffer *hs_buf;
5478 size_t msg_len = ssl->in_hslen - 12;
5479
5480 /* We should never receive an old handshake
5481 * message - double-check nonetheless. */
5482 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5483 {
5484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5485 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5486 }
5487
5488 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5489 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5490 {
5491 /* Silently ignore -- message too far in the future */
5492 MBEDTLS_SSL_DEBUG_MSG( 2,
5493 ( "Ignore future HS message with sequence number %u, "
5494 "buffering window %u - %u",
5495 recv_msg_seq, ssl->handshake->in_msg_seq,
5496 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5497
5498 goto exit;
5499 }
5500
5501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5502 recv_msg_seq, recv_msg_seq_offset ) );
5503
5504 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5505
5506 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005507 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005508 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005509 size_t reassembly_buf_sz;
5510
Hanno Becker37f95322018-08-16 13:55:32 +01005511 hs_buf->is_fragmented =
5512 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5513
5514 /* We copy the message back into the input buffer
5515 * after reassembly, so check that it's not too large.
5516 * This is an implementation-specific limitation
5517 * and not one from the standard, hence it is not
5518 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005519 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005520 {
5521 /* Ignore message */
5522 goto exit;
5523 }
5524
Hanno Beckere0b150f2018-08-21 15:51:03 +01005525 /* Check if we have enough space to buffer the message. */
5526 if( hs->buffering.total_bytes_buffered >
5527 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5528 {
5529 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5530 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5531 }
5532
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005533 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5534 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005535
5536 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5537 hs->buffering.total_bytes_buffered ) )
5538 {
5539 if( recv_msg_seq_offset > 0 )
5540 {
5541 /* If we can't buffer a future message because
5542 * of space limitations -- ignore. */
5543 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",
5544 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5545 (unsigned) hs->buffering.total_bytes_buffered ) );
5546 goto exit;
5547 }
Hanno Beckere1801392018-08-21 16:51:05 +01005548 else
5549 {
5550 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",
5551 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5552 (unsigned) hs->buffering.total_bytes_buffered ) );
5553 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005554
Hanno Beckera02b0b42018-08-21 17:20:27 +01005555 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005556 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005557 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",
5558 (unsigned) msg_len,
5559 (unsigned) reassembly_buf_sz,
5560 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005561 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005562 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5563 goto exit;
5564 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005565 }
5566
5567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5568 msg_len ) );
5569
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005570 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5571 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005572 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005573 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005574 goto exit;
5575 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005576 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005577
5578 /* Prepare final header: copy msg_type, length and message_seq,
5579 * then add standardised fragment_offset and fragment_length */
5580 memcpy( hs_buf->data, ssl->in_msg, 6 );
5581 memset( hs_buf->data + 6, 0, 3 );
5582 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5583
5584 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005585
5586 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005587 }
5588 else
5589 {
5590 /* Make sure msg_type and length are consistent */
5591 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5592 {
5593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5594 /* Ignore */
5595 goto exit;
5596 }
5597 }
5598
Hanno Becker4422bbb2018-08-20 09:40:19 +01005599 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005600 {
5601 size_t frag_len, frag_off;
5602 unsigned char * const msg = hs_buf->data + 12;
5603
5604 /*
5605 * Check and copy current fragment
5606 */
5607
5608 /* Validation of header fields already done in
5609 * mbedtls_ssl_prepare_handshake_record(). */
5610 frag_off = ssl_get_hs_frag_off( ssl );
5611 frag_len = ssl_get_hs_frag_len( ssl );
5612
5613 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5614 frag_off, frag_len ) );
5615 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5616
5617 if( hs_buf->is_fragmented )
5618 {
5619 unsigned char * const bitmask = msg + msg_len;
5620 ssl_bitmask_set( bitmask, frag_off, frag_len );
5621 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5622 msg_len ) == 0 );
5623 }
5624 else
5625 {
5626 hs_buf->is_complete = 1;
5627 }
5628
5629 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5630 hs_buf->is_complete ? "" : "not yet " ) );
5631 }
5632
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005633 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005634 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005635
5636 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005637 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005638 break;
5639 }
5640
5641exit:
5642
5643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5644 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005645}
5646#endif /* MBEDTLS_SSL_PROTO_DTLS */
5647
Hanno Becker1097b342018-08-15 14:09:41 +01005648static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005649{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005650 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005651 * Consume last content-layer message and potentially
5652 * update in_msglen which keeps track of the contents'
5653 * consumption state.
5654 *
5655 * (1) Handshake messages:
5656 * Remove last handshake message, move content
5657 * and adapt in_msglen.
5658 *
5659 * (2) Alert messages:
5660 * Consume whole record content, in_msglen = 0.
5661 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005662 * (3) Change cipher spec:
5663 * Consume whole record content, in_msglen = 0.
5664 *
5665 * (4) Application data:
5666 * Don't do anything - the record layer provides
5667 * the application data as a stream transport
5668 * and consumes through mbedtls_ssl_read only.
5669 *
5670 */
5671
5672 /* Case (1): Handshake messages */
5673 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005674 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005675 /* Hard assertion to be sure that no application data
5676 * is in flight, as corrupting ssl->in_msglen during
5677 * ssl->in_offt != NULL is fatal. */
5678 if( ssl->in_offt != NULL )
5679 {
5680 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5681 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5682 }
5683
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005684 /*
5685 * Get next Handshake message in the current record
5686 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005687
Hanno Becker4a810fb2017-05-24 16:27:30 +01005688 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005689 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005690 * current handshake content: If DTLS handshake
5691 * fragmentation is used, that's the fragment
5692 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005693 * size here is faulty and should be changed at
5694 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005695 * (2) While it doesn't seem to cause problems, one
5696 * has to be very careful not to assume that in_hslen
5697 * is always <= in_msglen in a sensible communication.
5698 * Again, it's wrong for DTLS handshake fragmentation.
5699 * The following check is therefore mandatory, and
5700 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005701 * Additionally, ssl->in_hslen might be arbitrarily out of
5702 * bounds after handling a DTLS message with an unexpected
5703 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005704 */
5705 if( ssl->in_hslen < ssl->in_msglen )
5706 {
5707 ssl->in_msglen -= ssl->in_hslen;
5708 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5709 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005710
Hanno Becker4a810fb2017-05-24 16:27:30 +01005711 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5712 ssl->in_msg, ssl->in_msglen );
5713 }
5714 else
5715 {
5716 ssl->in_msglen = 0;
5717 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005718
Hanno Becker4a810fb2017-05-24 16:27:30 +01005719 ssl->in_hslen = 0;
5720 }
5721 /* Case (4): Application data */
5722 else if( ssl->in_offt != NULL )
5723 {
5724 return( 0 );
5725 }
5726 /* Everything else (CCS & Alerts) */
5727 else
5728 {
5729 ssl->in_msglen = 0;
5730 }
5731
Hanno Becker1097b342018-08-15 14:09:41 +01005732 return( 0 );
5733}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005734
Hanno Beckere74d5562018-08-15 14:26:08 +01005735static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5736{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005737 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005738 return( 1 );
5739
5740 return( 0 );
5741}
5742
Hanno Becker5f066e72018-08-16 14:56:31 +01005743#if defined(MBEDTLS_SSL_PROTO_DTLS)
5744
5745static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5746{
5747 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5748 if( hs == NULL )
5749 return;
5750
Hanno Becker01315ea2018-08-21 17:22:17 +01005751 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005752 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005753 hs->buffering.total_bytes_buffered -=
5754 hs->buffering.future_record.len;
5755
5756 mbedtls_free( hs->buffering.future_record.data );
5757 hs->buffering.future_record.data = NULL;
5758 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005759}
5760
5761static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5762{
5763 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5764 unsigned char * rec;
5765 size_t rec_len;
5766 unsigned rec_epoch;
5767
5768 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5769 return( 0 );
5770
5771 if( hs == NULL )
5772 return( 0 );
5773
Hanno Becker5f066e72018-08-16 14:56:31 +01005774 rec = hs->buffering.future_record.data;
5775 rec_len = hs->buffering.future_record.len;
5776 rec_epoch = hs->buffering.future_record.epoch;
5777
5778 if( rec == NULL )
5779 return( 0 );
5780
Hanno Becker4cb782d2018-08-20 11:19:05 +01005781 /* Only consider loading future records if the
5782 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005783 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005784 return( 0 );
5785
Hanno Becker5f066e72018-08-16 14:56:31 +01005786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5787
5788 if( rec_epoch != ssl->in_epoch )
5789 {
5790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5791 goto exit;
5792 }
5793
5794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5795
5796 /* Double-check that the record is not too large */
5797 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5798 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5799 {
5800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5801 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5802 }
5803
5804 memcpy( ssl->in_hdr, rec, rec_len );
5805 ssl->in_left = rec_len;
5806 ssl->next_record_offset = 0;
5807
5808 ssl_free_buffered_record( ssl );
5809
5810exit:
5811 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5812 return( 0 );
5813}
5814
5815static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5816{
5817 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5818 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005819 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005820
5821 /* Don't buffer future records outside handshakes. */
5822 if( hs == NULL )
5823 return( 0 );
5824
5825 /* Only buffer handshake records (we are only interested
5826 * in Finished messages). */
5827 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5828 return( 0 );
5829
5830 /* Don't buffer more than one future epoch record. */
5831 if( hs->buffering.future_record.data != NULL )
5832 return( 0 );
5833
Hanno Becker01315ea2018-08-21 17:22:17 +01005834 /* Don't buffer record if there's not enough buffering space remaining. */
5835 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5836 hs->buffering.total_bytes_buffered ) )
5837 {
5838 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",
5839 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5840 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005841 return( 0 );
5842 }
5843
Hanno Becker5f066e72018-08-16 14:56:31 +01005844 /* Buffer record */
5845 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5846 ssl->in_epoch + 1 ) );
5847 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5848 rec_hdr_len + ssl->in_msglen );
5849
5850 /* ssl_parse_record_header() only considers records
5851 * of the next epoch as candidates for buffering. */
5852 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005853 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005854
5855 hs->buffering.future_record.data =
5856 mbedtls_calloc( 1, hs->buffering.future_record.len );
5857 if( hs->buffering.future_record.data == NULL )
5858 {
5859 /* If we run out of RAM trying to buffer a
5860 * record from the next epoch, just ignore. */
5861 return( 0 );
5862 }
5863
Hanno Becker01315ea2018-08-21 17:22:17 +01005864 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005865
Hanno Becker01315ea2018-08-21 17:22:17 +01005866 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005867 return( 0 );
5868}
5869
5870#endif /* MBEDTLS_SSL_PROTO_DTLS */
5871
Hanno Beckere74d5562018-08-15 14:26:08 +01005872static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005873{
5874 int ret;
5875
Hanno Becker5f066e72018-08-16 14:56:31 +01005876#if defined(MBEDTLS_SSL_PROTO_DTLS)
5877 /* We might have buffered a future record; if so,
5878 * and if the epoch matches now, load it.
5879 * On success, this call will set ssl->in_left to
5880 * the length of the buffered record, so that
5881 * the calls to ssl_fetch_input() below will
5882 * essentially be no-ops. */
5883 ret = ssl_load_buffered_record( ssl );
5884 if( ret != 0 )
5885 return( ret );
5886#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005887
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005888 /* Reset in pointers to default state for TLS/DTLS records,
5889 * assuming no CID and no offset between record content and
5890 * record plaintext. */
5891 ssl_update_in_pointers( ssl );
5892
5893 /* Ensure that we have enough space available for the default form
5894 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5895 * with no space for CIDs counted in). */
5896 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5897 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005900 return( ret );
5901 }
5902
5903 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005905#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005906 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5907 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005908 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005909 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5910 {
5911 ret = ssl_buffer_future_record( ssl );
5912 if( ret != 0 )
5913 return( ret );
5914
5915 /* Fall through to handling of unexpected records */
5916 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5917 }
5918
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005919 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5920 {
5921 /* Skip unexpected record (but not whole datagram) */
5922 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005923 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005924
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5926 "(header)" ) );
5927 }
5928 else
5929 {
5930 /* Skip invalid record and the rest of the datagram */
5931 ssl->next_record_offset = 0;
5932 ssl->in_left = 0;
5933
5934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5935 "(header)" ) );
5936 }
5937
5938 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005939 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005940 }
5941#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005942 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005943 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005944
5945 /*
5946 * Read and optionally decrypt the message contents
5947 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005948 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005949 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005950 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005951 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005952 return( ret );
5953 }
5954
5955 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005956#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005958 {
Hanno Becker5903de42019-05-03 14:46:38 +01005959 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005960 if( ssl->next_record_offset < ssl->in_left )
5961 {
5962 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5963 }
5964 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005965 else
5966#endif
5967 ssl->in_left = 0;
5968
5969 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005973 {
5974 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005975 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005976 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005977 /* Except when waiting for Finished as a bad mac here
5978 * probably means something went wrong in the handshake
5979 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5980 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5981 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5982 {
5983#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5984 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5985 {
5986 mbedtls_ssl_send_alert_message( ssl,
5987 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5988 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5989 }
5990#endif
5991 return( ret );
5992 }
5993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005994#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005995 if( ssl->conf->badmac_limit != 0 &&
5996 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5999 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006000 }
6001#endif
6002
Hanno Becker4a810fb2017-05-24 16:27:30 +01006003 /* As above, invalid records cause
6004 * dismissal of the whole datagram. */
6005
6006 ssl->next_record_offset = 0;
6007 ssl->in_left = 0;
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006010 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006011 }
6012
6013 return( ret );
6014 }
6015 else
6016#endif
6017 {
6018 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006019#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6020 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 mbedtls_ssl_send_alert_message( ssl,
6023 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6024 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006025 }
6026#endif
6027 return( ret );
6028 }
6029 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006030
Simon Butcher99000142016-10-13 17:21:01 +01006031 return( 0 );
6032}
6033
6034int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6035{
6036 int ret;
6037
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006038 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006039 * Handle particular types of records
6040 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006041 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006042 {
Simon Butcher99000142016-10-13 17:21:01 +01006043 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6044 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006045 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006046 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006047 }
6048
Hanno Beckere678eaa2018-08-21 14:57:46 +01006049 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006050 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006051 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006052 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006053 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6054 ssl->in_msglen ) );
6055 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006056 }
6057
Hanno Beckere678eaa2018-08-21 14:57:46 +01006058 if( ssl->in_msg[0] != 1 )
6059 {
6060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6061 ssl->in_msg[0] ) );
6062 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6063 }
6064
6065#if defined(MBEDTLS_SSL_PROTO_DTLS)
6066 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6067 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6068 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6069 {
6070 if( ssl->handshake == NULL )
6071 {
6072 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6073 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6074 }
6075
6076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6077 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6078 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006079#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006080 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006082 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006083 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006084 if( ssl->in_msglen != 2 )
6085 {
6086 /* Note: Standard allows for more than one 2 byte alert
6087 to be packed in a single message, but Mbed TLS doesn't
6088 currently support this. */
6089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6090 ssl->in_msglen ) );
6091 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6092 }
6093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006095 ssl->in_msg[0], ssl->in_msg[1] ) );
6096
6097 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006098 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006100 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006103 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006104 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006105 }
6106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006107 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6108 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6111 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006113
6114#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6115 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6116 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6117 {
Hanno Becker90333da2017-10-10 11:27:13 +01006118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006119 /* Will be handled when trying to parse ServerHello */
6120 return( 0 );
6121 }
6122#endif
6123
6124#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6125 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6126 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6127 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6128 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6129 {
6130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6131 /* Will be handled in mbedtls_ssl_parse_certificate() */
6132 return( 0 );
6133 }
6134#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6135
6136 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006137 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 }
6139
Hanno Beckerc76c6192017-06-06 10:03:17 +01006140#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006142 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006143 /* Drop unexpected ApplicationData records,
6144 * except at the beginning of renegotiations */
6145 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6146 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6147#if defined(MBEDTLS_SSL_RENEGOTIATION)
6148 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6149 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006150#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006151 )
6152 {
6153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6154 return( MBEDTLS_ERR_SSL_NON_FATAL );
6155 }
6156
6157 if( ssl->handshake != NULL &&
6158 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6159 {
6160 ssl_handshake_wrapup_free_hs_transform( ssl );
6161 }
6162 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006163#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006164
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 return( 0 );
6166}
6167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006168int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006169{
6170 int ret;
6171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006172 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6173 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6174 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006175 {
6176 return( ret );
6177 }
6178
6179 return( 0 );
6180}
6181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006182int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006183 unsigned char level,
6184 unsigned char message )
6185{
6186 int ret;
6187
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006188 if( ssl == NULL || ssl->conf == NULL )
6189 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006192 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006194 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006195 ssl->out_msglen = 2;
6196 ssl->out_msg[0] = level;
6197 ssl->out_msg[1] = message;
6198
Hanno Becker67bc7c32018-08-06 11:33:50 +01006199 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006202 return( ret );
6203 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006205
6206 return( 0 );
6207}
6208
Hanno Beckerb9d44792019-02-08 07:19:04 +00006209#if defined(MBEDTLS_X509_CRT_PARSE_C)
6210static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6211{
6212#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6213 if( session->peer_cert != NULL )
6214 {
6215 mbedtls_x509_crt_free( session->peer_cert );
6216 mbedtls_free( session->peer_cert );
6217 session->peer_cert = NULL;
6218 }
6219#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6220 if( session->peer_cert_digest != NULL )
6221 {
6222 /* Zeroization is not necessary. */
6223 mbedtls_free( session->peer_cert_digest );
6224 session->peer_cert_digest = NULL;
6225 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6226 session->peer_cert_digest_len = 0;
6227 }
6228#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6229}
6230#endif /* MBEDTLS_X509_CRT_PARSE_C */
6231
Paul Bakker5121ce52009-01-03 21:22:43 +00006232/*
6233 * Handshake functions
6234 */
Hanno Becker21489932019-02-05 13:20:55 +00006235#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006236/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006237int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006238{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006239 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6240 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006242 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006243
Hanno Becker7177a882019-02-05 13:36:46 +00006244 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006245 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006246 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006247 ssl->state++;
6248 return( 0 );
6249 }
6250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006251 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6252 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006253}
6254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006255int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006256{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006257 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6258 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006261
Hanno Becker7177a882019-02-05 13:36:46 +00006262 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006265 ssl->state++;
6266 return( 0 );
6267 }
6268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006271}
Gilles Peskinef9828522017-05-03 12:28:43 +02006272
Hanno Becker21489932019-02-05 13:20:55 +00006273#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006274/* Some certificate support -> implement write and parse */
6275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006277{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006279 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006281 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6282 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006285
Hanno Becker7177a882019-02-05 13:36:46 +00006286 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006289 ssl->state++;
6290 return( 0 );
6291 }
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006294 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006295 {
6296 if( ssl->client_auth == 0 )
6297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006299 ssl->state++;
6300 return( 0 );
6301 }
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006304 /*
6305 * If using SSLv3 and got no cert, send an Alert message
6306 * (otherwise an empty Certificate message will be sent).
6307 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6309 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006310 {
6311 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6313 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6314 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006317 goto write_msg;
6318 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006320 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#endif /* MBEDTLS_SSL_CLI_C */
6322#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006323 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6328 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 }
6330 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006331#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006334
6335 /*
6336 * 0 . 0 handshake type
6337 * 1 . 3 handshake length
6338 * 4 . 6 length of all certs
6339 * 7 . 9 length of cert. 1
6340 * 10 . n-1 peer certificate
6341 * n . n+2 length of cert. 2
6342 * n+3 . ... upper level cert, etc.
6343 */
6344 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346
Paul Bakker29087132010-03-21 21:03:34 +00006347 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 {
6349 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006350 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006353 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 }
6356
6357 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6358 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6359 ssl->out_msg[i + 2] = (unsigned char)( n );
6360
6361 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6362 i += n; crt = crt->next;
6363 }
6364
6365 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6366 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6367 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6368
6369 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006370 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6371 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006372
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006373#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006374write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006375#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006376
6377 ssl->state++;
6378
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006379 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006380 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006381 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 return( ret );
6383 }
6384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006386
Paul Bakkered27a042013-04-18 22:46:23 +02006387 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006388}
6389
Hanno Becker84879e32019-01-31 07:44:03 +00006390#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006391
6392#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006393static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6394 unsigned char *crt_buf,
6395 size_t crt_buf_len )
6396{
6397 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6398
6399 if( peer_crt == NULL )
6400 return( -1 );
6401
6402 if( peer_crt->raw.len != crt_buf_len )
6403 return( -1 );
6404
Hanno Becker46f34d02019-02-08 14:00:04 +00006405 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006406}
Hanno Becker177475a2019-02-05 17:02:46 +00006407#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6408static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6409 unsigned char *crt_buf,
6410 size_t crt_buf_len )
6411{
6412 int ret;
6413 unsigned char const * const peer_cert_digest =
6414 ssl->session->peer_cert_digest;
6415 mbedtls_md_type_t const peer_cert_digest_type =
6416 ssl->session->peer_cert_digest_type;
6417 mbedtls_md_info_t const * const digest_info =
6418 mbedtls_md_info_from_type( peer_cert_digest_type );
6419 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6420 size_t digest_len;
6421
6422 if( peer_cert_digest == NULL || digest_info == NULL )
6423 return( -1 );
6424
6425 digest_len = mbedtls_md_get_size( digest_info );
6426 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6427 return( -1 );
6428
6429 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6430 if( ret != 0 )
6431 return( -1 );
6432
6433 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6434}
6435#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006436#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006437
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006438/*
6439 * Once the certificate message is read, parse it into a cert chain and
6440 * perform basic checks, but leave actual verification to the caller
6441 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006442static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6443 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006444{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006445 int ret;
6446#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6447 int crt_cnt=0;
6448#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006449 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006450 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006452 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006454 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006455 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6456 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006457 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006458 }
6459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006460 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6461 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006463 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006464 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6465 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006466 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006467 }
6468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006470
Paul Bakker5121ce52009-01-03 21:22:43 +00006471 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006474 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006475
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006476 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006477 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006480 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6481 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 }
6484
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006485 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6486 i += 3;
6487
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006488 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 while( i < ssl->in_hslen )
6490 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006491 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006492 if ( i + 3 > ssl->in_hslen ) {
6493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006494 mbedtls_ssl_send_alert_message( ssl,
6495 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6496 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006497 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6498 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006499 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6500 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 if( ssl->in_msg[i] != 0 )
6502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006504 mbedtls_ssl_send_alert_message( ssl,
6505 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6506 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006508 }
6509
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006510 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6512 | (unsigned int) ssl->in_msg[i + 2];
6513 i += 3;
6514
6515 if( n < 128 || i + n > ssl->in_hslen )
6516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006518 mbedtls_ssl_send_alert_message( ssl,
6519 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6520 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006521 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006522 }
6523
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006524 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006525#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6526 if( crt_cnt++ == 0 &&
6527 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6528 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006529 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006530 /* During client-side renegotiation, check that the server's
6531 * end-CRTs hasn't changed compared to the initial handshake,
6532 * mitigating the triple handshake attack. On success, reuse
6533 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6535 if( ssl_check_peer_crt_unchanged( ssl,
6536 &ssl->in_msg[i],
6537 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006538 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6540 mbedtls_ssl_send_alert_message( ssl,
6541 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6542 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6543 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006544 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006545
6546 /* Now we can safely free the original chain. */
6547 ssl_clear_peer_cert( ssl->session );
6548 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006549#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6550
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006551 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006552#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006553 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006554#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006555 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006556 * it in-place from the input buffer instead of making a copy. */
6557 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6558#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006559 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006560 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006561 case 0: /*ok*/
6562 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6563 /* Ignore certificate with an unknown algorithm: maybe a
6564 prior certificate was already trusted. */
6565 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006566
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006567 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6568 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6569 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006570
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006571 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6572 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6573 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006574
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006575 default:
6576 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6577 crt_parse_der_failed:
6578 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6579 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6580 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006581 }
6582
6583 i += n;
6584 }
6585
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006586 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006587 return( 0 );
6588}
6589
Hanno Becker4a55f632019-02-05 12:49:06 +00006590#if defined(MBEDTLS_SSL_SRV_C)
6591static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6592{
6593 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6594 return( -1 );
6595
6596#if defined(MBEDTLS_SSL_PROTO_SSL3)
6597 /*
6598 * Check if the client sent an empty certificate
6599 */
6600 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6601 {
6602 if( ssl->in_msglen == 2 &&
6603 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6604 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6605 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6606 {
6607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6608 return( 0 );
6609 }
6610
6611 return( -1 );
6612 }
6613#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6614
6615#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6616 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6617 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6618 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6619 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6620 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6621 {
6622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6623 return( 0 );
6624 }
6625
6626 return( -1 );
6627#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6628 MBEDTLS_SSL_PROTO_TLS1_2 */
6629}
6630#endif /* MBEDTLS_SSL_SRV_C */
6631
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006632/* Check if a certificate message is expected.
6633 * Return either
6634 * - SSL_CERTIFICATE_EXPECTED, or
6635 * - SSL_CERTIFICATE_SKIP
6636 * indicating whether a Certificate message is expected or not.
6637 */
6638#define SSL_CERTIFICATE_EXPECTED 0
6639#define SSL_CERTIFICATE_SKIP 1
6640static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6641 int authmode )
6642{
6643 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006644 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006645
6646 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6647 return( SSL_CERTIFICATE_SKIP );
6648
6649#if defined(MBEDTLS_SSL_SRV_C)
6650 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6651 {
6652 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6653 return( SSL_CERTIFICATE_SKIP );
6654
6655 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6656 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006657 ssl->session_negotiate->verify_result =
6658 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6659 return( SSL_CERTIFICATE_SKIP );
6660 }
6661 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006662#else
6663 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006664#endif /* MBEDTLS_SSL_SRV_C */
6665
6666 return( SSL_CERTIFICATE_EXPECTED );
6667}
6668
Hanno Becker68636192019-02-05 14:36:34 +00006669static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6670 int authmode,
6671 mbedtls_x509_crt *chain,
6672 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006673{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006674 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006675 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006676 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006677 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006678
Hanno Becker8927c832019-04-03 12:52:50 +01006679 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6680 void *p_vrfy;
6681
Hanno Becker68636192019-02-05 14:36:34 +00006682 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6683 return( 0 );
6684
Hanno Becker8927c832019-04-03 12:52:50 +01006685 if( ssl->f_vrfy != NULL )
6686 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006687 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006688 f_vrfy = ssl->f_vrfy;
6689 p_vrfy = ssl->p_vrfy;
6690 }
6691 else
6692 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006693 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006694 f_vrfy = ssl->conf->f_vrfy;
6695 p_vrfy = ssl->conf->p_vrfy;
6696 }
6697
Hanno Becker68636192019-02-05 14:36:34 +00006698 /*
6699 * Main check: verify certificate
6700 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006701#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6702 if( ssl->conf->f_ca_cb != NULL )
6703 {
6704 ((void) rs_ctx);
6705 have_ca_chain = 1;
6706
6707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006708 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006709 chain,
6710 ssl->conf->f_ca_cb,
6711 ssl->conf->p_ca_cb,
6712 ssl->conf->cert_profile,
6713 ssl->hostname,
6714 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006715 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006716 }
6717 else
6718#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6719 {
6720 mbedtls_x509_crt *ca_chain;
6721 mbedtls_x509_crl *ca_crl;
6722
6723#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6724 if( ssl->handshake->sni_ca_chain != NULL )
6725 {
6726 ca_chain = ssl->handshake->sni_ca_chain;
6727 ca_crl = ssl->handshake->sni_ca_crl;
6728 }
6729 else
6730#endif
6731 {
6732 ca_chain = ssl->conf->ca_chain;
6733 ca_crl = ssl->conf->ca_crl;
6734 }
6735
6736 if( ca_chain != NULL )
6737 have_ca_chain = 1;
6738
6739 ret = mbedtls_x509_crt_verify_restartable(
6740 chain,
6741 ca_chain, ca_crl,
6742 ssl->conf->cert_profile,
6743 ssl->hostname,
6744 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006745 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006746 }
Hanno Becker68636192019-02-05 14:36:34 +00006747
6748 if( ret != 0 )
6749 {
6750 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6751 }
6752
6753#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6754 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6755 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6756#endif
6757
6758 /*
6759 * Secondary checks: always done, but change 'ret' only if it was 0
6760 */
6761
6762#if defined(MBEDTLS_ECP_C)
6763 {
6764 const mbedtls_pk_context *pk = &chain->pk;
6765
6766 /* If certificate uses an EC key, make sure the curve is OK */
6767 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6768 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6769 {
6770 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6771
6772 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6773 if( ret == 0 )
6774 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6775 }
6776 }
6777#endif /* MBEDTLS_ECP_C */
6778
6779 if( mbedtls_ssl_check_cert_usage( chain,
6780 ciphersuite_info,
6781 ! ssl->conf->endpoint,
6782 &ssl->session_negotiate->verify_result ) != 0 )
6783 {
6784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6785 if( ret == 0 )
6786 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6787 }
6788
6789 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6790 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6791 * with details encoded in the verification flags. All other kinds
6792 * of error codes, including those from the user provided f_vrfy
6793 * functions, are treated as fatal and lead to a failure of
6794 * ssl_parse_certificate even if verification was optional. */
6795 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6796 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6797 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6798 {
6799 ret = 0;
6800 }
6801
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006802 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006803 {
6804 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6805 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6806 }
6807
6808 if( ret != 0 )
6809 {
6810 uint8_t alert;
6811
6812 /* The certificate may have been rejected for several reasons.
6813 Pick one and send the corresponding alert. Which alert to send
6814 may be a subject of debate in some cases. */
6815 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6816 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6817 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6818 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6819 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6820 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6821 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6822 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6823 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6824 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6825 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6826 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6827 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6828 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6829 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6830 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6831 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6832 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6833 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6834 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6835 else
6836 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6837 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6838 alert );
6839 }
6840
6841#if defined(MBEDTLS_DEBUG_C)
6842 if( ssl->session_negotiate->verify_result != 0 )
6843 {
6844 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6845 ssl->session_negotiate->verify_result ) );
6846 }
6847 else
6848 {
6849 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6850 }
6851#endif /* MBEDTLS_DEBUG_C */
6852
6853 return( ret );
6854}
6855
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006856#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6857static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6858 unsigned char *start, size_t len )
6859{
6860 int ret;
6861 /* Remember digest of the peer's end-CRT. */
6862 ssl->session_negotiate->peer_cert_digest =
6863 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6864 if( ssl->session_negotiate->peer_cert_digest == NULL )
6865 {
6866 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6867 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6868 mbedtls_ssl_send_alert_message( ssl,
6869 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6870 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6871
6872 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6873 }
6874
6875 ret = mbedtls_md( mbedtls_md_info_from_type(
6876 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6877 start, len,
6878 ssl->session_negotiate->peer_cert_digest );
6879
6880 ssl->session_negotiate->peer_cert_digest_type =
6881 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6882 ssl->session_negotiate->peer_cert_digest_len =
6883 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6884
6885 return( ret );
6886}
6887
6888static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6889 unsigned char *start, size_t len )
6890{
6891 unsigned char *end = start + len;
6892 int ret;
6893
6894 /* Make a copy of the peer's raw public key. */
6895 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6896 ret = mbedtls_pk_parse_subpubkey( &start, end,
6897 &ssl->handshake->peer_pubkey );
6898 if( ret != 0 )
6899 {
6900 /* We should have parsed the public key before. */
6901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6902 }
6903
6904 return( 0 );
6905}
6906#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6907
Hanno Becker68636192019-02-05 14:36:34 +00006908int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6909{
6910 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006911 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006912#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6913 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6914 ? ssl->handshake->sni_authmode
6915 : ssl->conf->authmode;
6916#else
6917 const int authmode = ssl->conf->authmode;
6918#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006919 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006920 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006921
6922 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6923
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006924 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6925 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006926 {
6927 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006928 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006929 }
6930
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006931#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6932 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006933 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006934 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006935 chain = ssl->handshake->ecrs_peer_cert;
6936 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006937 goto crt_verify;
6938 }
6939#endif
6940
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006941 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006942 {
6943 /* mbedtls_ssl_read_record may have sent an alert already. We
6944 let it decide whether to alert. */
6945 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006946 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006947 }
6948
Hanno Becker4a55f632019-02-05 12:49:06 +00006949#if defined(MBEDTLS_SSL_SRV_C)
6950 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6951 {
6952 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006953
6954 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006955 ret = 0;
6956 else
6957 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006958
Hanno Becker6bdfab22019-02-05 13:11:17 +00006959 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006960 }
6961#endif /* MBEDTLS_SSL_SRV_C */
6962
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006963 /* Clear existing peer CRT structure in case we tried to
6964 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006965 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006966
6967 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6968 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006969 {
6970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6971 sizeof( mbedtls_x509_crt ) ) );
6972 mbedtls_ssl_send_alert_message( ssl,
6973 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6974 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006975
Hanno Becker3dad3112019-02-05 17:19:52 +00006976 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6977 goto exit;
6978 }
6979 mbedtls_x509_crt_init( chain );
6980
6981 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006982 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006983 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006984
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006985#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6986 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006987 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006988
6989crt_verify:
6990 if( ssl->handshake->ecrs_enabled)
6991 rs_ctx = &ssl->handshake->ecrs_ctx;
6992#endif
6993
Hanno Becker68636192019-02-05 14:36:34 +00006994 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006995 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006996 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006997 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006998
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006999#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007000 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007001 unsigned char *crt_start, *pk_start;
7002 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007003
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007004 /* We parse the CRT chain without copying, so
7005 * these pointers point into the input buffer,
7006 * and are hence still valid after freeing the
7007 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007008
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007009 crt_start = chain->raw.p;
7010 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007011
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007012 pk_start = chain->pk_raw.p;
7013 pk_len = chain->pk_raw.len;
7014
7015 /* Free the CRT structures before computing
7016 * digest and copying the peer's public key. */
7017 mbedtls_x509_crt_free( chain );
7018 mbedtls_free( chain );
7019 chain = NULL;
7020
7021 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007022 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007023 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007024
7025 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7026 if( ret != 0 )
7027 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007028 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007029#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7030 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007031 ssl->session_negotiate->peer_cert = chain;
7032 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007033#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007036
Hanno Becker6bdfab22019-02-05 13:11:17 +00007037exit:
7038
Hanno Becker3dad3112019-02-05 17:19:52 +00007039 if( ret == 0 )
7040 ssl->state++;
7041
7042#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7043 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7044 {
7045 ssl->handshake->ecrs_peer_cert = chain;
7046 chain = NULL;
7047 }
7048#endif
7049
7050 if( chain != NULL )
7051 {
7052 mbedtls_x509_crt_free( chain );
7053 mbedtls_free( chain );
7054 }
7055
Paul Bakker5121ce52009-01-03 21:22:43 +00007056 return( ret );
7057}
Hanno Becker21489932019-02-05 13:20:55 +00007058#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007060int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007061{
7062 int ret;
7063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007067 ssl->out_msglen = 1;
7068 ssl->out_msg[0] = 1;
7069
Paul Bakker5121ce52009-01-03 21:22:43 +00007070 ssl->state++;
7071
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007072 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007073 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007074 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007075 return( ret );
7076 }
7077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007079
7080 return( 0 );
7081}
7082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007083int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007084{
7085 int ret;
7086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007088
Hanno Becker327c93b2018-08-15 13:56:18 +01007089 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092 return( ret );
7093 }
7094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007098 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7099 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007100 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 }
7102
Hanno Beckere678eaa2018-08-21 14:57:46 +01007103 /* CCS records are only accepted if they have length 1 and content '1',
7104 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007105
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007106 /*
7107 * Switch to our negotiated transform and session parameters for inbound
7108 * data.
7109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007111 ssl->transform_in = ssl->transform_negotiate;
7112 ssl->session_in = ssl->session_negotiate;
7113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007115 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007118 ssl_dtls_replay_reset( ssl );
7119#endif
7120
7121 /* Increment epoch */
7122 if( ++ssl->in_epoch == 0 )
7123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007125 /* This is highly unlikely to happen for legitimate reasons, so
7126 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007128 }
7129 }
7130 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007132 memset( ssl->in_ctr, 0, 8 );
7133
Hanno Becker79594fd2019-05-08 09:38:41 +01007134 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007136#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7137 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007142 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7143 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 }
7146 }
7147#endif
7148
Paul Bakker5121ce52009-01-03 21:22:43 +00007149 ssl->state++;
7150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007152
7153 return( 0 );
7154}
7155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007158{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007159 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7162 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7163 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007164 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007165 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007166#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7168#if defined(MBEDTLS_SHA512_C)
7169 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007170 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7171 else
7172#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173#if defined(MBEDTLS_SHA256_C)
7174 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007175 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007176 else
7177#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007181 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007182 }
Paul Bakker380da532012-04-18 16:10:25 +00007183}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007186{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7188 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007189 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7190 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007191#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7193#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007194#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007195 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007196 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7197#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007198 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007199#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007202#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007203 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007204 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007205#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007206 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007207#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007210}
7211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007213 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7216 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007217 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7218 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7221#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007222#if defined(MBEDTLS_USE_PSA_CRYPTO)
7223 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7224#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007225 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007226#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007229#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007230 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007231#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007232 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007233#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007236}
7237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7239 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7240static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007241 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007242{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007243 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7244 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007245}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007246#endif
Paul Bakker380da532012-04-18 16:10:25 +00007247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7249#if defined(MBEDTLS_SHA256_C)
7250static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007251 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007252{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007253#if defined(MBEDTLS_USE_PSA_CRYPTO)
7254 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7255#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007256 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007257#endif
Paul Bakker380da532012-04-18 16:10:25 +00007258}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007259#endif
Paul Bakker380da532012-04-18 16:10:25 +00007260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007261#if defined(MBEDTLS_SHA512_C)
7262static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007263 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007264{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007265#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007266 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007267#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007268 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007269#endif
Paul Bakker380da532012-04-18 16:10:25 +00007270}
Paul Bakker769075d2012-11-24 11:26:46 +01007271#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007273
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007274#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007275static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007277{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007278 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007279 mbedtls_md5_context md5;
7280 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007281
Paul Bakker5121ce52009-01-03 21:22:43 +00007282 unsigned char padbuf[48];
7283 unsigned char md5sum[16];
7284 unsigned char sha1sum[20];
7285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007286 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007287 if( !session )
7288 session = ssl->session;
7289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007291
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007292 mbedtls_md5_init( &md5 );
7293 mbedtls_sha1_init( &sha1 );
7294
7295 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7296 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007297
7298 /*
7299 * SSLv3:
7300 * hash =
7301 * MD5( master + pad2 +
7302 * MD5( handshake + sender + master + pad1 ) )
7303 * + SHA1( master + pad2 +
7304 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007305 */
7306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007308 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7309 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007310#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007312#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007313 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7314 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007315#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007318 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007319
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007321
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007322 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7323 mbedtls_md5_update_ret( &md5, session->master, 48 );
7324 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7325 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007327 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7328 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7329 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7330 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007331
Paul Bakker1ef83d62012-04-11 12:09:53 +00007332 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007333
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007334 mbedtls_md5_starts_ret( &md5 );
7335 mbedtls_md5_update_ret( &md5, session->master, 48 );
7336 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7337 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7338 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007339
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007340 mbedtls_sha1_starts_ret( &sha1 );
7341 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7342 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7343 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7344 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007348 mbedtls_md5_free( &md5 );
7349 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007351 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7352 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7353 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007356}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007357#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007359#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007360static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007362{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007363 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007364 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007365 mbedtls_md5_context md5;
7366 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007367 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007369 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007370 if( !session )
7371 session = ssl->session;
7372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007375 mbedtls_md5_init( &md5 );
7376 mbedtls_sha1_init( &sha1 );
7377
7378 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7379 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007380
Paul Bakker1ef83d62012-04-11 12:09:53 +00007381 /*
7382 * TLSv1:
7383 * hash = PRF( master, finished_label,
7384 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7385 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007388 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7389 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007390#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007393 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7394 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007395#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007398 ? "client finished"
7399 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007400
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007401 mbedtls_md5_finish_ret( &md5, padbuf );
7402 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007403
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007404 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007405 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007408
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007409 mbedtls_md5_free( &md5 );
7410 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007412 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007415}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7419#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007420static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007422{
7423 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007424 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007425 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007426#if defined(MBEDTLS_USE_PSA_CRYPTO)
7427 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007428 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007429 psa_status_t status;
7430#else
7431 mbedtls_sha256_context sha256;
7432#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007435 if( !session )
7436 session = ssl->session;
7437
Andrzej Kurekeb342242019-01-29 09:14:33 -05007438 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7439 ? "client finished"
7440 : "server finished";
7441
7442#if defined(MBEDTLS_USE_PSA_CRYPTO)
7443 sha256_psa = psa_hash_operation_init();
7444
7445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7446
7447 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7448 if( status != PSA_SUCCESS )
7449 {
7450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7451 return;
7452 }
7453
7454 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7455 if( status != PSA_SUCCESS )
7456 {
7457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7458 return;
7459 }
7460 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7461#else
7462
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007463 mbedtls_sha256_init( &sha256 );
7464
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007466
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007467 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007468
7469 /*
7470 * TLSv1.2:
7471 * hash = PRF( master, finished_label,
7472 * Hash( handshake ) )[0.11]
7473 */
7474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007475#if !defined(MBEDTLS_SHA256_ALT)
7476 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007477 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007478#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007479
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007480 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007481 mbedtls_sha256_free( &sha256 );
7482#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007483
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007484 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007485 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007487 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007488
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007489 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007492}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007493#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007495#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007496static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007497 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007498{
7499 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007500 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007501 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007502#if defined(MBEDTLS_USE_PSA_CRYPTO)
7503 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007504 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007505 psa_status_t status;
7506#else
7507 mbedtls_sha512_context sha512;
7508#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007511 if( !session )
7512 session = ssl->session;
7513
Andrzej Kurekeb342242019-01-29 09:14:33 -05007514 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7515 ? "client finished"
7516 : "server finished";
7517
7518#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007519 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007520
7521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7522
Andrzej Kurek972fba52019-01-30 03:29:12 -05007523 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007524 if( status != PSA_SUCCESS )
7525 {
7526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7527 return;
7528 }
7529
Andrzej Kurek972fba52019-01-30 03:29:12 -05007530 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007531 if( status != PSA_SUCCESS )
7532 {
7533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7534 return;
7535 }
7536 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7537#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007538 mbedtls_sha512_init( &sha512 );
7539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007541
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007542 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007543
7544 /*
7545 * TLSv1.2:
7546 * hash = PRF( master, finished_label,
7547 * Hash( handshake ) )[0.11]
7548 */
7549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007550#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007551 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7552 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007553#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007554
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007555 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007556 mbedtls_sha512_free( &sha512 );
7557#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007558
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007559 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007560 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007562 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007563
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007564 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007567}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568#endif /* MBEDTLS_SHA512_C */
7569#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007572{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007574
7575 /*
7576 * Free our handshake params
7577 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007578 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007580 ssl->handshake = NULL;
7581
7582 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007583 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007584 */
7585 if( ssl->transform )
7586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587 mbedtls_ssl_transform_free( ssl->transform );
7588 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007589 }
7590 ssl->transform = ssl->transform_negotiate;
7591 ssl->transform_negotiate = NULL;
7592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007594}
7595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007597{
7598 int resume = ssl->handshake->resume;
7599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_SSL_RENEGOTIATION)
7603 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007605 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007606 ssl->renego_records_seen = 0;
7607 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007608#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007609
7610 /*
7611 * Free the previous session and switch in the current one
7612 */
Paul Bakker0a597072012-09-25 21:55:46 +00007613 if( ssl->session )
7614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007616 /* RFC 7366 3.1: keep the EtM state */
7617 ssl->session_negotiate->encrypt_then_mac =
7618 ssl->session->encrypt_then_mac;
7619#endif
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 mbedtls_ssl_session_free( ssl->session );
7622 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007623 }
7624 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007625 ssl->session_negotiate = NULL;
7626
Paul Bakker0a597072012-09-25 21:55:46 +00007627 /*
7628 * Add cache entry
7629 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007630 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007631 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007632 resume == 0 )
7633 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007634 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007636 }
Paul Bakker0a597072012-09-25 21:55:46 +00007637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007639 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640 ssl->handshake->flight != NULL )
7641 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007642 /* Cancel handshake timer */
7643 ssl_set_timer( ssl, 0 );
7644
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007645 /* Keep last flight around in case we need to resend it:
7646 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007647 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007648 }
7649 else
7650#endif
7651 ssl_handshake_wrapup_free_hs_transform( ssl );
7652
Paul Bakker48916f92012-09-16 19:57:18 +00007653 ssl->state++;
7654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007656}
7657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007658int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007659{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007660 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007663
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007664 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007665
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007666 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007667
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007668 /*
7669 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7670 * may define some other value. Currently (early 2016), no defined
7671 * ciphersuite does this (and this is unlikely to change as activity has
7672 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007677 ssl->verify_data_len = hash_len;
7678 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007679#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007680
Paul Bakker5121ce52009-01-03 21:22:43 +00007681 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7683 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007684
7685 /*
7686 * In case of session resuming, invert the client and server
7687 * ChangeCipherSpec messages order.
7688 */
Paul Bakker0a597072012-09-25 21:55:46 +00007689 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007692 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007694#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007695#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007696 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007698#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007699 }
7700 else
7701 ssl->state++;
7702
Paul Bakker48916f92012-09-16 19:57:18 +00007703 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007704 * Switch to our negotiated transform and session parameters for outbound
7705 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007710 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007711 {
7712 unsigned char i;
7713
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007714 /* Remember current epoch settings for resending */
7715 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007716 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007717
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007718 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007719 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007720
7721 /* Increment epoch */
7722 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007723 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007724 break;
7725
7726 /* The loop goes to its end iff the counter is wrapping */
7727 if( i == 0 )
7728 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007729 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7730 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007731 }
7732 }
7733 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007735 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007736
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007737 ssl->transform_out = ssl->transform_negotiate;
7738 ssl->session_out = ssl->session_negotiate;
7739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7741 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007745 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7746 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007747 }
7748 }
7749#endif
7750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007752 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007754#endif
7755
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007756 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007757 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007758 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007759 return( ret );
7760 }
7761
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007762#if defined(MBEDTLS_SSL_PROTO_DTLS)
7763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7764 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7765 {
7766 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7767 return( ret );
7768 }
7769#endif
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007772
7773 return( 0 );
7774}
7775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007777#define SSL_MAX_HASH_LEN 36
7778#else
7779#define SSL_MAX_HASH_LEN 12
7780#endif
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007783{
Paul Bakker23986e52011-04-24 08:57:21 +00007784 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007785 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007786 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007789
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007790 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007791
Hanno Becker327c93b2018-08-15 13:56:18 +01007792 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007793 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007795 return( ret );
7796 }
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007799 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007801 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7802 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804 }
7805
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007806 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_PROTO_SSL3)
7808 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007809 hash_len = 36;
7810 else
7811#endif
7812 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7815 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007818 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7819 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007820 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007821 }
7822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007824 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007827 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7828 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007830 }
7831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007833 ssl->verify_data_len = hash_len;
7834 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007835#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007836
Paul Bakker0a597072012-09-25 21:55:46 +00007837 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007839#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007840 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007844 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007846#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 }
7848 else
7849 ssl->state++;
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007852 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007854#endif
7855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857
7858 return( 0 );
7859}
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007862{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7866 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7867 mbedtls_md5_init( &handshake->fin_md5 );
7868 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007869 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7870 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7873#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007874#if defined(MBEDTLS_USE_PSA_CRYPTO)
7875 handshake->fin_sha256_psa = psa_hash_operation_init();
7876 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7877#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007879 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007880#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007881#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007883#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007884 handshake->fin_sha384_psa = psa_hash_operation_init();
7885 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007886#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007888 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007889#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007890#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007892
7893 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007894
7895#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7896 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7897 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7898#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_DHM_C)
7901 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_ECDH_C)
7904 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007905#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007906#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007907 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007908#if defined(MBEDTLS_SSL_CLI_C)
7909 handshake->ecjpake_cache = NULL;
7910 handshake->ecjpake_cache_len = 0;
7911#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007912#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007913
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007914#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007915 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007916#endif
7917
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007918#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7919 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7920#endif
Hanno Becker75173122019-02-06 16:18:31 +00007921
7922#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7923 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7924 mbedtls_pk_init( &handshake->peer_pubkey );
7925#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926}
7927
Hanno Beckera18d1322018-01-03 14:27:32 +00007928void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007929{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007931
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7933 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007934
Hanno Beckerd56ed242018-01-03 15:32:51 +00007935#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007936 mbedtls_md_init( &transform->md_ctx_enc );
7937 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007938#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007939}
7940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007941void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007942{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007943 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007944}
7945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007946static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007947{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007948 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007949 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007950 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007951 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007952 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007953 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007954 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955
7956 /*
7957 * Either the pointers are now NULL or cleared properly and can be freed.
7958 * Now allocate missing structures.
7959 */
7960 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007961 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007962 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007963 }
Paul Bakker48916f92012-09-16 19:57:18 +00007964
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007965 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007966 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007967 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007968 }
Paul Bakker48916f92012-09-16 19:57:18 +00007969
Paul Bakker82788fb2014-10-20 13:59:19 +02007970 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007971 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007972 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007973 }
Paul Bakker48916f92012-09-16 19:57:18 +00007974
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007976 if( ssl->handshake == NULL ||
7977 ssl->transform_negotiate == NULL ||
7978 ssl->session_negotiate == NULL )
7979 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 mbedtls_free( ssl->handshake );
7983 mbedtls_free( ssl->transform_negotiate );
7984 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985
7986 ssl->handshake = NULL;
7987 ssl->transform_negotiate = NULL;
7988 ssl->session_negotiate = NULL;
7989
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007990 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007991 }
7992
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007993 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007994 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007995 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007996 ssl_handshake_params_init( ssl->handshake );
7997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007999 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8000 {
8001 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008002
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008003 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8004 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8005 else
8006 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008007
8008 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008009 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008010#endif
8011
Paul Bakker48916f92012-09-16 19:57:18 +00008012 return( 0 );
8013}
8014
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008015#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008016/* Dummy cookie callbacks for defaults */
8017static int ssl_cookie_write_dummy( void *ctx,
8018 unsigned char **p, unsigned char *end,
8019 const unsigned char *cli_id, size_t cli_id_len )
8020{
8021 ((void) ctx);
8022 ((void) p);
8023 ((void) end);
8024 ((void) cli_id);
8025 ((void) cli_id_len);
8026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008028}
8029
8030static int ssl_cookie_check_dummy( void *ctx,
8031 const unsigned char *cookie, size_t cookie_len,
8032 const unsigned char *cli_id, size_t cli_id_len )
8033{
8034 ((void) ctx);
8035 ((void) cookie);
8036 ((void) cookie_len);
8037 ((void) cli_id);
8038 ((void) cli_id_len);
8039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008040 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008041}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008042#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008043
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008044/* Once ssl->out_hdr as the address of the beginning of the
8045 * next outgoing record is set, deduce the other pointers.
8046 *
8047 * Note: For TLS, we save the implicit record sequence number
8048 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8049 * and the caller has to make sure there's space for this.
8050 */
8051
8052static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8053 mbedtls_ssl_transform *transform )
8054{
8055#if defined(MBEDTLS_SSL_PROTO_DTLS)
8056 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8057 {
8058 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008059#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008060 ssl->out_cid = ssl->out_ctr + 8;
8061 ssl->out_len = ssl->out_cid;
8062 if( transform != NULL )
8063 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008064#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008065 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008066#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008067 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008068 }
8069 else
8070#endif
8071 {
8072 ssl->out_ctr = ssl->out_hdr - 8;
8073 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008074#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008075 ssl->out_cid = ssl->out_len;
8076#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008077 ssl->out_iv = ssl->out_hdr + 5;
8078 }
8079
8080 /* Adjust out_msg to make space for explicit IV, if used. */
8081 if( transform != NULL &&
8082 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8083 {
8084 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8085 }
8086 else
8087 ssl->out_msg = ssl->out_iv;
8088}
8089
8090/* Once ssl->in_hdr as the address of the beginning of the
8091 * next incoming record is set, deduce the other pointers.
8092 *
8093 * Note: For TLS, we save the implicit record sequence number
8094 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8095 * and the caller has to make sure there's space for this.
8096 */
8097
Hanno Becker79594fd2019-05-08 09:38:41 +01008098static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008099{
Hanno Becker79594fd2019-05-08 09:38:41 +01008100 /* This function sets the pointers to match the case
8101 * of unprotected TLS/DTLS records, with both ssl->in_iv
8102 * and ssl->in_msg pointing to the beginning of the record
8103 * content.
8104 *
8105 * When decrypting a protected record, ssl->in_msg
8106 * will be shifted to point to the beginning of the
8107 * record plaintext.
8108 */
8109
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008110#if defined(MBEDTLS_SSL_PROTO_DTLS)
8111 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8112 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008113 /* This sets the header pointers to match records
8114 * without CID. When we receive a record containing
8115 * a CID, the fields are shifted accordingly in
8116 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008117 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008118#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008119 ssl->in_cid = ssl->in_ctr + 8;
8120 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008121#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008122 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008123#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008124 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008125 }
8126 else
8127#endif
8128 {
8129 ssl->in_ctr = ssl->in_hdr - 8;
8130 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008131#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008132 ssl->in_cid = ssl->in_len;
8133#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008134 ssl->in_iv = ssl->in_hdr + 5;
8135 }
8136
Hanno Becker79594fd2019-05-08 09:38:41 +01008137 /* This will be adjusted at record decryption time. */
8138 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008139}
8140
Paul Bakker5121ce52009-01-03 21:22:43 +00008141/*
8142 * Initialize an SSL context
8143 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008144void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8145{
8146 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8147}
8148
8149/*
8150 * Setup an SSL context
8151 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008152
8153static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8154{
8155 /* Set the incoming and outgoing record pointers. */
8156#if defined(MBEDTLS_SSL_PROTO_DTLS)
8157 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8158 {
8159 ssl->out_hdr = ssl->out_buf;
8160 ssl->in_hdr = ssl->in_buf;
8161 }
8162 else
8163#endif /* MBEDTLS_SSL_PROTO_DTLS */
8164 {
8165 ssl->out_hdr = ssl->out_buf + 8;
8166 ssl->in_hdr = ssl->in_buf + 8;
8167 }
8168
8169 /* Derive other internal pointers. */
8170 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008171 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008172}
8173
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008174int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008175 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008176{
Paul Bakker48916f92012-09-16 19:57:18 +00008177 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008178
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008179 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008180
8181 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008182 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008183 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008184
8185 /* Set to NULL in case of an error condition */
8186 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008187
Angus Grattond8213d02016-05-25 20:56:48 +10008188 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8189 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008190 {
Angus Grattond8213d02016-05-25 20:56:48 +10008191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008192 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008193 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008194 }
8195
8196 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8197 if( ssl->out_buf == NULL )
8198 {
8199 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008200 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008201 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008202 }
8203
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008204 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008205
Paul Bakker48916f92012-09-16 19:57:18 +00008206 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008207 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008208
8209 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008210
8211error:
8212 mbedtls_free( ssl->in_buf );
8213 mbedtls_free( ssl->out_buf );
8214
8215 ssl->conf = NULL;
8216
8217 ssl->in_buf = NULL;
8218 ssl->out_buf = NULL;
8219
8220 ssl->in_hdr = NULL;
8221 ssl->in_ctr = NULL;
8222 ssl->in_len = NULL;
8223 ssl->in_iv = NULL;
8224 ssl->in_msg = NULL;
8225
8226 ssl->out_hdr = NULL;
8227 ssl->out_ctr = NULL;
8228 ssl->out_len = NULL;
8229 ssl->out_iv = NULL;
8230 ssl->out_msg = NULL;
8231
k-stachowiak9f7798e2018-07-31 16:52:32 +02008232 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008233}
8234
8235/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008236 * Reset an initialized and used SSL context for re-use while retaining
8237 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008238 *
8239 * If partial is non-zero, keep data in the input buffer and client ID.
8240 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008241 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008242static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008243{
Paul Bakker48916f92012-09-16 19:57:18 +00008244 int ret;
8245
Hanno Becker7e772132018-08-10 12:38:21 +01008246#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8247 !defined(MBEDTLS_SSL_SRV_C)
8248 ((void) partial);
8249#endif
8250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008251 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008252
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008253 /* Cancel any possibly running timer */
8254 ssl_set_timer( ssl, 0 );
8255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008256#if defined(MBEDTLS_SSL_RENEGOTIATION)
8257 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008258 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008259
8260 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008261 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8262 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008264 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008265
Paul Bakker7eb013f2011-10-06 12:37:39 +00008266 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008267 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008268
8269 ssl->in_msgtype = 0;
8270 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008272 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008273 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008276 ssl_dtls_replay_reset( ssl );
8277#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008278
8279 ssl->in_hslen = 0;
8280 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008281
8282 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008283
8284 ssl->out_msgtype = 0;
8285 ssl->out_msglen = 0;
8286 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8288 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008289 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008290#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008291
Hanno Becker19859472018-08-06 09:40:20 +01008292 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8293
Paul Bakker48916f92012-09-16 19:57:18 +00008294 ssl->transform_in = NULL;
8295 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008296
Hanno Becker78640902018-08-13 16:35:15 +01008297 ssl->session_in = NULL;
8298 ssl->session_out = NULL;
8299
Angus Grattond8213d02016-05-25 20:56:48 +10008300 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008301
8302#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008303 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008304#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8305 {
8306 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008307 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008308 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008310#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8311 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8314 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8317 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008318 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008319 }
8320#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008321
Paul Bakker48916f92012-09-16 19:57:18 +00008322 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008324 mbedtls_ssl_transform_free( ssl->transform );
8325 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008326 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008327 }
Paul Bakker48916f92012-09-16 19:57:18 +00008328
Paul Bakkerc0463502013-02-14 11:19:38 +01008329 if( ssl->session )
8330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 mbedtls_ssl_session_free( ssl->session );
8332 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008333 ssl->session = NULL;
8334 }
8335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008336#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008337 ssl->alpn_chosen = NULL;
8338#endif
8339
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008340#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008341#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008342 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008343#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008344 {
8345 mbedtls_free( ssl->cli_id );
8346 ssl->cli_id = NULL;
8347 ssl->cli_id_len = 0;
8348 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008349#endif
8350
Paul Bakker48916f92012-09-16 19:57:18 +00008351 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8352 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008353
8354 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008355}
8356
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008357/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008358 * Reset an initialized and used SSL context for re-use while retaining
8359 * all application-set variables, function pointers and data.
8360 */
8361int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8362{
8363 return( ssl_session_reset_int( ssl, 0 ) );
8364}
8365
8366/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008367 * SSL set accessors
8368 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008369void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008370{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008371 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008372}
8373
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008374void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008375{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008376 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008377}
8378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008379#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008380void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008381{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008382 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008383}
8384#endif
8385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008386#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008387void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008388{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008389 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008390}
8391#endif
8392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008393#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008394
Hanno Becker1841b0a2018-08-24 11:13:57 +01008395void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8396 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008397{
8398 ssl->disable_datagram_packing = !allow_packing;
8399}
8400
8401void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8402 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008403{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008404 conf->hs_timeout_min = min;
8405 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008406}
8407#endif
8408
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008409void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008411 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008412}
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008415void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008416 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008417 void *p_vrfy )
8418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008419 conf->f_vrfy = f_vrfy;
8420 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008421}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008423
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008424void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008425 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008426 void *p_rng )
8427{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008428 conf->f_rng = f_rng;
8429 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008430}
8431
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008432void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008433 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008434 void *p_dbg )
8435{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008436 conf->f_dbg = f_dbg;
8437 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008438}
8439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008440void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008441 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008442 mbedtls_ssl_send_t *f_send,
8443 mbedtls_ssl_recv_t *f_recv,
8444 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008445{
8446 ssl->p_bio = p_bio;
8447 ssl->f_send = f_send;
8448 ssl->f_recv = f_recv;
8449 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008450}
8451
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008452#if defined(MBEDTLS_SSL_PROTO_DTLS)
8453void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8454{
8455 ssl->mtu = mtu;
8456}
8457#endif
8458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008459void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008460{
8461 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008462}
8463
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008464void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8465 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008466 mbedtls_ssl_set_timer_t *f_set_timer,
8467 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008468{
8469 ssl->p_timer = p_timer;
8470 ssl->f_set_timer = f_set_timer;
8471 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008472
8473 /* Make sure we start with no timer running */
8474 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008475}
8476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008477#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008478void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008479 void *p_cache,
8480 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8481 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008482{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008483 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008484 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008485 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008486}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008487#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008489#if defined(MBEDTLS_SSL_CLI_C)
8490int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008491{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008492 int ret;
8493
8494 if( ssl == NULL ||
8495 session == NULL ||
8496 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008497 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008498 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008499 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008500 }
8501
Hanno Becker52055ae2019-02-06 14:30:46 +00008502 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8503 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008504 return( ret );
8505
Paul Bakker0a597072012-09-25 21:55:46 +00008506 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008507
8508 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008509}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008510#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008511
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008512void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008513 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008514{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008515 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8516 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8517 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8518 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008519}
8520
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008521void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008522 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008523 int major, int minor )
8524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008526 return;
8527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008529 return;
8530
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008531 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008532}
8533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008535void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008536 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008537{
8538 conf->cert_profile = profile;
8539}
8540
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008541/* Append a new keycert entry to a (possibly empty) list */
8542static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8543 mbedtls_x509_crt *cert,
8544 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008545{
niisato8ee24222018-06-25 19:05:48 +09008546 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008547
niisato8ee24222018-06-25 19:05:48 +09008548 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8549 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008550 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008551
niisato8ee24222018-06-25 19:05:48 +09008552 new_cert->cert = cert;
8553 new_cert->key = key;
8554 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008555
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008556 /* Update head is the list was null, else add to the end */
8557 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008558 {
niisato8ee24222018-06-25 19:05:48 +09008559 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008560 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008561 else
8562 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008563 mbedtls_ssl_key_cert *cur = *head;
8564 while( cur->next != NULL )
8565 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008566 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008567 }
8568
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008569 return( 0 );
8570}
8571
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008572int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008573 mbedtls_x509_crt *own_cert,
8574 mbedtls_pk_context *pk_key )
8575{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008576 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008577}
8578
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008579void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008580 mbedtls_x509_crt *ca_chain,
8581 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008582{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008583 conf->ca_chain = ca_chain;
8584 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008585
8586#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8587 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8588 * cannot be used together. */
8589 conf->f_ca_cb = NULL;
8590 conf->p_ca_cb = NULL;
8591#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008592}
Hanno Becker5adaad92019-03-27 16:54:37 +00008593
8594#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8595void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008596 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008597 void *p_ca_cb )
8598{
8599 conf->f_ca_cb = f_ca_cb;
8600 conf->p_ca_cb = p_ca_cb;
8601
8602 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8603 * cannot be used together. */
8604 conf->ca_chain = NULL;
8605 conf->ca_crl = NULL;
8606}
8607#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008608#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008609
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008610#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8611int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8612 mbedtls_x509_crt *own_cert,
8613 mbedtls_pk_context *pk_key )
8614{
8615 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8616 own_cert, pk_key ) );
8617}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008618
8619void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8620 mbedtls_x509_crt *ca_chain,
8621 mbedtls_x509_crl *ca_crl )
8622{
8623 ssl->handshake->sni_ca_chain = ca_chain;
8624 ssl->handshake->sni_ca_crl = ca_crl;
8625}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008626
8627void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8628 int authmode )
8629{
8630 ssl->handshake->sni_authmode = authmode;
8631}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008632#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8633
Hanno Becker8927c832019-04-03 12:52:50 +01008634#if defined(MBEDTLS_X509_CRT_PARSE_C)
8635void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8636 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8637 void *p_vrfy )
8638{
8639 ssl->f_vrfy = f_vrfy;
8640 ssl->p_vrfy = p_vrfy;
8641}
8642#endif
8643
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008644#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008645/*
8646 * Set EC J-PAKE password for current handshake
8647 */
8648int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8649 const unsigned char *pw,
8650 size_t pw_len )
8651{
8652 mbedtls_ecjpake_role role;
8653
Janos Follath8eb64132016-06-03 15:40:57 +01008654 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8656
8657 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8658 role = MBEDTLS_ECJPAKE_SERVER;
8659 else
8660 role = MBEDTLS_ECJPAKE_CLIENT;
8661
8662 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8663 role,
8664 MBEDTLS_MD_SHA256,
8665 MBEDTLS_ECP_DP_SECP256R1,
8666 pw, pw_len ) );
8667}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008668#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008670#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008671
8672static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8673{
8674 /* Remove reference to existing PSK, if any. */
8675#if defined(MBEDTLS_USE_PSA_CRYPTO)
8676 if( conf->psk_opaque != 0 )
8677 {
8678 /* The maintenance of the PSK key slot is the
8679 * user's responsibility. */
8680 conf->psk_opaque = 0;
8681 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008682 /* This and the following branch should never
8683 * be taken simultaenously as we maintain the
8684 * invariant that raw and opaque PSKs are never
8685 * configured simultaneously. As a safeguard,
8686 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008687#endif /* MBEDTLS_USE_PSA_CRYPTO */
8688 if( conf->psk != NULL )
8689 {
8690 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8691
8692 mbedtls_free( conf->psk );
8693 conf->psk = NULL;
8694 conf->psk_len = 0;
8695 }
8696
8697 /* Remove reference to PSK identity, if any. */
8698 if( conf->psk_identity != NULL )
8699 {
8700 mbedtls_free( conf->psk_identity );
8701 conf->psk_identity = NULL;
8702 conf->psk_identity_len = 0;
8703 }
8704}
8705
Hanno Becker7390c712018-11-15 13:33:04 +00008706/* This function assumes that PSK identity in the SSL config is unset.
8707 * It checks that the provided identity is well-formed and attempts
8708 * to make a copy of it in the SSL config.
8709 * On failure, the PSK identity in the config remains unset. */
8710static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8711 unsigned char const *psk_identity,
8712 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008713{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008714 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008715 if( psk_identity == NULL ||
8716 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008717 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008718 {
8719 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8720 }
8721
Hanno Becker7390c712018-11-15 13:33:04 +00008722 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8723 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008724 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008725
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008726 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008727 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008728
8729 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008730}
8731
Hanno Becker7390c712018-11-15 13:33:04 +00008732int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8733 const unsigned char *psk, size_t psk_len,
8734 const unsigned char *psk_identity, size_t psk_identity_len )
8735{
8736 int ret;
8737 /* Remove opaque/raw PSK + PSK Identity */
8738 ssl_conf_remove_psk( conf );
8739
8740 /* Check and set raw PSK */
8741 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8742 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8743 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8744 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8745 conf->psk_len = psk_len;
8746 memcpy( conf->psk, psk, conf->psk_len );
8747
8748 /* Check and set PSK Identity */
8749 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8750 if( ret != 0 )
8751 ssl_conf_remove_psk( conf );
8752
8753 return( ret );
8754}
8755
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008756static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8757{
8758#if defined(MBEDTLS_USE_PSA_CRYPTO)
8759 if( ssl->handshake->psk_opaque != 0 )
8760 {
8761 ssl->handshake->psk_opaque = 0;
8762 }
8763 else
8764#endif /* MBEDTLS_USE_PSA_CRYPTO */
8765 if( ssl->handshake->psk != NULL )
8766 {
8767 mbedtls_platform_zeroize( ssl->handshake->psk,
8768 ssl->handshake->psk_len );
8769 mbedtls_free( ssl->handshake->psk );
8770 ssl->handshake->psk_len = 0;
8771 }
8772}
8773
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008774int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8775 const unsigned char *psk, size_t psk_len )
8776{
8777 if( psk == NULL || ssl->handshake == NULL )
8778 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8779
8780 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8781 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8782
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008783 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008784
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008785 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008786 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008787
8788 ssl->handshake->psk_len = psk_len;
8789 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8790
8791 return( 0 );
8792}
8793
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008794#if defined(MBEDTLS_USE_PSA_CRYPTO)
8795int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008796 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008797 const unsigned char *psk_identity,
8798 size_t psk_identity_len )
8799{
Hanno Becker7390c712018-11-15 13:33:04 +00008800 int ret;
8801 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008802 ssl_conf_remove_psk( conf );
8803
Hanno Becker7390c712018-11-15 13:33:04 +00008804 /* Check and set opaque PSK */
8805 if( psk_slot == 0 )
8806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008807 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008808
8809 /* Check and set PSK Identity */
8810 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8811 psk_identity_len );
8812 if( ret != 0 )
8813 ssl_conf_remove_psk( conf );
8814
8815 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008816}
8817
8818int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008819 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008820{
8821 if( psk_slot == 0 || ssl->handshake == NULL )
8822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8823
8824 ssl_remove_psk( ssl );
8825 ssl->handshake->psk_opaque = psk_slot;
8826 return( 0 );
8827}
8828#endif /* MBEDTLS_USE_PSA_CRYPTO */
8829
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008830void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008831 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008832 size_t),
8833 void *p_psk )
8834{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008835 conf->f_psk = f_psk;
8836 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008837}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008838#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008839
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008840#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008841
8842#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008843int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008844{
8845 int ret;
8846
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008847 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8848 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8849 {
8850 mbedtls_mpi_free( &conf->dhm_P );
8851 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008852 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008854
8855 return( 0 );
8856}
Hanno Becker470a8c42017-10-04 15:28:46 +01008857#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008858
Hanno Beckera90658f2017-10-04 15:29:08 +01008859int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8860 const unsigned char *dhm_P, size_t P_len,
8861 const unsigned char *dhm_G, size_t G_len )
8862{
8863 int ret;
8864
8865 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8866 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8867 {
8868 mbedtls_mpi_free( &conf->dhm_P );
8869 mbedtls_mpi_free( &conf->dhm_G );
8870 return( ret );
8871 }
8872
8873 return( 0 );
8874}
Paul Bakker5121ce52009-01-03 21:22:43 +00008875
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008876int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008877{
8878 int ret;
8879
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008880 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8881 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8882 {
8883 mbedtls_mpi_free( &conf->dhm_P );
8884 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008885 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008886 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008887
8888 return( 0 );
8889}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008890#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008891
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008892#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8893/*
8894 * Set the minimum length for Diffie-Hellman parameters
8895 */
8896void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8897 unsigned int bitlen )
8898{
8899 conf->dhm_min_bitlen = bitlen;
8900}
8901#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8902
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008903#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008904/*
8905 * Set allowed/preferred hashes for handshake signatures
8906 */
8907void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8908 const int *hashes )
8909{
8910 conf->sig_hashes = hashes;
8911}
Hanno Becker947194e2017-04-07 13:25:49 +01008912#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008913
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008914#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008915/*
8916 * Set the allowed elliptic curves
8917 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008918void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008919 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008920{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008921 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008922}
Hanno Becker947194e2017-04-07 13:25:49 +01008923#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008924
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008925#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008926int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008927{
Hanno Becker947194e2017-04-07 13:25:49 +01008928 /* Initialize to suppress unnecessary compiler warning */
8929 size_t hostname_len = 0;
8930
8931 /* Check if new hostname is valid before
8932 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008933 if( hostname != NULL )
8934 {
8935 hostname_len = strlen( hostname );
8936
8937 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8938 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8939 }
8940
8941 /* Now it's clear that we will overwrite the old hostname,
8942 * so we can free it safely */
8943
8944 if( ssl->hostname != NULL )
8945 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008946 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008947 mbedtls_free( ssl->hostname );
8948 }
8949
8950 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008951
Paul Bakker5121ce52009-01-03 21:22:43 +00008952 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008953 {
8954 ssl->hostname = NULL;
8955 }
8956 else
8957 {
8958 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008959 if( ssl->hostname == NULL )
8960 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008961
Hanno Becker947194e2017-04-07 13:25:49 +01008962 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008963
Hanno Becker947194e2017-04-07 13:25:49 +01008964 ssl->hostname[hostname_len] = '\0';
8965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008966
8967 return( 0 );
8968}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008969#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008970
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008971#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008972void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008973 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008974 const unsigned char *, size_t),
8975 void *p_sni )
8976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008977 conf->f_sni = f_sni;
8978 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008980#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008983int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008984{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008985 size_t cur_len, tot_len;
8986 const char **p;
8987
8988 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008989 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8990 * MUST NOT be truncated."
8991 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008992 */
8993 tot_len = 0;
8994 for( p = protos; *p != NULL; p++ )
8995 {
8996 cur_len = strlen( *p );
8997 tot_len += cur_len;
8998
8999 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009001 }
9002
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009003 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009004
9005 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009006}
9007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009009{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009010 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009011}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009014void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009015{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009016 conf->max_major_ver = major;
9017 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009018}
9019
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009020void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009021{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009022 conf->min_major_ver = major;
9023 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009024}
9025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009026#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009027void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009028{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009029 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009030}
9031#endif
9032
Janos Follath088ce432017-04-10 12:42:31 +01009033#if defined(MBEDTLS_SSL_SRV_C)
9034void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9035 char cert_req_ca_list )
9036{
9037 conf->cert_req_ca_list = cert_req_ca_list;
9038}
9039#endif
9040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009042void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009043{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009044 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009045}
9046#endif
9047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009048#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009049void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009051 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009052}
9053#endif
9054
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009055#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009056void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009057{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009058 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009059}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009060#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009062#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009063int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009064{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009066 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009069 }
9070
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009071 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009072
9073 return( 0 );
9074}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009078void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009080 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009082#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009084#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009086{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009087 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009088}
9089#endif
9090
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009091void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009092{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009093 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009094}
9095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009097void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009098{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009099 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009100}
9101
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009102void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009103{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009104 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009105}
9106
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009107void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009108 const unsigned char period[8] )
9109{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009110 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009115#if defined(MBEDTLS_SSL_CLI_C)
9116void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009117{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009118 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009119}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009120#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009121
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009122#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009123void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9124 mbedtls_ssl_ticket_write_t *f_ticket_write,
9125 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9126 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009127{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009128 conf->f_ticket_write = f_ticket_write;
9129 conf->f_ticket_parse = f_ticket_parse;
9130 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009131}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009133#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009134
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009135#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9136void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9137 mbedtls_ssl_export_keys_t *f_export_keys,
9138 void *p_export_keys )
9139{
9140 conf->f_export_keys = f_export_keys;
9141 conf->p_export_keys = p_export_keys;
9142}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009143
9144void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9145 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9146 void *p_export_keys )
9147{
9148 conf->f_export_keys_ext = f_export_keys_ext;
9149 conf->p_export_keys = p_export_keys;
9150}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009151#endif
9152
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009153#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009154void mbedtls_ssl_conf_async_private_cb(
9155 mbedtls_ssl_config *conf,
9156 mbedtls_ssl_async_sign_t *f_async_sign,
9157 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9158 mbedtls_ssl_async_resume_t *f_async_resume,
9159 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009160 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009161{
9162 conf->f_async_sign_start = f_async_sign;
9163 conf->f_async_decrypt_start = f_async_decrypt;
9164 conf->f_async_resume = f_async_resume;
9165 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009166 conf->p_async_config_data = async_config_data;
9167}
9168
Gilles Peskine8f97af72018-04-26 11:46:10 +02009169void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9170{
9171 return( conf->p_async_config_data );
9172}
9173
Gilles Peskine1febfef2018-04-30 11:54:39 +02009174void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009175{
9176 if( ssl->handshake == NULL )
9177 return( NULL );
9178 else
9179 return( ssl->handshake->user_async_ctx );
9180}
9181
Gilles Peskine1febfef2018-04-30 11:54:39 +02009182void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009183 void *ctx )
9184{
9185 if( ssl->handshake != NULL )
9186 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009187}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009188#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009189
Paul Bakker5121ce52009-01-03 21:22:43 +00009190/*
9191 * SSL get accessors
9192 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009193size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009194{
9195 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9196}
9197
Hanno Becker8b170a02017-10-10 11:51:19 +01009198int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9199{
9200 /*
9201 * Case A: We're currently holding back
9202 * a message for further processing.
9203 */
9204
9205 if( ssl->keep_current_message == 1 )
9206 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009208 return( 1 );
9209 }
9210
9211 /*
9212 * Case B: Further records are pending in the current datagram.
9213 */
9214
9215#if defined(MBEDTLS_SSL_PROTO_DTLS)
9216 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9217 ssl->in_left > ssl->next_record_offset )
9218 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009220 return( 1 );
9221 }
9222#endif /* MBEDTLS_SSL_PROTO_DTLS */
9223
9224 /*
9225 * Case C: A handshake message is being processed.
9226 */
9227
Hanno Becker8b170a02017-10-10 11:51:19 +01009228 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9229 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009231 return( 1 );
9232 }
9233
9234 /*
9235 * Case D: An application data message is being processed
9236 */
9237 if( ssl->in_offt != NULL )
9238 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009239 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009240 return( 1 );
9241 }
9242
9243 /*
9244 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009245 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009246 * we implement support for multiple alerts in single records.
9247 */
9248
9249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9250 return( 0 );
9251}
9252
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009253uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009254{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009255 if( ssl->session != NULL )
9256 return( ssl->session->verify_result );
9257
9258 if( ssl->session_negotiate != NULL )
9259 return( ssl->session_negotiate->verify_result );
9260
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009261 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009262}
9263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009264const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009265{
Paul Bakker926c8e42013-03-06 10:23:34 +01009266 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009267 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009269 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009270}
9271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009272const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009273{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009274#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009276 {
9277 switch( ssl->minor_ver )
9278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009280 return( "DTLSv1.0" );
9281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009282 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009283 return( "DTLSv1.2" );
9284
9285 default:
9286 return( "unknown (DTLS)" );
9287 }
9288 }
9289#endif
9290
Paul Bakker43ca69c2011-01-15 17:35:19 +00009291 switch( ssl->minor_ver )
9292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009294 return( "SSLv3.0" );
9295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009297 return( "TLSv1.0" );
9298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009300 return( "TLSv1.1" );
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009303 return( "TLSv1.2" );
9304
Paul Bakker43ca69c2011-01-15 17:35:19 +00009305 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009306 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009307 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009308}
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009311{
Hanno Becker3136ede2018-08-17 15:28:19 +01009312 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009314 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009315
Hanno Becker5903de42019-05-03 14:46:38 +01009316 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9317
Hanno Becker78640902018-08-13 16:35:15 +01009318 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009319 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321#if defined(MBEDTLS_ZLIB_SUPPORT)
9322 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9323 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009324#endif
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 case MBEDTLS_MODE_GCM:
9329 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009330 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009332 transform_expansion = transform->minlen;
9333 break;
9334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009335 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009336
9337 block_size = mbedtls_cipher_get_block_size(
9338 &transform->cipher_ctx_enc );
9339
Hanno Becker3136ede2018-08-17 15:28:19 +01009340 /* Expansion due to the addition of the MAC. */
9341 transform_expansion += transform->maclen;
9342
9343 /* Expansion due to the addition of CBC padding;
9344 * Theoretically up to 256 bytes, but we never use
9345 * more than the block size of the underlying cipher. */
9346 transform_expansion += block_size;
9347
9348 /* For TLS 1.1 or higher, an explicit IV is added
9349 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009350#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9351 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009352 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009353#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009354
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009355 break;
9356
9357 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009360 }
9361
Hanno Beckera0e20d02019-05-15 14:03:01 +01009362#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009363 if( transform->out_cid_len != 0 )
9364 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009365#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009366
Hanno Becker5903de42019-05-03 14:46:38 +01009367 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009368}
9369
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009370#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9371size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9372{
9373 size_t max_len;
9374
9375 /*
9376 * Assume mfl_code is correct since it was checked when set
9377 */
Angus Grattond8213d02016-05-25 20:56:48 +10009378 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009379
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009380 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009381 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009382 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009383 {
Angus Grattond8213d02016-05-25 20:56:48 +10009384 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009385 }
9386
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009387 /* During a handshake, use the value being negotiated */
9388 if( ssl->session_negotiate != NULL &&
9389 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9390 {
9391 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9392 }
9393
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009394 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009395}
9396#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9397
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009398#if defined(MBEDTLS_SSL_PROTO_DTLS)
9399static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9400{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009401 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9402 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9403 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9404 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9405 return ( 0 );
9406
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009407 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9408 return( ssl->mtu );
9409
9410 if( ssl->mtu == 0 )
9411 return( ssl->handshake->mtu );
9412
9413 return( ssl->mtu < ssl->handshake->mtu ?
9414 ssl->mtu : ssl->handshake->mtu );
9415}
9416#endif /* MBEDTLS_SSL_PROTO_DTLS */
9417
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009418int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9419{
9420 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9421
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009422#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9423 !defined(MBEDTLS_SSL_PROTO_DTLS)
9424 (void) ssl;
9425#endif
9426
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009427#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9428 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9429
9430 if( max_len > mfl )
9431 max_len = mfl;
9432#endif
9433
9434#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009435 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009436 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009437 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009438 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9439 const size_t overhead = (size_t) ret;
9440
9441 if( ret < 0 )
9442 return( ret );
9443
9444 if( mtu <= overhead )
9445 {
9446 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9447 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9448 }
9449
9450 if( max_len > mtu - overhead )
9451 max_len = mtu - overhead;
9452 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009453#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009454
Hanno Becker0defedb2018-08-10 12:35:02 +01009455#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9456 !defined(MBEDTLS_SSL_PROTO_DTLS)
9457 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009458#endif
9459
9460 return( (int) max_len );
9461}
9462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009463#if defined(MBEDTLS_X509_CRT_PARSE_C)
9464const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009465{
9466 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009467 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009468
Hanno Beckere6824572019-02-07 13:18:46 +00009469#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009470 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009471#else
9472 return( NULL );
9473#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009474}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009475#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009478int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9479 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009480{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009481 if( ssl == NULL ||
9482 dst == NULL ||
9483 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009484 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009485 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009487 }
9488
Hanno Becker52055ae2019-02-06 14:30:46 +00009489 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009490}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009492
Paul Bakker5121ce52009-01-03 21:22:43 +00009493/*
Paul Bakker1961b702013-01-25 14:49:24 +01009494 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009495 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009496int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009497{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009498 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009499
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009500 if( ssl == NULL || ssl->conf == NULL )
9501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009506#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009508 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009510#endif
9511
Paul Bakker1961b702013-01-25 14:49:24 +01009512 return( ret );
9513}
9514
9515/*
9516 * Perform the SSL handshake
9517 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009519{
9520 int ret = 0;
9521
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009522 if( ssl == NULL || ssl->conf == NULL )
9523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009530
9531 if( ret != 0 )
9532 break;
9533 }
9534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009536
9537 return( ret );
9538}
9539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540#if defined(MBEDTLS_SSL_RENEGOTIATION)
9541#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009542/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009543 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009546{
9547 int ret;
9548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009550
9551 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9553 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009554
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009555 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009556 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009557 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009558 return( ret );
9559 }
9560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009562
9563 return( 0 );
9564}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009566
9567/*
9568 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569 * - any side: calling mbedtls_ssl_renegotiate(),
9570 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9571 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009572 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009573 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009577{
9578 int ret;
9579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009581
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009582 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9583 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009584
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009585 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9586 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009587#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009588 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009590 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009591 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009592 ssl->handshake->out_msg_seq = 1;
9593 else
9594 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009595 }
9596#endif
9597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9599 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009604 return( ret );
9605 }
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009608
9609 return( 0 );
9610}
9611
9612/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009613 * Renegotiate current connection on client,
9614 * or request renegotiation on server
9615 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009617{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009619
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009620 if( ssl == NULL || ssl->conf == NULL )
9621 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009624 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009625 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009631
9632 /* Did we already try/start sending HelloRequest? */
9633 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009635
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009636 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009641 /*
9642 * On client, either start the renegotiation process or,
9643 * if already in progress, continue the handshake
9644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009646 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009649
9650 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009653 return( ret );
9654 }
9655 }
9656 else
9657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009659 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009661 return( ret );
9662 }
9663 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009665
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009666 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009667}
9668
9669/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009670 * Check record counters and renegotiate if they're above the limit.
9671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009673{
Andres AG2196c7f2016-12-15 17:01:16 +00009674 size_t ep_len = ssl_ep_len( ssl );
9675 int in_ctr_cmp;
9676 int out_ctr_cmp;
9677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9679 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009680 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009681 {
9682 return( 0 );
9683 }
9684
Andres AG2196c7f2016-12-15 17:01:16 +00009685 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9686 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009687 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009688 ssl->conf->renego_period + ep_len, 8 - ep_len );
9689
9690 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009691 {
9692 return( 0 );
9693 }
9694
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009697}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009698#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009699
9700/*
9701 * Receive application data decrypted from the SSL layer
9702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009704{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009705 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009706 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009707
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009708 if( ssl == NULL || ssl->conf == NULL )
9709 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009714 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009715 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009717 return( ret );
9718
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009719 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009721 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009722 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009723 return( ret );
9724 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009725 }
9726#endif
9727
Hanno Becker4a810fb2017-05-24 16:27:30 +01009728 /*
9729 * Check if renegotiation is necessary and/or handshake is
9730 * in process. If yes, perform/continue, and fall through
9731 * if an unexpected packet is received while the client
9732 * is waiting for the ServerHello.
9733 *
9734 * (There is no equivalent to the last condition on
9735 * the server-side as it is not treated as within
9736 * a handshake while waiting for the ClientHello
9737 * after a renegotiation request.)
9738 */
9739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009741 ret = ssl_check_ctr_renegotiate( ssl );
9742 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9743 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009746 return( ret );
9747 }
9748#endif
9749
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009752 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009753 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9754 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009757 return( ret );
9758 }
9759 }
9760
Hanno Beckere41158b2017-10-23 13:30:32 +01009761 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009762 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009763 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009764 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009765 if( ssl->f_get_timer != NULL &&
9766 ssl->f_get_timer( ssl->p_timer ) == -1 )
9767 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009768 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009769 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009770
Hanno Becker327c93b2018-08-15 13:56:18 +01009771 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009772 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009773 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9774 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009775
Hanno Becker4a810fb2017-05-24 16:27:30 +01009776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9777 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009778 }
9779
9780 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009782 {
9783 /*
9784 * OpenSSL sends empty messages to randomize the IV
9785 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009786 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009788 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009789 return( 0 );
9790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009792 return( ret );
9793 }
9794 }
9795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009797 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009798 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009799
Hanno Becker4a810fb2017-05-24 16:27:30 +01009800 /*
9801 * - For client-side, expect SERVER_HELLO_REQUEST.
9802 * - For server-side, expect CLIENT_HELLO.
9803 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9804 */
9805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009807 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009809 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009810 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009812
9813 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009815 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009816 {
9817 continue;
9818 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009819#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009821 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009822#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009823
Hanno Becker4a810fb2017-05-24 16:27:30 +01009824#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009825 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009829
9830 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009833 {
9834 continue;
9835 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009836#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009838 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009839#endif /* MBEDTLS_SSL_SRV_C */
9840
Hanno Becker21df7f92017-10-17 11:03:26 +01009841#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009842 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009843 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9844 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9845 ssl->conf->allow_legacy_renegotiation ==
9846 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9847 {
9848 /*
9849 * Accept renegotiation request
9850 */
Paul Bakker48916f92012-09-16 19:57:18 +00009851
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009852 /* DTLS clients need to know renego is server-initiated */
9853#if defined(MBEDTLS_SSL_PROTO_DTLS)
9854 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9855 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9856 {
9857 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9858 }
9859#endif
9860 ret = ssl_start_renegotiation( ssl );
9861 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9862 ret != 0 )
9863 {
9864 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9865 return( ret );
9866 }
9867 }
9868 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009869#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009870 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 /*
9872 * Refuse renegotiation
9873 */
9874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009877#if defined(MBEDTLS_SSL_PROTO_SSL3)
9878 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009879 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009880 /* SSLv3 does not have a "no_renegotiation" warning, so
9881 we send a fatal alert and abort the connection. */
9882 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9883 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9884 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009885 }
9886 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009887#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9888#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9889 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9890 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9893 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9894 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009895 {
9896 return( ret );
9897 }
Paul Bakker48916f92012-09-16 19:57:18 +00009898 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009899 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009900#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9901 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9904 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009905 }
Paul Bakker48916f92012-09-16 19:57:18 +00009906 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009907
Hanno Becker90333da2017-10-10 11:27:13 +01009908 /* At this point, we don't know whether the renegotiation has been
9909 * completed or not. The cases to consider are the following:
9910 * 1) The renegotiation is complete. In this case, no new record
9911 * has been read yet.
9912 * 2) The renegotiation is incomplete because the client received
9913 * an application data record while awaiting the ServerHello.
9914 * 3) The renegotiation is incomplete because the client received
9915 * a non-handshake, non-application data message while awaiting
9916 * the ServerHello.
9917 * In each of these case, looping will be the proper action:
9918 * - For 1), the next iteration will read a new record and check
9919 * if it's application data.
9920 * - For 2), the loop condition isn't satisfied as application data
9921 * is present, hence continue is the same as break
9922 * - For 3), the loop condition is satisfied and read_record
9923 * will re-deliver the message that was held back by the client
9924 * when expecting the ServerHello.
9925 */
9926 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009927 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009928#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009930 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009931 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009932 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009933 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009936 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009937 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009938 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009939 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009940 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009941#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009943 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9944 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009945 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009947 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009948 }
9949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009951 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9953 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009954 }
9955
9956 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009957
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009958 /* We're going to return something now, cancel timer,
9959 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009961 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009962
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009963#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009964 /* If we requested renego but received AppData, resend HelloRequest.
9965 * Do it now, after setting in_offt, to avoid taking this branch
9966 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009968 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009970 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009971 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009974 return( ret );
9975 }
9976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009978#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009979 }
9980
9981 n = ( len < ssl->in_msglen )
9982 ? len : ssl->in_msglen;
9983
9984 memcpy( buf, ssl->in_offt, n );
9985 ssl->in_msglen -= n;
9986
9987 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009988 {
9989 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009990 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009991 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009993 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009994 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009995 /* more data available */
9996 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009997 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009998
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010000
Paul Bakker23986e52011-04-24 08:57:21 +000010001 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010002}
10003
10004/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010005 * Send application data to be encrypted by the SSL layer, taking care of max
10006 * fragment length and buffer size.
10007 *
10008 * According to RFC 5246 Section 6.2.1:
10009 *
10010 * Zero-length fragments of Application data MAY be sent as they are
10011 * potentially useful as a traffic analysis countermeasure.
10012 *
10013 * Therefore, it is possible that the input message length is 0 and the
10014 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010015 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010016static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010017 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010018{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010019 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10020 const size_t max_len = (size_t) ret;
10021
10022 if( ret < 0 )
10023 {
10024 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10025 return( ret );
10026 }
10027
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010028 if( len > max_len )
10029 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010034 "maximum fragment length: %d > %d",
10035 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010036 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010037 }
10038 else
10039#endif
10040 len = max_len;
10041 }
Paul Bakker887bd502011-06-08 13:10:54 +000010042
Paul Bakker5121ce52009-01-03 21:22:43 +000010043 if( ssl->out_left != 0 )
10044 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010045 /*
10046 * The user has previously tried to send the data and
10047 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10048 * written. In this case, we expect the high-level write function
10049 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010054 return( ret );
10055 }
10056 }
Paul Bakker887bd502011-06-08 13:10:54 +000010057 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010058 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010059 /*
10060 * The user is trying to send a message the first time, so we need to
10061 * copy the data into the internal buffers and setup the data structure
10062 * to keep track of partial writes
10063 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010064 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010066 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010067
Hanno Becker67bc7c32018-08-06 11:33:50 +010010068 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010071 return( ret );
10072 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010073 }
10074
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010075 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010076}
10077
10078/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010079 * Write application data, doing 1/n-1 splitting if necessary.
10080 *
10081 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010082 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010083 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010086static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010087 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010088{
10089 int ret;
10090
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010091 if( ssl->conf->cbc_record_splitting ==
10092 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010093 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10095 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10096 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010097 {
10098 return( ssl_write_real( ssl, buf, len ) );
10099 }
10100
10101 if( ssl->split_done == 0 )
10102 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010103 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010104 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010105 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010106 }
10107
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010108 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10109 return( ret );
10110 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010111
10112 return( ret + 1 );
10113}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010115
10116/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010117 * Write application data (public-facing wrapper)
10118 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010119int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010120{
10121 int ret;
10122
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010124
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010125 if( ssl == NULL || ssl->conf == NULL )
10126 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10127
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010128#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010129 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10130 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010131 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010132 return( ret );
10133 }
10134#endif
10135
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010136 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010137 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010138 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010139 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010141 return( ret );
10142 }
10143 }
10144
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010145#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010146 ret = ssl_write_split( ssl, buf, len );
10147#else
10148 ret = ssl_write_real( ssl, buf, len );
10149#endif
10150
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010152
10153 return( ret );
10154}
10155
10156/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010157 * Notify the peer that the connection is being closed
10158 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010159int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010160{
10161 int ret;
10162
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010163 if( ssl == NULL || ssl->conf == NULL )
10164 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010167
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010168 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010169 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010171 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010173 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10174 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10175 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010178 return( ret );
10179 }
10180 }
10181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010183
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010184 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010185}
10186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010188{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010189 if( transform == NULL )
10190 return;
10191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010193 deflateEnd( &transform->ctx_deflate );
10194 inflateEnd( &transform->ctx_inflate );
10195#endif
10196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10198 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010199
Hanno Beckerd56ed242018-01-03 15:32:51 +000010200#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 mbedtls_md_free( &transform->md_ctx_enc );
10202 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010203#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010204
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010205 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010206}
10207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208#if defined(MBEDTLS_X509_CRT_PARSE_C)
10209static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010212
10213 while( cur != NULL )
10214 {
10215 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010217 cur = next;
10218 }
10219}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010221
Hanno Becker0271f962018-08-16 13:23:47 +010010222#if defined(MBEDTLS_SSL_PROTO_DTLS)
10223
10224static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10225{
10226 unsigned offset;
10227 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10228
10229 if( hs == NULL )
10230 return;
10231
Hanno Becker283f5ef2018-08-24 09:34:47 +010010232 ssl_free_buffered_record( ssl );
10233
Hanno Becker0271f962018-08-16 13:23:47 +010010234 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010235 ssl_buffering_free_slot( ssl, offset );
10236}
10237
10238static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10239 uint8_t slot )
10240{
10241 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10242 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010243
10244 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10245 return;
10246
Hanno Beckere605b192018-08-21 15:59:07 +010010247 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010248 {
Hanno Beckere605b192018-08-21 15:59:07 +010010249 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010250 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010251 mbedtls_free( hs_buf->data );
10252 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010253 }
10254}
10255
10256#endif /* MBEDTLS_SSL_PROTO_DTLS */
10257
Gilles Peskine9b562d52018-04-25 20:32:43 +020010258void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010259{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010260 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10261
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010262 if( handshake == NULL )
10263 return;
10264
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010265#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10266 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10267 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010268 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010269 handshake->async_in_progress = 0;
10270 }
10271#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10272
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010273#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10274 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10275 mbedtls_md5_free( &handshake->fin_md5 );
10276 mbedtls_sha1_free( &handshake->fin_sha1 );
10277#endif
10278#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10279#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010280#if defined(MBEDTLS_USE_PSA_CRYPTO)
10281 psa_hash_abort( &handshake->fin_sha256_psa );
10282#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010283 mbedtls_sha256_free( &handshake->fin_sha256 );
10284#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010285#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010286#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010287#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010288 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010289#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010290 mbedtls_sha512_free( &handshake->fin_sha512 );
10291#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010292#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010293#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010295#if defined(MBEDTLS_DHM_C)
10296 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010297#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010298#if defined(MBEDTLS_ECDH_C)
10299 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010300#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010301#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010302 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010303#if defined(MBEDTLS_SSL_CLI_C)
10304 mbedtls_free( handshake->ecjpake_cache );
10305 handshake->ecjpake_cache = NULL;
10306 handshake->ecjpake_cache_len = 0;
10307#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010308#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010309
Janos Follath4ae5c292016-02-10 11:27:43 +000010310#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10311 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010312 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010314#endif
10315
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010316#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10317 if( handshake->psk != NULL )
10318 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010319 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010320 mbedtls_free( handshake->psk );
10321 }
10322#endif
10323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10325 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010326 /*
10327 * Free only the linked list wrapper, not the keys themselves
10328 * since the belong to the SNI callback
10329 */
10330 if( handshake->sni_key_cert != NULL )
10331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010332 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010333
10334 while( cur != NULL )
10335 {
10336 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010338 cur = next;
10339 }
10340 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010342
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010343#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010344 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010345 if( handshake->ecrs_peer_cert != NULL )
10346 {
10347 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10348 mbedtls_free( handshake->ecrs_peer_cert );
10349 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010350#endif
10351
Hanno Becker75173122019-02-06 16:18:31 +000010352#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10353 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10354 mbedtls_pk_free( &handshake->peer_pubkey );
10355#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010357#if defined(MBEDTLS_SSL_PROTO_DTLS)
10358 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010359 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010360 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010361#endif
10362
Hanno Becker4a63ed42019-01-08 11:39:35 +000010363#if defined(MBEDTLS_ECDH_C) && \
10364 defined(MBEDTLS_USE_PSA_CRYPTO)
10365 psa_destroy_key( handshake->ecdh_psa_privkey );
10366#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10367
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010368 mbedtls_platform_zeroize( handshake,
10369 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010370}
10371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010373{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010374 if( session == NULL )
10375 return;
10376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010377#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010378 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010379#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010380
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010381#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010382 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010383#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010384
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010385 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010386}
10387
Paul Bakker5121ce52009-01-03 21:22:43 +000010388/*
10389 * Free an SSL context
10390 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010391void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010392{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010393 if( ssl == NULL )
10394 return;
10395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010397
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010398 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010399 {
Angus Grattond8213d02016-05-25 20:56:48 +100010400 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010402 }
10403
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010404 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010405 {
Angus Grattond8213d02016-05-25 20:56:48 +100010406 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010408 }
10409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010411 if( ssl->compress_buf != NULL )
10412 {
Angus Grattond8213d02016-05-25 20:56:48 +100010413 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010414 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010415 }
10416#endif
10417
Paul Bakker48916f92012-09-16 19:57:18 +000010418 if( ssl->transform )
10419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 mbedtls_ssl_transform_free( ssl->transform );
10421 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010422 }
10423
10424 if( ssl->handshake )
10425 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010426 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10428 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 mbedtls_free( ssl->handshake );
10431 mbedtls_free( ssl->transform_negotiate );
10432 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010433 }
10434
Paul Bakkerc0463502013-02-14 11:19:38 +010010435 if( ssl->session )
10436 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 mbedtls_ssl_session_free( ssl->session );
10438 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010439 }
10440
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010441#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010442 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010443 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010444 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010446 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010447#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010449#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10450 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10453 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010454 }
10455#endif
10456
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010457#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010459#endif
10460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010462
Paul Bakker86f04f42013-02-14 11:20:09 +010010463 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010464 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010465}
10466
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010467/*
10468 * Initialze mbedtls_ssl_config
10469 */
10470void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10471{
10472 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10473}
10474
Simon Butcherc97b6972015-12-27 23:48:17 +000010475#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010476static int ssl_preset_default_hashes[] = {
10477#if defined(MBEDTLS_SHA512_C)
10478 MBEDTLS_MD_SHA512,
10479 MBEDTLS_MD_SHA384,
10480#endif
10481#if defined(MBEDTLS_SHA256_C)
10482 MBEDTLS_MD_SHA256,
10483 MBEDTLS_MD_SHA224,
10484#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010485#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010486 MBEDTLS_MD_SHA1,
10487#endif
10488 MBEDTLS_MD_NONE
10489};
Simon Butcherc97b6972015-12-27 23:48:17 +000010490#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010491
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010492static int ssl_preset_suiteb_ciphersuites[] = {
10493 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10494 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10495 0
10496};
10497
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010498#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010499static int ssl_preset_suiteb_hashes[] = {
10500 MBEDTLS_MD_SHA256,
10501 MBEDTLS_MD_SHA384,
10502 MBEDTLS_MD_NONE
10503};
10504#endif
10505
10506#if defined(MBEDTLS_ECP_C)
10507static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010508#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010509 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010510#endif
10511#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010512 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010513#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010514 MBEDTLS_ECP_DP_NONE
10515};
10516#endif
10517
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010518/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010519 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010520 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010521int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010522 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010523{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010524#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010525 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010526#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010527
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010528 /* Use the functions here so that they are covered in tests,
10529 * but otherwise access member directly for efficiency */
10530 mbedtls_ssl_conf_endpoint( conf, endpoint );
10531 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010532
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010533 /*
10534 * Things that are common to all presets
10535 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010536#if defined(MBEDTLS_SSL_CLI_C)
10537 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10538 {
10539 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10540#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10541 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10542#endif
10543 }
10544#endif
10545
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010546#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010548#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549
10550#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10551 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10552#endif
10553
10554#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10555 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10556#endif
10557
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010558#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10559 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10560#endif
10561
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010562#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010563 conf->f_cookie_write = ssl_cookie_write_dummy;
10564 conf->f_cookie_check = ssl_cookie_check_dummy;
10565#endif
10566
10567#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10568 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10569#endif
10570
Janos Follath088ce432017-04-10 12:42:31 +010010571#if defined(MBEDTLS_SSL_SRV_C)
10572 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10573#endif
10574
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010575#if defined(MBEDTLS_SSL_PROTO_DTLS)
10576 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10577 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10578#endif
10579
10580#if defined(MBEDTLS_SSL_RENEGOTIATION)
10581 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010582 memset( conf->renego_period, 0x00, 2 );
10583 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010584#endif
10585
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010586#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10587 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10588 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010589 const unsigned char dhm_p[] =
10590 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10591 const unsigned char dhm_g[] =
10592 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10593
Hanno Beckera90658f2017-10-04 15:29:08 +010010594 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10595 dhm_p, sizeof( dhm_p ),
10596 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010597 {
10598 return( ret );
10599 }
10600 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010601#endif
10602
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010603 /*
10604 * Preset-specific defaults
10605 */
10606 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010607 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010608 /*
10609 * NSA Suite B
10610 */
10611 case MBEDTLS_SSL_PRESET_SUITEB:
10612 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10613 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10614 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10615 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10616
10617 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10618 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10619 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10620 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10621 ssl_preset_suiteb_ciphersuites;
10622
10623#if defined(MBEDTLS_X509_CRT_PARSE_C)
10624 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010625#endif
10626
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010627#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010628 conf->sig_hashes = ssl_preset_suiteb_hashes;
10629#endif
10630
10631#if defined(MBEDTLS_ECP_C)
10632 conf->curve_list = ssl_preset_suiteb_curves;
10633#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010634 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010635
10636 /*
10637 * Default
10638 */
10639 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010640 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10641 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10642 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10643 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10644 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10645 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10646 MBEDTLS_SSL_MIN_MINOR_VERSION :
10647 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010648 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10649 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10650
10651#if defined(MBEDTLS_SSL_PROTO_DTLS)
10652 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10653 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10654#endif
10655
10656 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10657 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10658 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10659 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10660 mbedtls_ssl_list_ciphersuites();
10661
10662#if defined(MBEDTLS_X509_CRT_PARSE_C)
10663 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10664#endif
10665
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010666#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010667 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010668#endif
10669
10670#if defined(MBEDTLS_ECP_C)
10671 conf->curve_list = mbedtls_ecp_grp_id_list();
10672#endif
10673
10674#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10675 conf->dhm_min_bitlen = 1024;
10676#endif
10677 }
10678
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010679 return( 0 );
10680}
10681
10682/*
10683 * Free mbedtls_ssl_config
10684 */
10685void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10686{
10687#if defined(MBEDTLS_DHM_C)
10688 mbedtls_mpi_free( &conf->dhm_P );
10689 mbedtls_mpi_free( &conf->dhm_G );
10690#endif
10691
10692#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10693 if( conf->psk != NULL )
10694 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010695 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010696 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010697 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010698 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010699 }
10700
10701 if( conf->psk_identity != NULL )
10702 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010703 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010704 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010705 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010706 conf->psk_identity_len = 0;
10707 }
10708#endif
10709
10710#if defined(MBEDTLS_X509_CRT_PARSE_C)
10711 ssl_key_cert_free( conf->key_cert );
10712#endif
10713
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010714 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010715}
10716
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010717#if defined(MBEDTLS_PK_C) && \
10718 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010719/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010722unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010723{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010724#if defined(MBEDTLS_RSA_C)
10725 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10726 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010727#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010728#if defined(MBEDTLS_ECDSA_C)
10729 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10730 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010731#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010732 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010733}
10734
Hanno Becker7e5437a2017-04-28 17:15:26 +010010735unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10736{
10737 switch( type ) {
10738 case MBEDTLS_PK_RSA:
10739 return( MBEDTLS_SSL_SIG_RSA );
10740 case MBEDTLS_PK_ECDSA:
10741 case MBEDTLS_PK_ECKEY:
10742 return( MBEDTLS_SSL_SIG_ECDSA );
10743 default:
10744 return( MBEDTLS_SSL_SIG_ANON );
10745 }
10746}
10747
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010749{
10750 switch( sig )
10751 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752#if defined(MBEDTLS_RSA_C)
10753 case MBEDTLS_SSL_SIG_RSA:
10754 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756#if defined(MBEDTLS_ECDSA_C)
10757 case MBEDTLS_SSL_SIG_ECDSA:
10758 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010759#endif
10760 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010762 }
10763}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010764#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010765
Hanno Becker7e5437a2017-04-28 17:15:26 +010010766#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10767 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10768
10769/* Find an entry in a signature-hash set matching a given hash algorithm. */
10770mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10771 mbedtls_pk_type_t sig_alg )
10772{
10773 switch( sig_alg )
10774 {
10775 case MBEDTLS_PK_RSA:
10776 return( set->rsa );
10777 case MBEDTLS_PK_ECDSA:
10778 return( set->ecdsa );
10779 default:
10780 return( MBEDTLS_MD_NONE );
10781 }
10782}
10783
10784/* Add a signature-hash-pair to a signature-hash set */
10785void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10786 mbedtls_pk_type_t sig_alg,
10787 mbedtls_md_type_t md_alg )
10788{
10789 switch( sig_alg )
10790 {
10791 case MBEDTLS_PK_RSA:
10792 if( set->rsa == MBEDTLS_MD_NONE )
10793 set->rsa = md_alg;
10794 break;
10795
10796 case MBEDTLS_PK_ECDSA:
10797 if( set->ecdsa == MBEDTLS_MD_NONE )
10798 set->ecdsa = md_alg;
10799 break;
10800
10801 default:
10802 break;
10803 }
10804}
10805
10806/* Allow exactly one hash algorithm for each signature. */
10807void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10808 mbedtls_md_type_t md_alg )
10809{
10810 set->rsa = md_alg;
10811 set->ecdsa = md_alg;
10812}
10813
10814#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10815 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10816
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010817/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010818 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010820mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010821{
10822 switch( hash )
10823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010824#if defined(MBEDTLS_MD5_C)
10825 case MBEDTLS_SSL_HASH_MD5:
10826 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010827#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010828#if defined(MBEDTLS_SHA1_C)
10829 case MBEDTLS_SSL_HASH_SHA1:
10830 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010831#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010832#if defined(MBEDTLS_SHA256_C)
10833 case MBEDTLS_SSL_HASH_SHA224:
10834 return( MBEDTLS_MD_SHA224 );
10835 case MBEDTLS_SSL_HASH_SHA256:
10836 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010837#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010838#if defined(MBEDTLS_SHA512_C)
10839 case MBEDTLS_SSL_HASH_SHA384:
10840 return( MBEDTLS_MD_SHA384 );
10841 case MBEDTLS_SSL_HASH_SHA512:
10842 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010843#endif
10844 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010846 }
10847}
10848
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010849/*
10850 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10851 */
10852unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10853{
10854 switch( md )
10855 {
10856#if defined(MBEDTLS_MD5_C)
10857 case MBEDTLS_MD_MD5:
10858 return( MBEDTLS_SSL_HASH_MD5 );
10859#endif
10860#if defined(MBEDTLS_SHA1_C)
10861 case MBEDTLS_MD_SHA1:
10862 return( MBEDTLS_SSL_HASH_SHA1 );
10863#endif
10864#if defined(MBEDTLS_SHA256_C)
10865 case MBEDTLS_MD_SHA224:
10866 return( MBEDTLS_SSL_HASH_SHA224 );
10867 case MBEDTLS_MD_SHA256:
10868 return( MBEDTLS_SSL_HASH_SHA256 );
10869#endif
10870#if defined(MBEDTLS_SHA512_C)
10871 case MBEDTLS_MD_SHA384:
10872 return( MBEDTLS_SSL_HASH_SHA384 );
10873 case MBEDTLS_MD_SHA512:
10874 return( MBEDTLS_SSL_HASH_SHA512 );
10875#endif
10876 default:
10877 return( MBEDTLS_SSL_HASH_NONE );
10878 }
10879}
10880
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010881#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010882/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010883 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010884 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010885 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010886int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010887{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010888 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010889
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010890 if( ssl->conf->curve_list == NULL )
10891 return( -1 );
10892
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010893 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010894 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010895 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010896
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010897 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010898}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010899#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010900
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010901#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010902/*
10903 * Check if a hash proposed by the peer is in our list.
10904 * Return 0 if we're willing to use it, -1 otherwise.
10905 */
10906int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10907 mbedtls_md_type_t md )
10908{
10909 const int *cur;
10910
10911 if( ssl->conf->sig_hashes == NULL )
10912 return( -1 );
10913
10914 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10915 if( *cur == (int) md )
10916 return( 0 );
10917
10918 return( -1 );
10919}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010920#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922#if defined(MBEDTLS_X509_CRT_PARSE_C)
10923int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10924 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010925 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010926 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010927{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010928 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010929#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010930 int usage = 0;
10931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010932#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010933 const char *ext_oid;
10934 size_t ext_len;
10935#endif
10936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010937#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10938 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010939 ((void) cert);
10940 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010941 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010942#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010943
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10945 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010946 {
10947 /* Server part of the key exchange */
10948 switch( ciphersuite->key_exchange )
10949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010950 case MBEDTLS_KEY_EXCHANGE_RSA:
10951 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010952 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010953 break;
10954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10956 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10957 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10958 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010959 break;
10960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10962 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010963 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010964 break;
10965
10966 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010967 case MBEDTLS_KEY_EXCHANGE_NONE:
10968 case MBEDTLS_KEY_EXCHANGE_PSK:
10969 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10970 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010971 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010972 usage = 0;
10973 }
10974 }
10975 else
10976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010977 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10978 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010979 }
10980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010981 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010982 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010983 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010984 ret = -1;
10985 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010986#else
10987 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010988#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10991 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010992 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10994 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010995 }
10996 else
10997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010998 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10999 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011000 }
11001
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011003 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011004 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011005 ret = -1;
11006 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011008
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011009 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011011#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011012
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011013/*
11014 * Convert version numbers to/from wire format
11015 * and, for DTLS, to/from TLS equivalent.
11016 *
11017 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011018 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011019 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11020 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11021 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011023 unsigned char ver[2] )
11024{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025#if defined(MBEDTLS_SSL_PROTO_DTLS)
11026 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011029 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11030
11031 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11032 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11033 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011034 else
11035#else
11036 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011037#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011038 {
11039 ver[0] = (unsigned char) major;
11040 ver[1] = (unsigned char) minor;
11041 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011042}
11043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011044void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011045 const unsigned char ver[2] )
11046{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047#if defined(MBEDTLS_SSL_PROTO_DTLS)
11048 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011049 {
11050 *major = 255 - ver[0] + 2;
11051 *minor = 255 - ver[1] + 1;
11052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11055 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011056 else
11057#else
11058 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011059#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011060 {
11061 *major = ver[0];
11062 *minor = ver[1];
11063 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011064}
11065
Simon Butcher99000142016-10-13 17:21:01 +010011066int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11067{
11068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11069 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11070 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11071
11072 switch( md )
11073 {
11074#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11075#if defined(MBEDTLS_MD5_C)
11076 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011077 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011078#endif
11079#if defined(MBEDTLS_SHA1_C)
11080 case MBEDTLS_SSL_HASH_SHA1:
11081 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11082 break;
11083#endif
11084#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11085#if defined(MBEDTLS_SHA512_C)
11086 case MBEDTLS_SSL_HASH_SHA384:
11087 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11088 break;
11089#endif
11090#if defined(MBEDTLS_SHA256_C)
11091 case MBEDTLS_SSL_HASH_SHA256:
11092 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11093 break;
11094#endif
11095 default:
11096 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11097 }
11098
11099 return 0;
11100#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11101 (void) ssl;
11102 (void) md;
11103
11104 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11105#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11106}
11107
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011108#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11109 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11110int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11111 unsigned char *output,
11112 unsigned char *data, size_t data_len )
11113{
11114 int ret = 0;
11115 mbedtls_md5_context mbedtls_md5;
11116 mbedtls_sha1_context mbedtls_sha1;
11117
11118 mbedtls_md5_init( &mbedtls_md5 );
11119 mbedtls_sha1_init( &mbedtls_sha1 );
11120
11121 /*
11122 * digitally-signed struct {
11123 * opaque md5_hash[16];
11124 * opaque sha_hash[20];
11125 * };
11126 *
11127 * md5_hash
11128 * MD5(ClientHello.random + ServerHello.random
11129 * + ServerParams);
11130 * sha_hash
11131 * SHA(ClientHello.random + ServerHello.random
11132 * + ServerParams);
11133 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011134 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011136 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011137 goto exit;
11138 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011139 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011140 ssl->handshake->randbytes, 64 ) ) != 0 )
11141 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011142 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011143 goto exit;
11144 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011145 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011146 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011147 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011148 goto exit;
11149 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011150 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011151 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011152 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011153 goto exit;
11154 }
11155
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011156 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011157 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011159 goto exit;
11160 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011161 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011162 ssl->handshake->randbytes, 64 ) ) != 0 )
11163 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011165 goto exit;
11166 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011167 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 data_len ) ) != 0 )
11169 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 goto exit;
11172 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011174 output + 16 ) ) != 0 )
11175 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 goto exit;
11178 }
11179
11180exit:
11181 mbedtls_md5_free( &mbedtls_md5 );
11182 mbedtls_sha1_free( &mbedtls_sha1 );
11183
11184 if( ret != 0 )
11185 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11186 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11187
11188 return( ret );
11189
11190}
11191#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11192 MBEDTLS_SSL_PROTO_TLS1_1 */
11193
11194#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11195 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011196
11197#if defined(MBEDTLS_USE_PSA_CRYPTO)
11198int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11199 unsigned char *hash, size_t *hashlen,
11200 unsigned char *data, size_t data_len,
11201 mbedtls_md_type_t md_alg )
11202{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011203 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011204 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011205 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11206
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011208
11209 if( ( status = psa_hash_setup( &hash_operation,
11210 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011211 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011212 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011213 goto exit;
11214 }
11215
Andrzej Kurek814feff2019-01-14 04:35:19 -050011216 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11217 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011218 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011219 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011220 goto exit;
11221 }
11222
Andrzej Kurek814feff2019-01-14 04:35:19 -050011223 if( ( status = psa_hash_update( &hash_operation,
11224 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011225 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011226 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011227 goto exit;
11228 }
11229
Andrzej Kurek814feff2019-01-14 04:35:19 -050011230 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11231 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011232 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011233 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011234 goto exit;
11235 }
11236
11237exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011238 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011239 {
11240 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11241 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011242 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011243 {
11244 case PSA_ERROR_NOT_SUPPORTED:
11245 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011246 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 case PSA_ERROR_BUFFER_TOO_SMALL:
11248 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11249 case PSA_ERROR_INSUFFICIENT_MEMORY:
11250 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11251 default:
11252 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11253 }
11254 }
11255 return( 0 );
11256}
11257
11258#else
11259
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011260int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011261 unsigned char *hash, size_t *hashlen,
11262 unsigned char *data, size_t data_len,
11263 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011264{
11265 int ret = 0;
11266 mbedtls_md_context_t ctx;
11267 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011268 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011269
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011271
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011272 mbedtls_md_init( &ctx );
11273
11274 /*
11275 * digitally-signed struct {
11276 * opaque client_random[32];
11277 * opaque server_random[32];
11278 * ServerDHParams params;
11279 * };
11280 */
11281 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11282 {
11283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11284 goto exit;
11285 }
11286 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11287 {
11288 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11289 goto exit;
11290 }
11291 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11292 {
11293 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11294 goto exit;
11295 }
11296 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11297 {
11298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11299 goto exit;
11300 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011301 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011302 {
11303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11304 goto exit;
11305 }
11306
11307exit:
11308 mbedtls_md_free( &ctx );
11309
11310 if( ret != 0 )
11311 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11312 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11313
11314 return( ret );
11315}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011316#endif /* MBEDTLS_USE_PSA_CRYPTO */
11317
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011318#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11319 MBEDTLS_SSL_PROTO_TLS1_2 */
11320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011321#endif /* MBEDTLS_SSL_TLS_C */