blob: 9682bbbb1f8758a5b7f4add7a3a8f4d046614ccf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
Hanno Beckercfe45792019-07-03 16:13:00 +0100115#if defined(MBEDTLS_SSL_RECORD_CHECKING)
116int mbedtls_ssl_check_record( mbedtls_ssl_context const *ssl,
117 unsigned char *buf,
118 size_t buflen )
119{
120 ((void) ssl);
121 ((void) buf);
122 ((void) buflen);
123 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
124}
125#endif /* MBEDTLS_SSL_RECORD_CHECKING */
126
Hanno Becker67bc7c32018-08-06 11:33:50 +0100127#define SSL_DONT_FORCE_FLUSH 0
128#define SSL_FORCE_FLUSH 1
129
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100131
Hanno Beckera0e20d02019-05-15 14:03:01 +0100132#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100133/* Top-level Connection ID API */
134
Hanno Becker8367ccc2019-05-14 11:30:10 +0100135int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
136 size_t len,
137 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100138{
139 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
141
Hanno Becker611ac772019-05-14 11:45:26 +0100142 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
143 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
144 {
145 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
146 }
147
148 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100149 conf->cid_len = len;
150 return( 0 );
151}
152
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
154 int enable,
155 unsigned char const *own_cid,
156 size_t own_cid_len )
157{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100158 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160
Hanno Beckerca092242019-04-25 16:01:49 +0100161 ssl->negotiate_cid = enable;
162 if( enable == MBEDTLS_SSL_CID_DISABLED )
163 {
164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
165 return( 0 );
166 }
167 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100168 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100169
Hanno Beckerad4a1372019-05-03 13:06:44 +0100170 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100171 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100172 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
173 (unsigned) own_cid_len,
174 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
176 }
177
178 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100179 /* Truncation is not an issue here because
180 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
181 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100182
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100183 return( 0 );
184}
185
186int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
187 int *enabled,
188 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
189 size_t *peer_cid_len )
190{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100191 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
Hanno Becker76a79ab2019-05-03 14:38:32 +0100193 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
194 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
195 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100196 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100197 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100198
Hanno Beckerc5f24222019-05-03 12:54:52 +0100199 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
200 * were used, but client and server requested the empty CID.
201 * This is indistinguishable from not using the CID extension
202 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100203 if( ssl->transform_in->in_cid_len == 0 &&
204 ssl->transform_in->out_cid_len == 0 )
205 {
206 return( 0 );
207 }
208
Hanno Becker615ef172019-05-22 16:50:35 +0100209 if( peer_cid_len != NULL )
210 {
211 *peer_cid_len = ssl->transform_in->out_cid_len;
212 if( peer_cid != NULL )
213 {
214 memcpy( peer_cid, ssl->transform_in->out_cid,
215 ssl->transform_in->out_cid_len );
216 }
217 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100218
219 *enabled = MBEDTLS_SSL_CID_ENABLED;
220
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100221 return( 0 );
222}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100223#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100224
Hanno Beckerd5847772018-08-28 10:09:23 +0100225/* Forward declarations for functions related to message buffering. */
226static void ssl_buffering_free( mbedtls_ssl_context *ssl );
227static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
228 uint8_t slot );
229static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
230static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
231static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
232static int ssl_buffer_message( mbedtls_ssl_context *ssl );
233static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100234static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100235
Hanno Beckera67dee22018-08-22 10:05:20 +0100236static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100237static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100238{
Hanno Becker11682cc2018-08-22 14:41:02 +0100239 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100240
241 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100242 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100243
244 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
245}
246
Hanno Becker67bc7c32018-08-06 11:33:50 +0100247static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
248{
Hanno Becker11682cc2018-08-22 14:41:02 +0100249 size_t const bytes_written = ssl->out_left;
250 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251
252 /* Double-check that the write-index hasn't gone
253 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100254 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100255 {
256 /* Should never happen... */
257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
258 }
259
260 return( (int) ( mtu - bytes_written ) );
261}
262
263static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
264{
265 int ret;
266 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400267 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100268
269#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
270 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
271
272 if( max_len > mfl )
273 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100274
275 /* By the standard (RFC 6066 Sect. 4), the MFL extension
276 * only limits the maximum record payload size, so in theory
277 * we would be allowed to pack multiple records of payload size
278 * MFL into a single datagram. However, this would mean that there's
279 * no way to explicitly communicate MTU restrictions to the peer.
280 *
281 * The following reduction of max_len makes sure that we never
282 * write datagrams larger than MFL + Record Expansion Overhead.
283 */
284 if( max_len <= ssl->out_left )
285 return( 0 );
286
287 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100288#endif
289
290 ret = ssl_get_remaining_space_in_datagram( ssl );
291 if( ret < 0 )
292 return( ret );
293 remaining = (size_t) ret;
294
295 ret = mbedtls_ssl_get_record_expansion( ssl );
296 if( ret < 0 )
297 return( ret );
298 expansion = (size_t) ret;
299
300 if( remaining <= expansion )
301 return( 0 );
302
303 remaining -= expansion;
304 if( remaining >= max_len )
305 remaining = max_len;
306
307 return( (int) remaining );
308}
309
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200310/*
311 * Double the retransmit timeout value, within the allowed range,
312 * returning -1 if the maximum value has already been reached.
313 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200314static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200315{
316 uint32_t new_timeout;
317
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200318 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319 return( -1 );
320
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200321 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
322 * in the following way: after the initial transmission and a first
323 * retransmission, back off to a temporary estimated MTU of 508 bytes.
324 * This value is guaranteed to be deliverable (if not guaranteed to be
325 * delivered) of any compliant IPv4 (and IPv6) network, and should work
326 * on most non-IP stacks too. */
327 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400328 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200329 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
331 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200332
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200333 new_timeout = 2 * ssl->handshake->retransmit_timeout;
334
335 /* Avoid arithmetic overflow and range overflow */
336 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200337 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200339 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200340 }
341
342 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200344 ssl->handshake->retransmit_timeout ) );
345
346 return( 0 );
347}
348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200350{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200351 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200353 ssl->handshake->retransmit_timeout ) );
354}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200358/*
359 * Convert max_fragment_length codes to length.
360 * RFC 6066 says:
361 * enum{
362 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
363 * } MaxFragmentLength;
364 * and we add 0 -> extension unused
365 */
Angus Grattond8213d02016-05-25 20:56:48 +1000366static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200367{
Angus Grattond8213d02016-05-25 20:56:48 +1000368 switch( mfl )
369 {
370 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
371 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
372 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
373 return 512;
374 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
375 return 1024;
376 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
377 return 2048;
378 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
379 return 4096;
380 default:
381 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
382 }
383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200384#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200385
Hanno Becker52055ae2019-02-06 14:30:46 +0000386int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
387 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200388{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 mbedtls_ssl_session_free( dst );
390 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000393
394#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395 if( src->peer_cert != NULL )
396 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200397 int ret;
398
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200399 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200400 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200401 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200406 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200408 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200409 dst->peer_cert = NULL;
410 return( ret );
411 }
412 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000413#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000414 if( src->peer_cert_digest != NULL )
415 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000416 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000417 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000418 if( dst->peer_cert_digest == NULL )
419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
420
421 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
422 src->peer_cert_digest_len );
423 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000424 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000425 }
426#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200429
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200430#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200431 if( src->ticket != NULL )
432 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200433 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200434 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200435 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200436
437 memcpy( dst->ticket, src->ticket, src->ticket_len );
438 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200439#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200440
441 return( 0 );
442}
443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
445int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200446 const unsigned char *key_enc, const unsigned char *key_dec,
447 size_t keylen,
448 const unsigned char *iv_enc, const unsigned char *iv_dec,
449 size_t ivlen,
450 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200451 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
453int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
454int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
455int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
456int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
457#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000458
Paul Bakker5121ce52009-01-03 21:22:43 +0000459/*
460 * Key material generation
461 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200463static int ssl3_prf( const unsigned char *secret, size_t slen,
464 const char *label,
465 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000466 unsigned char *dstbuf, size_t dlen )
467{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100468 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000469 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200470 mbedtls_md5_context md5;
471 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000472 unsigned char padding[16];
473 unsigned char sha1sum[20];
474 ((void)label);
475
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_init( &md5 );
477 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200478
Paul Bakker5f70b252012-09-13 14:23:06 +0000479 /*
480 * SSLv3:
481 * block =
482 * MD5( secret + SHA1( 'A' + secret + random ) ) +
483 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
484 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
485 * ...
486 */
487 for( i = 0; i < dlen / 16; i++ )
488 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200489 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000490
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100491 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100492 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100493 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100494 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100495 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100497 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100498 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100499 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100500 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000501
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100502 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100504 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100505 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100506 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100507 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100508 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100509 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000510 }
511
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100512exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200513 mbedtls_md5_free( &md5 );
514 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500516 mbedtls_platform_zeroize( padding, sizeof( padding ) );
517 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000518
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100519 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200521#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200524static int tls1_prf( const unsigned char *secret, size_t slen,
525 const char *label,
526 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000527 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000528{
Paul Bakker23986e52011-04-24 08:57:21 +0000529 size_t nb, hs;
530 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200531 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300532 unsigned char *tmp;
533 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000534 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 const mbedtls_md_info_t *md_info;
536 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100537 int ret;
538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200539 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000540
Ron Eldor3b350852019-05-07 18:31:49 +0300541 tmp_len = 20 + strlen( label ) + rlen;
542 tmp = mbedtls_calloc( 1, tmp_len );
543 if( tmp == NULL )
544 {
545 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
546 goto exit;
547 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 hs = ( slen + 1 ) / 2;
550 S1 = secret;
551 S2 = secret + slen - hs;
552
553 nb = strlen( label );
554 memcpy( tmp + 20, label, nb );
555 memcpy( tmp + 20 + nb, random, rlen );
556 nb += rlen;
557
558 /*
559 * First compute P_md5(secret,label+random)[0..dlen]
560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300562 {
563 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
564 goto exit;
565 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300568 {
569 goto exit;
570 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
573 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
574 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000575
576 for( i = 0; i < dlen; i += 16 )
577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200578 mbedtls_md_hmac_reset ( &md_ctx );
579 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
580 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200582 mbedtls_md_hmac_reset ( &md_ctx );
583 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
584 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000585
586 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
587
588 for( j = 0; j < k; j++ )
589 dstbuf[i + j] = h_i[j];
590 }
591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593
Paul Bakker5121ce52009-01-03 21:22:43 +0000594 /*
595 * XOR out with P_sha1(secret,label+random)[0..dlen]
596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200597 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300598 {
599 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
600 goto exit;
601 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300604 {
605 goto exit;
606 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
609 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
610 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000611
612 for( i = 0; i < dlen; i += 20 )
613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_md_hmac_reset ( &md_ctx );
615 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
616 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_md_hmac_reset ( &md_ctx );
619 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
620 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
623
624 for( j = 0; j < k; j++ )
625 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
626 }
627
Ron Eldor3b350852019-05-07 18:31:49 +0300628exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200629 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100630
Ron Eldor3b350852019-05-07 18:31:49 +0300631 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500632 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000633
Ron Eldor3b350852019-05-07 18:31:49 +0300634 mbedtls_free( tmp );
635 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200639#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500640#if defined(MBEDTLS_USE_PSA_CRYPTO)
641static int tls_prf_generic( mbedtls_md_type_t md_type,
642 const unsigned char *secret, size_t slen,
643 const char *label,
644 const unsigned char *random, size_t rlen,
645 unsigned char *dstbuf, size_t dlen )
646{
647 psa_status_t status;
648 psa_algorithm_t alg;
649 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500650 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500651 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
652
Andrzej Kurek2f760752019-01-28 08:08:15 -0500653 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500655
Andrzej Kurekc929a822019-01-14 03:51:11 -0500656 if( md_type == MBEDTLS_MD_SHA384 )
657 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
658 else
659 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
660
Andrzej Kurek2f760752019-01-28 08:08:15 -0500661 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500662 psa_key_policy_set_usage( &policy,
663 PSA_KEY_USAGE_DERIVE,
664 alg );
665 status = psa_set_key_policy( master_slot, &policy );
666 if( status != PSA_SUCCESS )
667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
668
669 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
670 if( status != PSA_SUCCESS )
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672
673 status = psa_key_derivation( &generator,
674 master_slot, alg,
675 random, rlen,
676 (unsigned char const *) label,
677 (size_t) strlen( label ),
678 dlen );
679 if( status != PSA_SUCCESS )
680 {
681 psa_generator_abort( &generator );
682 psa_destroy_key( master_slot );
683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
684 }
685
686 status = psa_generator_read( &generator, dstbuf, dlen );
687 if( status != PSA_SUCCESS )
688 {
689 psa_generator_abort( &generator );
690 psa_destroy_key( master_slot );
691 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
692 }
693
694 status = psa_generator_abort( &generator );
695 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500696 {
697 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500698 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500699 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500700
701 status = psa_destroy_key( master_slot );
702 if( status != PSA_SUCCESS )
703 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
704
Andrzej Kurek33171262019-01-15 03:25:18 -0500705 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500706}
707
708#else /* MBEDTLS_USE_PSA_CRYPTO */
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100711 const unsigned char *secret, size_t slen,
712 const char *label,
713 const unsigned char *random, size_t rlen,
714 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000715{
716 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100717 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300718 unsigned char *tmp;
719 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
721 const mbedtls_md_info_t *md_info;
722 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100723 int ret;
724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200725 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
728 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100731
Ron Eldor3b350852019-05-07 18:31:49 +0300732 tmp_len = md_len + strlen( label ) + rlen;
733 tmp = mbedtls_calloc( 1, tmp_len );
734 if( tmp == NULL )
735 {
736 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
737 goto exit;
738 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739
740 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100741 memcpy( tmp + md_len, label, nb );
742 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743 nb += rlen;
744
745 /*
746 * Compute P_<hash>(secret, label + random)[0..dlen]
747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200748 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300749 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100750
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
752 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
753 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100754
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100755 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_md_hmac_reset ( &md_ctx );
758 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
759 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 mbedtls_md_hmac_reset ( &md_ctx );
762 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
763 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100765 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000766
767 for( j = 0; j < k; j++ )
768 dstbuf[i + j] = h_i[j];
769 }
770
Ron Eldor3b350852019-05-07 18:31:49 +0300771exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100773
Ron Eldor3b350852019-05-07 18:31:49 +0300774 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500775 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776
Ron Eldor3b350852019-05-07 18:31:49 +0300777 mbedtls_free( tmp );
778
779 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500781#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200782#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100783static int tls_prf_sha256( const unsigned char *secret, size_t slen,
784 const char *label,
785 const unsigned char *random, size_t rlen,
786 unsigned char *dstbuf, size_t dlen )
787{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200788 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100789 label, random, rlen, dstbuf, dlen ) );
790}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200794static int tls_prf_sha384( const unsigned char *secret, size_t slen,
795 const char *label,
796 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000797 unsigned char *dstbuf, size_t dlen )
798{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200799 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100800 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000801}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#endif /* MBEDTLS_SHA512_C */
803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
808 defined(MBEDTLS_SSL_PROTO_TLS1_1)
809static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200810#endif
Paul Bakker380da532012-04-18 16:10:25 +0000811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200812#if defined(MBEDTLS_SSL_PROTO_SSL3)
813static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
814static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif
816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
818static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
819static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200820#endif
821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
823#if defined(MBEDTLS_SHA256_C)
824static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
825static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
826static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200827#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200829#if defined(MBEDTLS_SHA512_C)
830static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
831static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
832static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100833#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200834#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000835
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100836#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100837 defined(MBEDTLS_USE_PSA_CRYPTO)
838static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
839{
840 if( ssl->conf->f_psk != NULL )
841 {
842 /* If we've used a callback to select the PSK,
843 * the static configuration is irrelevant. */
844 if( ssl->handshake->psk_opaque != 0 )
845 return( 1 );
846
847 return( 0 );
848 }
849
850 if( ssl->conf->psk_opaque != 0 )
851 return( 1 );
852
853 return( 0 );
854}
855#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100856 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100857
Ron Eldorcf280092019-05-14 20:19:13 +0300858#if defined(MBEDTLS_SSL_EXPORT_KEYS)
859static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
860{
861#if defined(MBEDTLS_SSL_PROTO_SSL3)
862 if( tls_prf == ssl3_prf )
863 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300864 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300865 }
866 else
867#endif
868#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
869 if( tls_prf == tls1_prf )
870 {
871 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
872 }
873 else
874#endif
875#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
876#if defined(MBEDTLS_SHA512_C)
877 if( tls_prf == tls_prf_sha384 )
878 {
879 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
880 }
881 else
882#endif
883#if defined(MBEDTLS_SHA256_C)
884 if( tls_prf == tls_prf_sha256 )
885 {
886 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
887 }
888 else
889#endif
890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
891 return( MBEDTLS_SSL_TLS_PRF_NONE );
892}
893#endif /* MBEDTLS_SSL_EXPORT_KEYS */
894
Ron Eldor51d3ab52019-05-12 14:54:30 +0300895int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
896 const unsigned char *secret, size_t slen,
897 const char *label,
898 const unsigned char *random, size_t rlen,
899 unsigned char *dstbuf, size_t dlen )
900{
901 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
902
903 switch( prf )
904 {
905#if defined(MBEDTLS_SSL_PROTO_SSL3)
906 case MBEDTLS_SSL_TLS_PRF_SSL3:
907 tls_prf = ssl3_prf;
908 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300909#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300910#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
911 case MBEDTLS_SSL_TLS_PRF_TLS1:
912 tls_prf = tls1_prf;
913 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300914#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
915
916#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300917#if defined(MBEDTLS_SHA512_C)
918 case MBEDTLS_SSL_TLS_PRF_SHA384:
919 tls_prf = tls_prf_sha384;
920 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300921#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300922#if defined(MBEDTLS_SHA256_C)
923 case MBEDTLS_SSL_TLS_PRF_SHA256:
924 tls_prf = tls_prf_sha256;
925 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300926#endif /* MBEDTLS_SHA256_C */
927#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300928 default:
929 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
930 }
931
932 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
933}
934
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000936{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200937 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000938#if defined(MBEDTLS_USE_PSA_CRYPTO)
939 int psa_fallthrough;
940#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000941 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000942 unsigned char keyblk[256];
943 unsigned char *key1;
944 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100945 unsigned char *mac_enc;
946 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000947 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200948 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000949 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000950 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 const mbedtls_cipher_info_t *cipher_info;
952 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100953
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000954 /* cf. RFC 5246, Section 8.1:
955 * "The master secret is always exactly 48 bytes in length." */
956 size_t const master_secret_len = 48;
957
Hanno Becker35b23c72018-10-23 12:10:41 +0100958#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
959 unsigned char session_hash[48];
960#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 mbedtls_ssl_session *session = ssl->session_negotiate;
963 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
964 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000967
Jaeden Amero2de07f12019-06-05 13:32:08 +0100968#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
969 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000970 transform->encrypt_then_mac = session->encrypt_then_mac;
971#endif
972 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000973
974 ciphersuite_info = handshake->ciphersuite_info;
975 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100976 if( cipher_info == NULL )
977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000979 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200980 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100981 }
982
Hanno Beckere694c3e2017-12-27 21:34:08 +0000983 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100984 if( md_info == NULL )
985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200986 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000987 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200988 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100989 }
990
Hanno Beckera0e20d02019-05-15 14:03:01 +0100991#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100992 /* Copy own and peer's CID if the use of the CID
993 * extension has been negotiated. */
994 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
995 {
996 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100997
Hanno Becker05154c32019-05-03 15:23:51 +0100998 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100999 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +01001000 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +01001001 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +01001002
1003 transform->out_cid_len = ssl->handshake->peer_cid_len;
1004 memcpy( transform->out_cid, ssl->handshake->peer_cid,
1005 ssl->handshake->peer_cid_len );
1006 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
1007 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +01001008 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01001009#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +01001010
Paul Bakker5121ce52009-01-03 21:22:43 +00001011 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001014#if defined(MBEDTLS_SSL_PROTO_SSL3)
1015 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 {
Paul Bakker48916f92012-09-16 19:57:18 +00001017 handshake->tls_prf = ssl3_prf;
1018 handshake->calc_verify = ssl_calc_verify_ssl;
1019 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001020 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021 else
1022#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001023#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1024 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001025 {
Paul Bakker48916f92012-09-16 19:57:18 +00001026 handshake->tls_prf = tls1_prf;
1027 handshake->calc_verify = ssl_calc_verify_tls;
1028 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001030 else
1031#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1033#if defined(MBEDTLS_SHA512_C)
1034 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001035 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001036 {
Paul Bakker48916f92012-09-16 19:57:18 +00001037 handshake->tls_prf = tls_prf_sha384;
1038 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1039 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001040 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001041 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001042#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001043#if defined(MBEDTLS_SHA256_C)
1044 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001045 {
Paul Bakker48916f92012-09-16 19:57:18 +00001046 handshake->tls_prf = tls_prf_sha256;
1047 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1048 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001049 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001050 else
1051#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001054 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1055 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001056 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001057
1058 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001059 * SSLv3:
1060 * master =
1061 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1062 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1063 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001064 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001065 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001066 * master = PRF( premaster, "master secret", randbytes )[0..47]
1067 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001068 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001069 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001070 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1071 }
1072 else
1073 {
1074 /* The label for the KDF used for key expansion.
1075 * This is either "master secret" or "extended master secret"
1076 * depending on whether the Extended Master Secret extension
1077 * is used. */
1078 char const *lbl = "master secret";
1079
1080 /* The salt for the KDF used for key expansion.
1081 * - If the Extended Master Secret extension is not used,
1082 * this is ClientHello.Random + ServerHello.Random
1083 * (see Sect. 8.1 in RFC 5246).
1084 * - If the Extended Master Secret extension is used,
1085 * this is the transcript of the handshake so far.
1086 * (see Sect. 4 in RFC 7627). */
1087 unsigned char const *salt = handshake->randbytes;
1088 size_t salt_len = 64;
1089
1090#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001091 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094
Hanno Becker35b23c72018-10-23 12:10:41 +01001095 lbl = "extended master secret";
1096 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001097 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1099 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001102 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1103 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001104 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001105 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001106 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001107#endif /* MBEDTLS_SHA512_C */
1108 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001109 }
1110 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001111#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001112 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001113
Hanno Becker35b23c72018-10-23 12:10:41 +01001114 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001115 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001116#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1117
Hanno Becker7d0a5692018-10-23 15:26:22 +01001118#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1119 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1120 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1121 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1122 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001123 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 /* Perform PSK-to-MS expansion in a single step. */
1125 psa_status_t status;
1126 psa_algorithm_t alg;
1127 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001128 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001129
1130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1131
1132 psk = ssl->conf->psk_opaque;
1133 if( ssl->handshake->psk_opaque != 0 )
1134 psk = ssl->handshake->psk_opaque;
1135
Hanno Becker22bf1452019-04-05 11:21:08 +01001136 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001137 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1138 else
1139 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1140
1141 status = psa_key_derivation( &generator, psk, alg,
1142 salt, salt_len,
1143 (unsigned char const *) lbl,
1144 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001145 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001146 if( status != PSA_SUCCESS )
1147 {
1148 psa_generator_abort( &generator );
1149 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1150 }
1151
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001152 status = psa_generator_read( &generator, session->master,
1153 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001154 if( status != PSA_SUCCESS )
1155 {
1156 psa_generator_abort( &generator );
1157 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1158 }
1159
1160 status = psa_generator_abort( &generator );
1161 if( status != PSA_SUCCESS )
1162 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001163 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001164 else
1165#endif
1166 {
1167 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1168 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001169 session->master,
1170 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001171 if( ret != 0 )
1172 {
1173 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1174 return( ret );
1175 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001176
Hanno Becker7d0a5692018-10-23 15:26:22 +01001177 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1178 handshake->premaster,
1179 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001180
Hanno Becker7d0a5692018-10-23 15:26:22 +01001181 mbedtls_platform_zeroize( handshake->premaster,
1182 sizeof(handshake->premaster) );
1183 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001185
1186 /*
1187 * Swap the client and server random values.
1188 */
Paul Bakker48916f92012-09-16 19:57:18 +00001189 memcpy( tmp, handshake->randbytes, 64 );
1190 memcpy( handshake->randbytes, tmp + 32, 32 );
1191 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001192 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001193
1194 /*
1195 * SSLv3:
1196 * key block =
1197 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1198 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1199 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1200 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1201 * ...
1202 *
1203 * TLSv1:
1204 * key block = PRF( master, "key expansion", randbytes )
1205 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001206 ret = handshake->tls_prf( session->master, 48, "key expansion",
1207 handshake->randbytes, 64, keyblk, 256 );
1208 if( ret != 0 )
1209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001211 return( ret );
1212 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1215 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1216 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1217 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1218 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001219
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 /*
1221 * Determine the appropriate key, IV and MAC length.
1222 */
Paul Bakker68884e32013-01-07 18:20:04 +01001223
Hanno Becker88aaf652017-12-27 08:17:40 +00001224 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001225
Hanno Becker8031d062018-01-03 15:32:31 +00001226#if defined(MBEDTLS_GCM_C) || \
1227 defined(MBEDTLS_CCM_C) || \
1228 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001229 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001230 cipher_info->mode == MBEDTLS_MODE_CCM ||
1231 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001232 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001233 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001234
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001236 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001237 transform->taglen =
1238 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001239
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001240 /* All modes haves 96-bit IVs;
1241 * GCM and CCM has 4 implicit and 8 explicit bytes
1242 * ChachaPoly has all 12 bytes implicit
1243 */
Paul Bakker68884e32013-01-07 18:20:04 +01001244 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001245 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1246 transform->fixed_ivlen = 12;
1247 else
1248 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001249
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001250 /* Minimum length of encrypted record */
1251 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001252 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 }
1254 else
Hanno Becker8031d062018-01-03 15:32:31 +00001255#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1256#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1257 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1258 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001259 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001260 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1262 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001265 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001266 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001267
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001268 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001269 mac_key_len = mbedtls_md_get_size( md_info );
1270 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001272#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001273 /*
1274 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1275 * (rfc 6066 page 13 or rfc 2104 section 4),
1276 * so we only need to adjust the length here.
1277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001278 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001280 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001281
1282#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1283 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001284 * HMAC implementation which also truncates the key
1285 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001286 mac_key_len = transform->maclen;
1287#endif
1288 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001290
1291 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001292 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001293
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001294 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001295 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001296 transform->minlen = transform->maclen;
1297 else
Paul Bakker68884e32013-01-07 18:20:04 +01001298 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299 /*
1300 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001301 * 1. if EtM is in use: one block plus MAC
1302 * otherwise: * first multiple of blocklen greater than maclen
1303 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001304 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1306 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001307 {
1308 transform->minlen = transform->maclen
1309 + cipher_info->block_size;
1310 }
1311 else
1312#endif
1313 {
1314 transform->minlen = transform->maclen
1315 + cipher_info->block_size
1316 - transform->maclen % cipher_info->block_size;
1317 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001319#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1320 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1321 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001322 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001323 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001325#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1326 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1327 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001328 {
1329 transform->minlen += transform->ivlen;
1330 }
1331 else
1332#endif
1333 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001334 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001335 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1336 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001337 }
Paul Bakker68884e32013-01-07 18:20:04 +01001338 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
Hanno Becker8031d062018-01-03 15:32:31 +00001340 else
1341#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1342 {
1343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1345 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001346
Hanno Becker88aaf652017-12-27 08:17:40 +00001347 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1348 (unsigned) keylen,
1349 (unsigned) transform->minlen,
1350 (unsigned) transform->ivlen,
1351 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
1353 /*
1354 * Finally setup the cipher contexts, IVs and MAC secrets.
1355 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001357 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001358 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001359 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001360 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Paul Bakker68884e32013-01-07 18:20:04 +01001362 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001363 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001364
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001365 /*
1366 * This is not used in TLS v1.1.
1367 */
Paul Bakker48916f92012-09-16 19:57:18 +00001368 iv_copy_len = ( transform->fixed_ivlen ) ?
1369 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001370 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1371 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001372 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001373 }
1374 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001375#endif /* MBEDTLS_SSL_CLI_C */
1376#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001377 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001378 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001379 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001380 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001381
Hanno Becker81c7b182017-11-09 18:39:33 +00001382 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001383 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001384
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001385 /*
1386 * This is not used in TLS v1.1.
1387 */
Paul Bakker48916f92012-09-16 19:57:18 +00001388 iv_copy_len = ( transform->fixed_ivlen ) ?
1389 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001390 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1391 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001392 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001394 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1399 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001400 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001401
Hanno Beckerd56ed242018-01-03 15:32:51 +00001402#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_PROTO_SSL3)
1404 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001405 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001406 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001409 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1410 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001411 }
1412
Hanno Becker81c7b182017-11-09 18:39:33 +00001413 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1414 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001415 }
1416 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1418#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1419 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1420 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001421 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001422 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1423 For AEAD-based ciphersuites, there is nothing to do here. */
1424 if( mac_key_len != 0 )
1425 {
1426 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1427 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1428 }
Paul Bakker68884e32013-01-07 18:20:04 +01001429 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001430 else
1431#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001434 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1435 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001436 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001437#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1440 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001441 {
1442 int ret = 0;
1443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001445
Hanno Becker88aaf652017-12-27 08:17:40 +00001446 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001447 transform->iv_enc, transform->iv_dec,
1448 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001449 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001450 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001453 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1454 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001455 }
1456 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001457#else
1458 ((void) mac_dec);
1459 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001460#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001461
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001462#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1463 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001464 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001465 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1466 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001467 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001468 iv_copy_len );
1469 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001470
1471 if( ssl->conf->f_export_keys_ext != NULL )
1472 {
1473 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1474 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001475 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001476 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001477 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001478 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001479 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001480 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001481#endif
1482
Hanno Beckerf704bef2018-11-16 15:21:18 +00001483#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001484
1485 /* Only use PSA-based ciphers for TLS-1.2.
1486 * That's relevant at least for TLS-1.0, where
1487 * we assume that mbedtls_cipher_crypt() updates
1488 * the structure field for the IV, which the PSA-based
1489 * implementation currently doesn't. */
1490#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1491 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001493 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001494 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001495 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1496 {
1497 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001498 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 }
1500
1501 if( ret == 0 )
1502 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001504 psa_fallthrough = 0;
1505 }
1506 else
1507 {
1508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1509 psa_fallthrough = 1;
1510 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001511 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001512 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001513 psa_fallthrough = 1;
1514#else
1515 psa_fallthrough = 1;
1516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001517
Hanno Beckercb1cc802018-11-17 22:27:38 +00001518 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001519#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001520 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001521 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001522 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001523 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001524 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001525 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001526
Hanno Beckerf704bef2018-11-16 15:21:18 +00001527#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001528 /* Only use PSA-based ciphers for TLS-1.2.
1529 * That's relevant at least for TLS-1.0, where
1530 * we assume that mbedtls_cipher_crypt() updates
1531 * the structure field for the IV, which the PSA-based
1532 * implementation currently doesn't. */
1533#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1534 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001535 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001536 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001537 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001538 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1539 {
1540 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001541 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 }
1543
1544 if( ret == 0 )
1545 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001546 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001547 psa_fallthrough = 0;
1548 }
1549 else
1550 {
1551 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1552 psa_fallthrough = 1;
1553 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001554 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001555 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001556 psa_fallthrough = 1;
1557#else
1558 psa_fallthrough = 1;
1559#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001560
Hanno Beckercb1cc802018-11-17 22:27:38 +00001561 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001562#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001563 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 cipher_info ) ) != 0 )
1565 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001567 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001571 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001573 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001575 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001579 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001583 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586#if defined(MBEDTLS_CIPHER_MODE_CBC)
1587 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1590 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001591 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001593 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001594 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1597 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001600 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001602 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001603#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001604
Paul Bakker5121ce52009-01-03 21:22:43 +00001605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001606#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001607 // Initialize compression
1608 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001610 {
Paul Bakker16770332013-10-11 09:59:44 +02001611 if( ssl->compress_buf == NULL )
1612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001613 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001614 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001615 if( ssl->compress_buf == NULL )
1616 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001618 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001619 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1620 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001621 }
1622 }
1623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625
Paul Bakker48916f92012-09-16 19:57:18 +00001626 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1627 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001628
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001629 if( deflateInit( &transform->ctx_deflate,
1630 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001631 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001633 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001634 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1635 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001636 }
1637 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001641end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001642 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1643 mbedtls_platform_zeroize( handshake->randbytes,
1644 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001645 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001646}
1647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#if defined(MBEDTLS_SSL_PROTO_SSL3)
1649void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001651 mbedtls_md5_context md5;
1652 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653 unsigned char pad_1[48];
1654 unsigned char pad_2[48];
1655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001658 mbedtls_md5_init( &md5 );
1659 mbedtls_sha1_init( &sha1 );
1660
1661 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1662 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Paul Bakker380da532012-04-18 16:10:25 +00001664 memset( pad_1, 0x36, 48 );
1665 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001667 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1668 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1669 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001670
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001671 mbedtls_md5_starts_ret( &md5 );
1672 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1673 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1674 mbedtls_md5_update_ret( &md5, hash, 16 );
1675 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001676
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001677 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1678 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1679 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001681 mbedtls_sha1_starts_ret( &sha1 );
1682 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1683 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1684 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1685 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1688 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001690 mbedtls_md5_free( &md5 );
1691 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001692
Paul Bakker380da532012-04-18 16:10:25 +00001693 return;
1694}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001697#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1698void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001699{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001700 mbedtls_md5_context md5;
1701 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001704
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001705 mbedtls_md5_init( &md5 );
1706 mbedtls_sha1_init( &sha1 );
1707
1708 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1709 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001710
Andrzej Kurekeb342242019-01-29 09:14:33 -05001711 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001712 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001714 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001716
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001717 mbedtls_md5_free( &md5 );
1718 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001719
Paul Bakker380da532012-04-18 16:10:25 +00001720 return;
1721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1725#if defined(MBEDTLS_SHA256_C)
1726void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001727{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001728#if defined(MBEDTLS_USE_PSA_CRYPTO)
1729 size_t hash_size;
1730 psa_status_t status;
1731 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1732
1733 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1734 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1735 if( status != PSA_SUCCESS )
1736 {
1737 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1738 return;
1739 }
1740
1741 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1742 if( status != PSA_SUCCESS )
1743 {
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1745 return;
1746 }
1747 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1749#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001750 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001751
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001752 mbedtls_sha256_init( &sha256 );
1753
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001754 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001755
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001756 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001757 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001759 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001761
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001762 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001764 return;
1765}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001768#if defined(MBEDTLS_SHA512_C)
1769void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001770{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001771#if defined(MBEDTLS_USE_PSA_CRYPTO)
1772 size_t hash_size;
1773 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001774 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001775
1776 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001777 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001778 if( status != PSA_SUCCESS )
1779 {
1780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1781 return;
1782 }
1783
Andrzej Kurek972fba52019-01-30 03:29:12 -05001784 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001785 if( status != PSA_SUCCESS )
1786 {
1787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1788 return;
1789 }
1790 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1792#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001793 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001794
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001795 mbedtls_sha512_init( &sha512 );
1796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001798
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001799 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001800 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001804
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001805 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001806#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001807 return;
1808}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001809#endif /* MBEDTLS_SHA512_C */
1810#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001812#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1813int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001814{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001815 unsigned char *p = ssl->handshake->premaster;
1816 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001817 const unsigned char *psk = ssl->conf->psk;
1818 size_t psk_len = ssl->conf->psk_len;
1819
1820 /* If the psk callback was called, use its result */
1821 if( ssl->handshake->psk != NULL )
1822 {
1823 psk = ssl->handshake->psk;
1824 psk_len = ssl->handshake->psk_len;
1825 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001826
1827 /*
1828 * PMS = struct {
1829 * opaque other_secret<0..2^16-1>;
1830 * opaque psk<0..2^16-1>;
1831 * };
1832 * with "other_secret" depending on the particular key exchange
1833 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1835 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001836 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001837 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001839
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001840 *(p++) = (unsigned char)( psk_len >> 8 );
1841 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001842
1843 if( end < p || (size_t)( end - p ) < psk_len )
1844 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1845
1846 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001847 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001848 }
1849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1851#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1852 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001853 {
1854 /*
1855 * other_secret already set by the ClientKeyExchange message,
1856 * and is 48 bytes long
1857 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001858 if( end - p < 2 )
1859 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1860
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001861 *p++ = 0;
1862 *p++ = 48;
1863 p += 48;
1864 }
1865 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1867#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1868 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001870 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001871 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001873 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001875 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001876 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001878 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 return( ret );
1880 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001881 *(p++) = (unsigned char)( len >> 8 );
1882 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001883 p += len;
1884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 }
1887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001888#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1889#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1890 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001891 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001892 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 size_t zlen;
1894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001896 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001897 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001900 return( ret );
1901 }
1902
1903 *(p++) = (unsigned char)( zlen >> 8 );
1904 *(p++) = (unsigned char)( zlen );
1905 p += zlen;
1906
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001907 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1908 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001909 }
1910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001911#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001912 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1914 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001915 }
1916
1917 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001918 if( end - p < 2 )
1919 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001920
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001921 *(p++) = (unsigned char)( psk_len >> 8 );
1922 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001923
1924 if( end < p || (size_t)( end - p ) < psk_len )
1925 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1926
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001927 memcpy( p, psk, psk_len );
1928 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001929
1930 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1931
1932 return( 0 );
1933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001937/*
1938 * SSLv3.0 MAC functions
1939 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001940#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001941static void ssl_mac( mbedtls_md_context_t *md_ctx,
1942 const unsigned char *secret,
1943 const unsigned char *buf, size_t len,
1944 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001945 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001946{
1947 unsigned char header[11];
1948 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001949 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001950 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1951 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001952
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001953 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001955 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001956 else
Paul Bakker68884e32013-01-07 18:20:04 +01001957 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
1959 memcpy( header, ctr, 8 );
1960 header[ 8] = (unsigned char) type;
1961 header[ 9] = (unsigned char)( len >> 8 );
1962 header[10] = (unsigned char)( len );
1963
Paul Bakker68884e32013-01-07 18:20:04 +01001964 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965 mbedtls_md_starts( md_ctx );
1966 mbedtls_md_update( md_ctx, secret, md_size );
1967 mbedtls_md_update( md_ctx, padding, padlen );
1968 mbedtls_md_update( md_ctx, header, 11 );
1969 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001970 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001971
Paul Bakker68884e32013-01-07 18:20:04 +01001972 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001973 mbedtls_md_starts( md_ctx );
1974 mbedtls_md_update( md_ctx, secret, md_size );
1975 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001976 mbedtls_md_update( md_ctx, out, md_size );
1977 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001978}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001979#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001980
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001981/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001982 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001983#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001984 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1985 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1986 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1987/* This function makes sure every byte in the memory region is accessed
1988 * (in ascending addresses order) */
1989static void ssl_read_memory( unsigned char *p, size_t len )
1990{
1991 unsigned char acc = 0;
1992 volatile unsigned char force;
1993
1994 for( ; len != 0; p++, len-- )
1995 acc ^= *p;
1996
1997 force = acc;
1998 (void) force;
1999}
2000#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
2001
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002002/*
Paul Bakker5121ce52009-01-03 21:22:43 +00002003 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02002004 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002005
Hanno Beckera0e20d02019-05-15 14:03:01 +01002006#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01002007/* This functions transforms a DTLS plaintext fragment and a record content
2008 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002009 *
2010 * struct {
2011 * opaque content[DTLSPlaintext.length];
2012 * ContentType real_type;
2013 * uint8 zeros[length_of_padding];
2014 * } DTLSInnerPlaintext;
2015 *
2016 * Input:
2017 * - `content`: The beginning of the buffer holding the
2018 * plaintext to be wrapped.
2019 * - `*content_size`: The length of the plaintext in Bytes.
2020 * - `max_len`: The number of Bytes available starting from
2021 * `content`. This must be `>= *content_size`.
2022 * - `rec_type`: The desired record content type.
2023 *
2024 * Output:
2025 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2026 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2027 *
2028 * Returns:
2029 * - `0` on success.
2030 * - A negative error code if `max_len` didn't offer enough space
2031 * for the expansion.
2032 */
2033static int ssl_cid_build_inner_plaintext( unsigned char *content,
2034 size_t *content_size,
2035 size_t remaining,
2036 uint8_t rec_type )
2037{
2038 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002039 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2040 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2041 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002042
2043 /* Write real content type */
2044 if( remaining == 0 )
2045 return( -1 );
2046 content[ len ] = rec_type;
2047 len++;
2048 remaining--;
2049
2050 if( remaining < pad )
2051 return( -1 );
2052 memset( content + len, 0, pad );
2053 len += pad;
2054 remaining -= pad;
2055
2056 *content_size = len;
2057 return( 0 );
2058}
2059
Hanno Becker07dc97d2019-05-20 15:08:01 +01002060/* This function parses a DTLSInnerPlaintext structure.
2061 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002062static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2063 size_t *content_size,
2064 uint8_t *rec_type )
2065{
2066 size_t remaining = *content_size;
2067
2068 /* Determine length of padding by skipping zeroes from the back. */
2069 do
2070 {
2071 if( remaining == 0 )
2072 return( -1 );
2073 remaining--;
2074 } while( content[ remaining ] == 0 );
2075
2076 *content_size = remaining;
2077 *rec_type = content[ remaining ];
2078
2079 return( 0 );
2080}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002081#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002082
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002083/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002084 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002085static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002086 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002087 mbedtls_record *rec )
2088{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002089 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002090 *
2091 * additional_data = seq_num + TLSCompressed.type +
2092 * TLSCompressed.version + TLSCompressed.length;
2093 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002094 * For the CID extension, this is extended as follows
2095 * (quoting draft-ietf-tls-dtls-connection-id-05,
2096 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002097 *
2098 * additional_data = seq_num + DTLSPlaintext.type +
2099 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002100 * cid +
2101 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002102 * length_of_DTLSInnerPlaintext;
2103 */
2104
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002105 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2106 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002107 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002108
Hanno Beckera0e20d02019-05-15 14:03:01 +01002109#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002110 if( rec->cid_len != 0 )
2111 {
2112 memcpy( add_data + 11, rec->cid, rec->cid_len );
2113 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2114 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2115 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2116 *add_data_len = 13 + 1 + rec->cid_len;
2117 }
2118 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002119#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002120 {
2121 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2122 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2123 *add_data_len = 13;
2124 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125}
2126
Hanno Beckera18d1322018-01-03 14:27:32 +00002127int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2128 mbedtls_ssl_transform *transform,
2129 mbedtls_record *rec,
2130 int (*f_rng)(void *, unsigned char *, size_t),
2131 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002132{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002133 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002134 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002136 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002137 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002138 size_t post_avail;
2139
2140 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002141#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002142 ((void) ssl);
2143#endif
2144
2145 /* The PRNG is used for dynamic IV generation that's used
2146 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2147#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2148 ( defined(MBEDTLS_AES_C) || \
2149 defined(MBEDTLS_ARIA_C) || \
2150 defined(MBEDTLS_CAMELLIA_C) ) && \
2151 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2152 ((void) f_rng);
2153 ((void) p_rng);
2154#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002157
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002159 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2162 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002163 if( rec == NULL
2164 || rec->buf == NULL
2165 || rec->buf_len < rec->data_offset
2166 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002167#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002168 || rec->cid_len != 0
2169#endif
2170 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002173 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002174 }
2175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002176 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002177 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002178 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002179 data, rec->data_len );
2180
2181 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2182
2183 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2184 {
2185 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2186 (unsigned) rec->data_len,
2187 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2189 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002190
Hanno Beckera0e20d02019-05-15 14:03:01 +01002191#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002192 /*
2193 * Add CID information
2194 */
2195 rec->cid_len = transform->out_cid_len;
2196 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2197 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002198
2199 if( rec->cid_len != 0 )
2200 {
2201 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002202 * Wrap plaintext into DTLSInnerPlaintext structure.
2203 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002204 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002205 * Note that this changes `rec->data_len`, and hence
2206 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002207 */
2208 if( ssl_cid_build_inner_plaintext( data,
2209 &rec->data_len,
2210 post_avail,
2211 rec->type ) != 0 )
2212 {
2213 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2214 }
2215
2216 rec->type = MBEDTLS_SSL_MSG_CID;
2217 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002218#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002219
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002220 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2221
Paul Bakker5121ce52009-01-03 21:22:43 +00002222 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002223 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002224 */
Hanno Becker52344c22018-01-03 15:24:20 +00002225#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226 if( mode == MBEDTLS_MODE_STREAM ||
2227 ( mode == MBEDTLS_MODE_CBC
2228#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002230#endif
2231 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002232 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002233 if( post_avail < transform->maclen )
2234 {
2235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2236 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2237 }
2238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002239#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002242 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2244 data, rec->data_len, rec->ctr, rec->type, mac );
2245 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 }
2247 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002249#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2250 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002251 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 {
Hanno Becker992b6872017-11-09 18:57:39 +00002253 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2254
Hanno Beckercab87e62019-04-29 13:52:53 +01002255 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002256
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002258 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2260 data, rec->data_len );
2261 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2262 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2263
2264 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002265 }
2266 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002267#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2270 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002271 }
2272
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2274 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002275
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 rec->data_len += transform->maclen;
2277 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002278 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002279 }
Hanno Becker52344c22018-01-03 15:24:20 +00002280#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002282 /*
2283 * Encrypt
2284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2286 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002287 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002288 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002290 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 "including %d bytes of padding",
2292 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002293
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2295 transform->iv_enc, transform->ivlen,
2296 data, rec->data_len,
2297 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002300 return( ret );
2301 }
2302
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002303 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002304 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002305 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2306 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002307 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002308 }
Paul Bakker68884e32013-01-07 18:20:04 +01002309 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002310#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002311
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002312#if defined(MBEDTLS_GCM_C) || \
2313 defined(MBEDTLS_CCM_C) || \
2314 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002315 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002316 mode == MBEDTLS_MODE_CCM ||
2317 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002318 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002319 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002321 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002322
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 /* Check that there's space for both the authentication tag
2324 * and the explicit IV before and after the record content. */
2325 if( post_avail < transform->taglen ||
2326 rec->data_offset < explicit_iv_len )
2327 {
2328 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2329 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2330 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002331
Paul Bakker68884e32013-01-07 18:20:04 +01002332 /*
2333 * Generate IV
2334 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2336 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002337 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002338 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2340 explicit_iv_len );
2341 /* Prefix record content with explicit IV. */
2342 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002343 }
2344 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2345 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002346 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 unsigned char i;
2348
2349 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2350
2351 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002353 }
2354 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002355 {
2356 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2358 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002359 }
2360
Hanno Beckercab87e62019-04-29 13:52:53 +01002361 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002362
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2364 iv, transform->ivlen );
2365 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 data - explicit_iv_len, explicit_iv_len );
2367 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002368 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002369 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002370 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002371 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002372
Paul Bakker68884e32013-01-07 18:20:04 +01002373 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002374 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002375 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002377 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002379 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002380 data, rec->data_len, /* source */
2381 data, &rec->data_len, /* destination */
2382 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002385 return( ret );
2386 }
2387
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002388 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2389 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002390
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 rec->data_len += transform->taglen + explicit_iv_len;
2392 rec->data_offset -= explicit_iv_len;
2393 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002394 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002395 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002397#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2398#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002399 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002400 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002401 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002402 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002403 size_t padlen, i;
2404 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002405
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 /* Currently we're always using minimal padding
2407 * (up to 255 bytes would be allowed). */
2408 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2409 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002410 padlen = 0;
2411
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002412 /* Check there's enough space in the buffer for the padding. */
2413 if( post_avail < padlen + 1 )
2414 {
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2416 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2417 }
2418
Paul Bakker5121ce52009-01-03 21:22:43 +00002419 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002421
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002422 rec->data_len += padlen + 1;
2423 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002425#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002427 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2428 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002430 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 if( f_rng == NULL )
2433 {
2434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2435 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2436 }
2437
2438 if( rec->data_offset < transform->ivlen )
2439 {
2440 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2441 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2442 }
2443
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444 /*
2445 * Generate IV
2446 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002448 if( ret != 0 )
2449 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002450
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002451 memcpy( data - transform->ivlen, transform->iv_enc,
2452 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002453
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002454 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002457 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002458 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002460 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002461
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2463 transform->iv_enc,
2464 transform->ivlen,
2465 data, rec->data_len,
2466 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002469 return( ret );
2470 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002471
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002472 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002473 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2475 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002476 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002478#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002479 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002480 {
2481 /*
2482 * Save IV in SSL3 and TLS1
2483 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002484 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2485 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002486 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002487 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002488#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489 {
2490 data -= transform->ivlen;
2491 rec->data_offset -= transform->ivlen;
2492 rec->data_len += transform->ivlen;
2493 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002495#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002496 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002497 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002498 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2499
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500 /*
2501 * MAC(MAC_write_key, seq_num +
2502 * TLSCipherText.type +
2503 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002504 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002505 * IV + // except for TLS 1.0
2506 * ENC(content + padding + padding_length));
2507 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508
2509 if( post_avail < transform->maclen)
2510 {
2511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2512 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2513 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Beckercab87e62019-04-29 13:52:53 +01002515 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002517 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002518 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002519 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002520
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002521 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002522 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002523 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2524 data, rec->data_len );
2525 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2526 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002527
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002528 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002529
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002530 rec->data_len += transform->maclen;
2531 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002532 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002533 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002535 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002536 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002538 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002540 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2541 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002542 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002543
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002544 /* Make extra sure authentication was performed, exactly once */
2545 if( auth_done != 1 )
2546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002549 }
2550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002552
2553 return( 0 );
2554}
2555
Hanno Beckera18d1322018-01-03 14:27:32 +00002556int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2557 mbedtls_ssl_transform *transform,
2558 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002559{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002560 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002563#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002564 size_t padlen = 0, correct = 1;
2565#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002566 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002567 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002568 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002569
Hanno Beckera18d1322018-01-03 14:27:32 +00002570#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002571 ((void) ssl);
2572#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002575 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002576 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002577 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2578 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2579 }
2580 if( rec == NULL ||
2581 rec->buf == NULL ||
2582 rec->buf_len < rec->data_offset ||
2583 rec->buf_len - rec->data_offset < rec->data_len )
2584 {
2585 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002586 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002587 }
2588
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002589 data = rec->buf + rec->data_offset;
2590 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002591
Hanno Beckera0e20d02019-05-15 14:03:01 +01002592#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002593 /*
2594 * Match record's CID with incoming CID.
2595 */
Hanno Becker938489a2019-05-08 13:02:22 +01002596 if( rec->cid_len != transform->in_cid_len ||
2597 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2598 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002599 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002600 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002601#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002602
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2604 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002605 {
2606 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002607 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2608 transform->iv_dec,
2609 transform->ivlen,
2610 data, rec->data_len,
2611 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002613 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002614 return( ret );
2615 }
2616
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002617 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2620 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002621 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002622 }
Paul Bakker68884e32013-01-07 18:20:04 +01002623 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002625#if defined(MBEDTLS_GCM_C) || \
2626 defined(MBEDTLS_CCM_C) || \
2627 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002629 mode == MBEDTLS_MODE_CCM ||
2630 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002631 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002634
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002635 /*
2636 * Compute and update sizes
2637 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002638 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002641 "+ taglen (%d)", rec->data_len,
2642 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002643 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002644 }
Paul Bakker68884e32013-01-07 18:20:04 +01002645
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002646 /*
2647 * Prepare IV
2648 */
2649 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2650 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002651 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002653 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002654
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002655 }
2656 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2657 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002658 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002659 unsigned char i;
2660
2661 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2662
2663 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002665 }
2666 else
2667 {
2668 /* Reminder if we ever add an AEAD mode with a different size */
2669 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2670 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2671 }
2672
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 data += explicit_iv_len;
2674 rec->data_offset += explicit_iv_len;
2675 rec->data_len -= explicit_iv_len + transform->taglen;
2676
Hanno Beckercab87e62019-04-29 13:52:53 +01002677 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002679 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680
2681 memcpy( transform->iv_dec + transform->fixed_ivlen,
2682 data - explicit_iv_len, explicit_iv_len );
2683
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002684 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002685 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002686 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002687
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002688
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002689 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002690 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002691 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002692 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2693 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002694 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002695 data, rec->data_len,
2696 data, &olen,
2697 data + rec->data_len,
2698 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002699 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2703 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002704
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002705 return( ret );
2706 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002707 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002708
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002709 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002713 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002714 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002715 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002716#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2717#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002718 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002719 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002720 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002721 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002722
Paul Bakker5121ce52009-01-03 21:22:43 +00002723 /*
Paul Bakker45829992013-01-03 14:52:21 +01002724 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002725 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002726#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002727 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2728 {
2729 /* The ciphertext is prefixed with the CBC IV. */
2730 minlen += transform->ivlen;
2731 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002732#endif
Paul Bakker45829992013-01-03 14:52:21 +01002733
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002734 /* Size considerations:
2735 *
2736 * - The CBC cipher text must not be empty and hence
2737 * at least of size transform->ivlen.
2738 *
2739 * Together with the potential IV-prefix, this explains
2740 * the first of the two checks below.
2741 *
2742 * - The record must contain a MAC, either in plain or
2743 * encrypted, depending on whether Encrypt-then-MAC
2744 * is used or not.
2745 * - If it is, the message contains the IV-prefix,
2746 * the CBC ciphertext, and the MAC.
2747 * - If it is not, the padded plaintext, and hence
2748 * the CBC ciphertext, has at least length maclen + 1
2749 * because there is at least the padding length byte.
2750 *
2751 * As the CBC ciphertext is not empty, both cases give the
2752 * lower bound minlen + maclen + 1 on the record size, which
2753 * we test for in the second check below.
2754 */
2755 if( rec->data_len < minlen + transform->ivlen ||
2756 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 "+ 1 ) ( + expl IV )", rec->data_len,
2760 transform->ivlen,
2761 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002762 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002763 }
2764
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765 /*
2766 * Authenticate before decrypt if enabled
2767 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002768#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002769 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002770 {
Hanno Becker992b6872017-11-09 18:57:39 +00002771 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002773 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002774
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2776 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777
Hanno Beckercab87e62019-04-29 13:52:53 +01002778 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002779
Hanno Beckercab87e62019-04-29 13:52:53 +01002780 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2781 add_data_len );
2782 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2783 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002784 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2785 data, rec->data_len );
2786 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2787 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002788
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002789 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2790 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002791 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002792 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2795 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002799 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002800 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002801 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002803
2804 /*
2805 * Check length sanity
2806 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002809 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002810 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002811 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002812 }
2813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002814#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002816 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002819 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002820 /* This is safe because data_len >= minlen + maclen + 1 initially,
2821 * and at this point we have at most subtracted maclen (note that
2822 * minlen == transform->ivlen here). */
2823 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002824
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 data += transform->ivlen;
2826 rec->data_offset += transform->ivlen;
2827 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002828 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002830
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002831 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2832 transform->iv_dec, transform->ivlen,
2833 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002836 return( ret );
2837 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002838
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2842 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002843 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002845#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002846 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002847 {
2848 /*
2849 * Save IV in SSL3 and TLS1
2850 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002851 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2852 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002853 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002854#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002855
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 /* Safe since data_len >= minlen + maclen + 1, so after having
2857 * subtracted at most minlen and maclen up to this point,
2858 * data_len > 0. */
2859 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002860
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002861 if( auth_done == 1 )
2862 {
2863 correct *= ( rec->data_len >= padlen + 1 );
2864 padlen *= ( rec->data_len >= padlen + 1 );
2865 }
2866 else
Paul Bakker45829992013-01-03 14:52:21 +01002867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 if( rec->data_len < transform->maclen + padlen + 1 )
2870 {
2871 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2872 rec->data_len,
2873 transform->maclen,
2874 padlen + 1 ) );
2875 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002876#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002877
2878 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2879 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002881
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 padlen++;
2883
2884 /* Regardless of the validity of the padding,
2885 * we have data_len >= padlen here. */
2886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002892#if defined(MBEDTLS_SSL_DEBUG_ALL)
2893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 "should be no more than %d",
2895 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002896#endif
Paul Bakker45829992013-01-03 14:52:21 +01002897 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002898 }
2899 }
2900 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002901#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2902#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2903 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002905 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 /* The padding check involves a series of up to 256
2907 * consecutive memory reads at the end of the record
2908 * plaintext buffer. In order to hide the length and
2909 * validity of the padding, always perform exactly
2910 * `min(256,plaintext_len)` reads (but take into account
2911 * only the last `padlen` bytes for the padding check). */
2912 size_t pad_count = 0;
2913 size_t real_count = 0;
2914 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002915
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 /* Index of first padding byte; it has been ensured above
2917 * that the subtraction is safe. */
2918 size_t const padding_idx = rec->data_len - padlen;
2919 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2920 size_t const start_idx = rec->data_len - num_checks;
2921 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002922
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002923 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002924 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002925 real_count |= ( idx >= padding_idx );
2926 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002927 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002928 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002931 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002933#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002934 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2938 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002942 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002943
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002944 /* If the padding was found to be invalid, padlen == 0
2945 * and the subtraction is safe. If the padding was found valid,
2946 * padlen hasn't been changed and the previous assertion
2947 * data_len >= padlen still holds. */
2948 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002949 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002950 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002951#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002952 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2955 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002957
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002958#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002959 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002960 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002961#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002962
2963 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002964 * Authenticate if not done yet.
2965 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002966 */
Hanno Becker52344c22018-01-03 15:24:20 +00002967#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002968 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002969 {
Hanno Becker992b6872017-11-09 18:57:39 +00002970 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002971
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002972 /* If the initial value of padlen was such that
2973 * data_len < maclen + padlen + 1, then padlen
2974 * got reset to 1, and the initial check
2975 * data_len >= minlen + maclen + 1
2976 * guarantees that at this point we still
2977 * have at least data_len >= maclen.
2978 *
2979 * If the initial value of padlen was such that
2980 * data_len >= maclen + padlen + 1, then we have
2981 * subtracted either padlen + 1 (if the padding was correct)
2982 * or 0 (if the padding was incorrect) since then,
2983 * hence data_len >= maclen in any case.
2984 */
2985 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986
Hanno Beckercab87e62019-04-29 13:52:53 +01002987 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002989#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002991 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002992 ssl_mac( &transform->md_ctx_dec,
2993 transform->mac_dec,
2994 data, rec->data_len,
2995 rec->ctr, rec->type,
2996 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002997 }
2998 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002999#endif /* MBEDTLS_SSL_PROTO_SSL3 */
3000#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
3001 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003002 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003003 {
3004 /*
3005 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02003006 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003007 *
3008 * Known timing attacks:
3009 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
3010 *
Gilles Peskine20b44082018-05-29 14:06:49 +02003011 * To compensate for different timings for the MAC calculation
3012 * depending on how much padding was removed (which is determined
3013 * by padlen), process extra_run more blocks through the hash
3014 * function.
3015 *
3016 * The formula in the paper is
3017 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3018 * where L1 is the size of the header plus the decrypted message
3019 * plus CBC padding and L2 is the size of the header plus the
3020 * decrypted message. This is for an underlying hash function
3021 * with 64-byte blocks.
3022 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3023 * correctly. We round down instead of up, so -56 is the correct
3024 * value for our calculations instead of -55.
3025 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003026 * Repeat the formula rather than defining a block_size variable.
3027 * This avoids requiring division by a variable at runtime
3028 * (which would be marginally less efficient and would require
3029 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003030 */
3031 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003032 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003033
3034 /*
3035 * The next two sizes are the minimum and maximum values of
3036 * in_msglen over all padlen values.
3037 *
3038 * They're independent of padlen, since we previously did
3039 * in_msglen -= padlen.
3040 *
3041 * Note that max_len + maclen is never more than the buffer
3042 * length, as we previously did in_msglen -= maclen too.
3043 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003044 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003045 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3046
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003047 memset( tmp, 0, sizeof( tmp ) );
3048
3049 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003050 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003051#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3052 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003053 case MBEDTLS_MD_MD5:
3054 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003055 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003056 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003057 extra_run =
3058 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3059 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003060 break;
3061#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003062#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003063 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003064 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003065 extra_run =
3066 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3067 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003068 break;
3069#endif
3070 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003071 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003072 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3073 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003074
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003075 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003076
Hanno Beckercab87e62019-04-29 13:52:53 +01003077 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3078 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3080 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003081 /* Make sure we access everything even when padlen > 0. This
3082 * makes the synchronisation requirements for just-in-time
3083 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 ssl_read_memory( data + rec->data_len, padlen );
3085 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003086
3087 /* Call mbedtls_md_process at least once due to cache attacks
3088 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003089 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003090 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003091
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003092 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003093
3094 /* Make sure we access all the memory that could contain the MAC,
3095 * before we check it in the next code block. This makes the
3096 * synchronisation requirements for just-in-time Prime+Probe
3097 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003098 ssl_read_memory( data + min_len,
3099 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 }
3101 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3103 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3106 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003107 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003108
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003109#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003110 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3111 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003112#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003113
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003114 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3115 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003117#if defined(MBEDTLS_SSL_DEBUG_ALL)
3118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003119#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003120 correct = 0;
3121 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003122 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003123 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003124
3125 /*
3126 * Finally check the correct flag
3127 */
3128 if( correct == 0 )
3129 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003130#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003131
3132 /* Make extra sure authentication was performed, exactly once */
3133 if( auth_done != 1 )
3134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3136 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003137 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
Hanno Beckera0e20d02019-05-15 14:03:01 +01003139#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003140 if( rec->cid_len != 0 )
3141 {
3142 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3143 &rec->type );
3144 if( ret != 0 )
3145 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3146 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003150
3151 return( 0 );
3152}
3153
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003154#undef MAC_NONE
3155#undef MAC_PLAINTEXT
3156#undef MAC_CIPHERTEXT
3157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159/*
3160 * Compression/decompression functions
3161 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003162static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163{
3164 int ret;
3165 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003166 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003168 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003170 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003171
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003172 if( len_pre == 0 )
3173 return( 0 );
3174
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175 memcpy( msg_pre, ssl->out_msg, len_pre );
3176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003178 ssl->out_msglen ) );
3179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003180 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181 ssl->out_msg, ssl->out_msglen );
3182
Paul Bakker48916f92012-09-16 19:57:18 +00003183 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3184 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3185 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003186 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187
Paul Bakker48916f92012-09-16 19:57:18 +00003188 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189 if( ret != Z_OK )
3190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3192 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193 }
3194
Angus Grattond8213d02016-05-25 20:56:48 +10003195 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003196 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003199 ssl->out_msglen ) );
3200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 ssl->out_msg, ssl->out_msglen );
3203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205
3206 return( 0 );
3207}
3208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210{
3211 int ret;
3212 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003213 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003215 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003217 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003218
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003219 if( len_pre == 0 )
3220 return( 0 );
3221
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222 memcpy( msg_pre, ssl->in_msg, len_pre );
3223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003224 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 ssl->in_msglen ) );
3226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228 ssl->in_msg, ssl->in_msglen );
3229
Paul Bakker48916f92012-09-16 19:57:18 +00003230 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3231 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3232 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003233 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003234 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003235
Paul Bakker48916f92012-09-16 19:57:18 +00003236 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237 if( ret != Z_OK )
3238 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3240 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003241 }
3242
Angus Grattond8213d02016-05-25 20:56:48 +10003243 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003244 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003247 ssl->in_msglen ) );
3248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003250 ssl->in_msg, ssl->in_msglen );
3251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003253
3254 return( 0 );
3255}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003256#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003258#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3259static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003261#if defined(MBEDTLS_SSL_PROTO_DTLS)
3262static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003263{
3264 /* If renegotiation is not enforced, retransmit until we would reach max
3265 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003266 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003267 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003268 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003269 unsigned char doublings = 1;
3270
3271 while( ratio != 0 )
3272 {
3273 ++doublings;
3274 ratio >>= 1;
3275 }
3276
3277 if( ++ssl->renego_records_seen > doublings )
3278 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003280 return( 0 );
3281 }
3282 }
3283
3284 return( ssl_write_hello_request( ssl ) );
3285}
3286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003287#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003288
Paul Bakker5121ce52009-01-03 21:22:43 +00003289/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003290 * Fill the input message buffer by appending data to it.
3291 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003292 *
3293 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3294 * available (from this read and/or a previous one). Otherwise, an error code
3295 * is returned (possibly EOF or WANT_READ).
3296 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003297 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3298 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3299 * since we always read a whole datagram at once.
3300 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003301 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003302 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003303 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003305{
Paul Bakker23986e52011-04-24 08:57:21 +00003306 int ret;
3307 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003309 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003310
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003311 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003314 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003316 }
3317
Angus Grattond8213d02016-05-25 20:56:48 +10003318 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003322 }
3323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003325 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003326 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003327 uint32_t timeout;
3328
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003329 /* Just to be sure */
3330 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3331 {
3332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3333 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3334 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3335 }
3336
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003337 /*
3338 * The point is, we need to always read a full datagram at once, so we
3339 * sometimes read more then requested, and handle the additional data.
3340 * It could be the rest of the current record (while fetching the
3341 * header) and/or some other records in the same datagram.
3342 */
3343
3344 /*
3345 * Move to the next record in the already read datagram if applicable
3346 */
3347 if( ssl->next_record_offset != 0 )
3348 {
3349 if( ssl->in_left < ssl->next_record_offset )
3350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3352 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003353 }
3354
3355 ssl->in_left -= ssl->next_record_offset;
3356
3357 if( ssl->in_left != 0 )
3358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003359 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003360 ssl->next_record_offset ) );
3361 memmove( ssl->in_hdr,
3362 ssl->in_hdr + ssl->next_record_offset,
3363 ssl->in_left );
3364 }
3365
3366 ssl->next_record_offset = 0;
3367 }
3368
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003370 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003371
3372 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003373 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003374 */
3375 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003376 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003377 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003378 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003379 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003380
3381 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003382 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003383 * are not at the beginning of a new record, the caller did something
3384 * wrong.
3385 */
3386 if( ssl->in_left != 0 )
3387 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003390 }
3391
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392 /*
3393 * Don't even try to read if time's out already.
3394 * This avoids by-passing the timer when repeatedly receiving messages
3395 * that will end up being dropped.
3396 */
3397 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003398 {
3399 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003400 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003401 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003402 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003403 {
Angus Grattond8213d02016-05-25 20:56:48 +10003404 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407 timeout = ssl->handshake->retransmit_timeout;
3408 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003409 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003412
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003413 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003414 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3415 timeout );
3416 else
3417 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003420
3421 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003423 }
3424
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003425 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003428 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003431 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003432 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3433 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003435 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003436 }
3437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003441 return( ret );
3442 }
3443
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003444 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003447 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003448 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003449 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003450 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003453 return( ret );
3454 }
3455
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003456 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003457 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003458#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003459 }
3460
Paul Bakker5121ce52009-01-03 21:22:43 +00003461 if( ret < 0 )
3462 return( ret );
3463
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003464 ssl->in_left = ret;
3465 }
3466 else
3467#endif
3468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003470 ssl->in_left, nb_want ) );
3471
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003472 while( ssl->in_left < nb_want )
3473 {
3474 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003475
3476 if( ssl_check_timer( ssl ) != 0 )
3477 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3478 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003479 {
3480 if( ssl->f_recv_timeout != NULL )
3481 {
3482 ret = ssl->f_recv_timeout( ssl->p_bio,
3483 ssl->in_hdr + ssl->in_left, len,
3484 ssl->conf->read_timeout );
3485 }
3486 else
3487 {
3488 ret = ssl->f_recv( ssl->p_bio,
3489 ssl->in_hdr + ssl->in_left, len );
3490 }
3491 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003494 ssl->in_left, nb_want ) );
3495 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003496
3497 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003498 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003499
3500 if( ret < 0 )
3501 return( ret );
3502
mohammad160352aecb92018-03-28 23:41:40 -07003503 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003504 {
Darryl Green11999bb2018-03-13 15:22:58 +00003505 MBEDTLS_SSL_DEBUG_MSG( 1,
3506 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003507 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003508 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3509 }
3510
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003511 ssl->in_left += ret;
3512 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003513 }
3514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
3517 return( 0 );
3518}
3519
3520/*
3521 * Flush any data not yet written
3522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003523int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003524{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003525 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003526 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003527
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003530 if( ssl->f_send == NULL )
3531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003533 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003535 }
3536
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003537 /* Avoid incrementing counter if data is flushed */
3538 if( ssl->out_left == 0 )
3539 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003541 return( 0 );
3542 }
3543
Paul Bakker5121ce52009-01-03 21:22:43 +00003544 while( ssl->out_left > 0 )
3545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003546 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003547 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003548
Hanno Becker2b1e3542018-08-06 11:19:13 +01003549 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003550 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003552 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003553
3554 if( ret <= 0 )
3555 return( ret );
3556
mohammad160352aecb92018-03-28 23:41:40 -07003557 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003558 {
Darryl Green11999bb2018-03-13 15:22:58 +00003559 MBEDTLS_SSL_DEBUG_MSG( 1,
3560 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003561 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3563 }
3564
Paul Bakker5121ce52009-01-03 21:22:43 +00003565 ssl->out_left -= ret;
3566 }
3567
Hanno Becker2b1e3542018-08-06 11:19:13 +01003568#if defined(MBEDTLS_SSL_PROTO_DTLS)
3569 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003570 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003571 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003572 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003573 else
3574#endif
3575 {
3576 ssl->out_hdr = ssl->out_buf + 8;
3577 }
3578 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003581
3582 return( 0 );
3583}
3584
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003585/*
3586 * Functions to handle the DTLS retransmission state machine
3587 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003588#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589/*
3590 * Append current handshake message to current outgoing flight
3591 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003592static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3596 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3597 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598
3599 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003600 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003601 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003602 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003603 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003604 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 }
3606
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003607 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003608 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003609 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003610 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003611 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 }
3613
3614 /* Copy current handshake message with headers */
3615 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3616 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003617 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618 msg->next = NULL;
3619
3620 /* Append to the current flight */
3621 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003622 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623 else
3624 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626 while( cur->next != NULL )
3627 cur = cur->next;
3628 cur->next = msg;
3629 }
3630
Hanno Becker3b235902018-08-06 09:54:53 +01003631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003632 return( 0 );
3633}
3634
3635/*
3636 * Free the current flight of handshake messages
3637 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003639{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640 mbedtls_ssl_flight_item *cur = flight;
3641 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642
3643 while( cur != NULL )
3644 {
3645 next = cur->next;
3646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 mbedtls_free( cur->p );
3648 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003649
3650 cur = next;
3651 }
3652}
3653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3655static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003656#endif
3657
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003658/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003659 * Swap transform_out and out_ctr with the alternative ones
3660 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003661static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003663 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003664 unsigned char tmp_out_ctr[8];
3665
3666 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3667 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003669 return;
3670 }
3671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003672 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003673
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003674 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003675 tmp_transform = ssl->transform_out;
3676 ssl->transform_out = ssl->handshake->alt_transform_out;
3677 ssl->handshake->alt_transform_out = tmp_transform;
3678
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003679 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003680 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3681 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003682 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003683
3684 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003685 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003687#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3688 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003690 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003692 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3693 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003694 }
3695 }
3696#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003697}
3698
3699/*
3700 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003701 */
3702int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3703{
3704 int ret = 0;
3705
3706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3707
3708 ret = mbedtls_ssl_flight_transmit( ssl );
3709
3710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3711
3712 return( ret );
3713}
3714
3715/*
3716 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003717 *
3718 * Need to remember the current message in case flush_output returns
3719 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003720 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003722int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003724 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003727 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003728 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003730
3731 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003732 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003733 ssl_swap_epochs( ssl );
3734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003735 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003736 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003737
3738 while( ssl->handshake->cur_msg != NULL )
3739 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003740 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003741 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003742
Hanno Beckere1dcb032018-08-17 16:47:58 +01003743 int const is_finished =
3744 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3745 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3746
Hanno Becker04da1892018-08-14 13:22:10 +01003747 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3748 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3749
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003750 /* Swap epochs before sending Finished: we can't do it after
3751 * sending ChangeCipherSpec, in case write returns WANT_READ.
3752 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003753 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003754 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003756 ssl_swap_epochs( ssl );
3757 }
3758
Hanno Becker67bc7c32018-08-06 11:33:50 +01003759 ret = ssl_get_remaining_payload_in_datagram( ssl );
3760 if( ret < 0 )
3761 return( ret );
3762 max_frag_len = (size_t) ret;
3763
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003764 /* CCS is copied as is, while HS messages may need fragmentation */
3765 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3766 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003767 if( max_frag_len == 0 )
3768 {
3769 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3770 return( ret );
3771
3772 continue;
3773 }
3774
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003775 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003776 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003777 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003778
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003779 /* Update position inside current message */
3780 ssl->handshake->cur_msg_p += cur->len;
3781 }
3782 else
3783 {
3784 const unsigned char * const p = ssl->handshake->cur_msg_p;
3785 const size_t hs_len = cur->len - 12;
3786 const size_t frag_off = p - ( cur->p + 12 );
3787 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003788 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003789
Hanno Beckere1dcb032018-08-17 16:47:58 +01003790 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003791 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003792 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003793 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003794
Hanno Becker67bc7c32018-08-06 11:33:50 +01003795 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3796 return( ret );
3797
3798 continue;
3799 }
3800 max_hs_frag_len = max_frag_len - 12;
3801
3802 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3803 max_hs_frag_len : rem_len;
3804
3805 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003806 {
3807 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 (unsigned) cur_hs_frag_len,
3809 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003810 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003811
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003812 /* Messages are stored with handshake headers as if not fragmented,
3813 * copy beginning of headers then fill fragmentation fields.
3814 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3815 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003816
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003817 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3818 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3819 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3820
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3822 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3823 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003824
3825 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3826
Hanno Becker3f7b9732018-08-28 09:53:25 +01003827 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003828 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3829 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003830 ssl->out_msgtype = cur->type;
3831
3832 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003833 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003834 }
3835
3836 /* If done with the current message move to the next one if any */
3837 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3838 {
3839 if( cur->next != NULL )
3840 {
3841 ssl->handshake->cur_msg = cur->next;
3842 ssl->handshake->cur_msg_p = cur->next->p + 12;
3843 }
3844 else
3845 {
3846 ssl->handshake->cur_msg = NULL;
3847 ssl->handshake->cur_msg_p = NULL;
3848 }
3849 }
3850
3851 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003852 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003855 return( ret );
3856 }
3857 }
3858
Hanno Becker67bc7c32018-08-06 11:33:50 +01003859 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3860 return( ret );
3861
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003862 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3864 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003865 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003868 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3869 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003870
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003872
3873 return( 0 );
3874}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003875
3876/*
3877 * To be called when the last message of an incoming flight is received.
3878 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003880{
3881 /* We won't need to resend that one any more */
3882 ssl_flight_free( ssl->handshake->flight );
3883 ssl->handshake->flight = NULL;
3884 ssl->handshake->cur_msg = NULL;
3885
3886 /* The next incoming flight will start with this msg_seq */
3887 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3888
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003889 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003890 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003891
Hanno Becker0271f962018-08-16 13:23:47 +01003892 /* Clear future message buffering structure. */
3893 ssl_buffering_free( ssl );
3894
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003895 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003896 ssl_set_timer( ssl, 0 );
3897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3899 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003902 }
3903 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003905}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003906
3907/*
3908 * To be called when the last message of an outgoing flight is send.
3909 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003911{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003912 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003913 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3916 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003919 }
3920 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003921 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003922}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003924
Paul Bakker5121ce52009-01-03 21:22:43 +00003925/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003927 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003928
3929/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003930 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003931 *
3932 * - fill in handshake headers
3933 * - update handshake checksum
3934 * - DTLS: save message for resending
3935 * - then pass to the record layer
3936 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003937 * DTLS: except for HelloRequest, messages are only queued, and will only be
3938 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003939 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003940 * Inputs:
3941 * - ssl->out_msglen: 4 + actual handshake message len
3942 * (4 is the size of handshake headers for TLS)
3943 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3944 * - ssl->out_msg + 4: the handshake message body
3945 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003946 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003947 * - ssl->out_msglen: the length of the record contents
3948 * (including handshake headers but excluding record headers)
3949 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003950 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003951int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003952{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003953 int ret;
3954 const size_t hs_len = ssl->out_msglen - 4;
3955 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003956
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3958
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003959 /*
3960 * Sanity checks
3961 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003962 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003963 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3964 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003965 /* In SSLv3, the client might send a NoCertificate alert. */
3966#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3967 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3968 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3969 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3970#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3971 {
3972 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3973 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3974 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003975 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003976
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003977 /* Whenever we send anything different from a
3978 * HelloRequest we should be in a handshake - double check. */
3979 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3980 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003981 ssl->handshake == NULL )
3982 {
3983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3984 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3985 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003989 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003991 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003992 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3993 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003994 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003995#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003996
Hanno Beckerb50a2532018-08-06 11:52:54 +01003997 /* Double-check that we did not exceed the bounds
3998 * of the outgoing record buffer.
3999 * This should never fail as the various message
4000 * writing functions must obey the bounds of the
4001 * outgoing record buffer, but better be safe.
4002 *
4003 * Note: We deliberately do not check for the MTU or MFL here.
4004 */
4005 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
4006 {
4007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
4008 "size %u, maximum %u",
4009 (unsigned) ssl->out_msglen,
4010 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
4011 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4012 }
4013
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004014 /*
4015 * Fill handshake headers
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004018 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4020 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4021 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004022
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004023 /*
4024 * DTLS has additional fields in the Handshake layer,
4025 * between the length field and the actual payload:
4026 * uint16 message_seq;
4027 * uint24 fragment_offset;
4028 * uint24 fragment_length;
4029 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004030#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004031 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004033 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004034 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004035 {
4036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4037 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004038 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004039 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004040 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4041 }
4042
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004043 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004044 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004045
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004046 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004047 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004048 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004049 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4050 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4051 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004052 }
4053 else
4054 {
4055 ssl->out_msg[4] = 0;
4056 ssl->out_msg[5] = 0;
4057 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004058
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004059 /* Handshake hashes are computed without fragmentation,
4060 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004061 memset( ssl->out_msg + 6, 0x00, 3 );
4062 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004063 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004065
Hanno Becker0207e532018-08-28 10:28:28 +01004066 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004067 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4068 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004069 }
4070
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004074 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4075 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004076 {
4077 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4078 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004079 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004080 return( ret );
4081 }
4082 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004083 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004084#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004085 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004086 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004087 {
4088 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4089 return( ret );
4090 }
4091 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004092
4093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4094
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004095 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004096}
4097
4098/*
4099 * Record layer functions
4100 */
4101
4102/*
4103 * Write current record.
4104 *
4105 * Uses:
4106 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4107 * - ssl->out_msglen: length of the record content (excl headers)
4108 * - ssl->out_msg: record content
4109 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004110int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004111{
4112 int ret, done = 0;
4113 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004114 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004115
4116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004119 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004121 {
4122 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004125 return( ret );
4126 }
4127
4128 len = ssl->out_msglen;
4129 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004130#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004132#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4133 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 ret = mbedtls_ssl_hw_record_write( ssl );
4138 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4141 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004142 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004143
4144 if( ret == 0 )
4145 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004146 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004147#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004148 if( !done )
4149 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004150 unsigned i;
4151 size_t protected_record_size;
4152
Hanno Becker6430faf2019-05-08 11:57:13 +01004153 /* Skip writing the record content type to after the encryption,
4154 * as it may change when using the CID extension. */
4155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004156 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004157 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004158
Hanno Becker19859472018-08-06 09:40:20 +01004159 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004160 ssl->out_len[0] = (unsigned char)( len >> 8 );
4161 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004162
Paul Bakker48916f92012-09-16 19:57:18 +00004163 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004164 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004165 mbedtls_record rec;
4166
4167 rec.buf = ssl->out_iv;
4168 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4169 ( ssl->out_iv - ssl->out_buf );
4170 rec.data_len = ssl->out_msglen;
4171 rec.data_offset = ssl->out_msg - rec.buf;
4172
4173 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4174 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4175 ssl->conf->transport, rec.ver );
4176 rec.type = ssl->out_msgtype;
4177
Hanno Beckera0e20d02019-05-15 14:03:01 +01004178#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004179 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004180 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004181#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004182
Hanno Beckera18d1322018-01-03 14:27:32 +00004183 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004184 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004185 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004186 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004187 return( ret );
4188 }
4189
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004190 if( rec.data_offset != 0 )
4191 {
4192 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4193 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4194 }
4195
Hanno Becker6430faf2019-05-08 11:57:13 +01004196 /* Update the record content type and CID. */
4197 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004198#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004199 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004201 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004202 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4203 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004204 }
4205
Hanno Becker5903de42019-05-03 14:46:38 +01004206 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004207
4208#if defined(MBEDTLS_SSL_PROTO_DTLS)
4209 /* In case of DTLS, double-check that we don't exceed
4210 * the remaining space in the datagram. */
4211 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4212 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004213 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004214 if( ret < 0 )
4215 return( ret );
4216
4217 if( protected_record_size > (size_t) ret )
4218 {
4219 /* Should never happen */
4220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4221 }
4222 }
4223#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004224
Hanno Becker6430faf2019-05-08 11:57:13 +01004225 /* Now write the potentially updated record content type. */
4226 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004229 "version = [%d:%d], msglen = %d",
4230 ssl->out_hdr[0], ssl->out_hdr[1],
4231 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004233 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004234 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004235
4236 ssl->out_left += protected_record_size;
4237 ssl->out_hdr += protected_record_size;
4238 ssl_update_out_pointers( ssl, ssl->transform_out );
4239
Hanno Becker04484622018-08-06 09:49:38 +01004240 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4241 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4242 break;
4243
4244 /* The loop goes to its end iff the counter is wrapping */
4245 if( i == ssl_ep_len( ssl ) )
4246 {
4247 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4248 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4249 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 }
4251
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004253 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4254 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004255 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004256 size_t remaining;
4257 ret = ssl_get_remaining_payload_in_datagram( ssl );
4258 if( ret < 0 )
4259 {
4260 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4261 ret );
4262 return( ret );
4263 }
4264
4265 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004266 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004267 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004268 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004269 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004270 else
4271 {
Hanno Becker513815a2018-08-20 11:56:09 +01004272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004273 }
4274 }
4275#endif /* MBEDTLS_SSL_PROTO_DTLS */
4276
4277 if( ( flush == SSL_FORCE_FLUSH ) &&
4278 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004281 return( ret );
4282 }
4283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004285
4286 return( 0 );
4287}
4288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004290
4291static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4292{
4293 if( ssl->in_msglen < ssl->in_hslen ||
4294 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4295 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4296 {
4297 return( 1 );
4298 }
4299 return( 0 );
4300}
Hanno Becker44650b72018-08-16 12:51:11 +01004301
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004302static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004303{
4304 return( ( ssl->in_msg[9] << 16 ) |
4305 ( ssl->in_msg[10] << 8 ) |
4306 ssl->in_msg[11] );
4307}
4308
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004309static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004310{
4311 return( ( ssl->in_msg[6] << 16 ) |
4312 ( ssl->in_msg[7] << 8 ) |
4313 ssl->in_msg[8] );
4314}
4315
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004316static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004317{
4318 uint32_t msg_len, frag_off, frag_len;
4319
4320 msg_len = ssl_get_hs_total_len( ssl );
4321 frag_off = ssl_get_hs_frag_off( ssl );
4322 frag_len = ssl_get_hs_frag_len( ssl );
4323
4324 if( frag_off > msg_len )
4325 return( -1 );
4326
4327 if( frag_len > msg_len - frag_off )
4328 return( -1 );
4329
4330 if( frag_len + 12 > ssl->in_msglen )
4331 return( -1 );
4332
4333 return( 0 );
4334}
4335
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004336/*
4337 * Mark bits in bitmask (used for DTLS HS reassembly)
4338 */
4339static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4340{
4341 unsigned int start_bits, end_bits;
4342
4343 start_bits = 8 - ( offset % 8 );
4344 if( start_bits != 8 )
4345 {
4346 size_t first_byte_idx = offset / 8;
4347
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004348 /* Special case */
4349 if( len <= start_bits )
4350 {
4351 for( ; len != 0; len-- )
4352 mask[first_byte_idx] |= 1 << ( start_bits - len );
4353
4354 /* Avoid potential issues with offset or len becoming invalid */
4355 return;
4356 }
4357
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004358 offset += start_bits; /* Now offset % 8 == 0 */
4359 len -= start_bits;
4360
4361 for( ; start_bits != 0; start_bits-- )
4362 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4363 }
4364
4365 end_bits = len % 8;
4366 if( end_bits != 0 )
4367 {
4368 size_t last_byte_idx = ( offset + len ) / 8;
4369
4370 len -= end_bits; /* Now len % 8 == 0 */
4371
4372 for( ; end_bits != 0; end_bits-- )
4373 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4374 }
4375
4376 memset( mask + offset / 8, 0xFF, len / 8 );
4377}
4378
4379/*
4380 * Check that bitmask is full
4381 */
4382static int ssl_bitmask_check( unsigned char *mask, size_t len )
4383{
4384 size_t i;
4385
4386 for( i = 0; i < len / 8; i++ )
4387 if( mask[i] != 0xFF )
4388 return( -1 );
4389
4390 for( i = 0; i < len % 8; i++ )
4391 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4392 return( -1 );
4393
4394 return( 0 );
4395}
4396
Hanno Becker56e205e2018-08-16 09:06:12 +01004397/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004398static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004399 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004400{
Hanno Becker56e205e2018-08-16 09:06:12 +01004401 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004402
Hanno Becker56e205e2018-08-16 09:06:12 +01004403 alloc_len = 12; /* Handshake header */
4404 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004405
Hanno Beckerd07df862018-08-16 09:14:58 +01004406 if( add_bitmap )
4407 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004408
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004409 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004410}
Hanno Becker56e205e2018-08-16 09:06:12 +01004411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004413
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004414static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004415{
4416 return( ( ssl->in_msg[1] << 16 ) |
4417 ( ssl->in_msg[2] << 8 ) |
4418 ssl->in_msg[3] );
4419}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004420
Simon Butcher99000142016-10-13 17:21:01 +01004421int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004422{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004424 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004426 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004427 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004428 }
4429
Hanno Becker12555c62018-08-16 12:47:53 +01004430 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004432 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004433 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004434 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004436#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004437 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004438 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004439 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004440 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004441
Hanno Becker44650b72018-08-16 12:51:11 +01004442 if( ssl_check_hs_header( ssl ) != 0 )
4443 {
4444 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4445 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4446 }
4447
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004448 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004449 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4450 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4451 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4452 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004453 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004454 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4455 {
4456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4457 recv_msg_seq,
4458 ssl->handshake->in_msg_seq ) );
4459 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4460 }
4461
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004462 /* Retransmit only on last message from previous flight, to avoid
4463 * too many retransmissions.
4464 * Besides, No sane server ever retransmits HelloVerifyRequest */
4465 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004466 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004469 "message_seq = %d, start_of_flight = %d",
4470 recv_msg_seq,
4471 ssl->handshake->in_flight_start_seq ) );
4472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004474 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004475 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004476 return( ret );
4477 }
4478 }
4479 else
4480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004482 "message_seq = %d, expected = %d",
4483 recv_msg_seq,
4484 ssl->handshake->in_msg_seq ) );
4485 }
4486
Hanno Becker90333da2017-10-10 11:27:13 +01004487 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004488 }
4489 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004490
Hanno Becker6d97ef52018-08-16 13:09:04 +01004491 /* Message reassembly is handled alongside buffering of future
4492 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004493 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004494 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004495 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004496 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004498 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004499 }
4500 }
4501 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004502#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004503 /* With TLS we don't handle fragmentation (for now) */
4504 if( ssl->in_msglen < ssl->in_hslen )
4505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4507 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004508 }
4509
Simon Butcher99000142016-10-13 17:21:01 +01004510 return( 0 );
4511}
4512
4513void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4514{
Hanno Becker0271f962018-08-16 13:23:47 +01004515 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004516
Hanno Becker0271f962018-08-16 13:23:47 +01004517 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004518 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004519 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004520 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004521
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004522 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004523#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004524 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004525 ssl->handshake != NULL )
4526 {
Hanno Becker0271f962018-08-16 13:23:47 +01004527 unsigned offset;
4528 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004529
Hanno Becker0271f962018-08-16 13:23:47 +01004530 /* Increment handshake sequence number */
4531 hs->in_msg_seq++;
4532
4533 /*
4534 * Clear up handshake buffering and reassembly structure.
4535 */
4536
4537 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004538 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004539
4540 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004541 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4542 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004543 offset++, hs_buf++ )
4544 {
4545 *hs_buf = *(hs_buf + 1);
4546 }
4547
4548 /* Create a fresh last entry */
4549 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004550 }
4551#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004552}
4553
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004554/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004555 * DTLS anti-replay: RFC 6347 4.1.2.6
4556 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004557 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4558 * Bit n is set iff record number in_window_top - n has been seen.
4559 *
4560 * Usually, in_window_top is the last record number seen and the lsb of
4561 * in_window is set. The only exception is the initial state (record number 0
4562 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004563 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004564#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4565static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004566{
4567 ssl->in_window_top = 0;
4568 ssl->in_window = 0;
4569}
4570
4571static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4572{
4573 return( ( (uint64_t) buf[0] << 40 ) |
4574 ( (uint64_t) buf[1] << 32 ) |
4575 ( (uint64_t) buf[2] << 24 ) |
4576 ( (uint64_t) buf[3] << 16 ) |
4577 ( (uint64_t) buf[4] << 8 ) |
4578 ( (uint64_t) buf[5] ) );
4579}
4580
4581/*
4582 * Return 0 if sequence number is acceptable, -1 otherwise
4583 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004584int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004585{
4586 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4587 uint64_t bit;
4588
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004589 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004590 return( 0 );
4591
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004592 if( rec_seqnum > ssl->in_window_top )
4593 return( 0 );
4594
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004595 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596
4597 if( bit >= 64 )
4598 return( -1 );
4599
4600 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4601 return( -1 );
4602
4603 return( 0 );
4604}
4605
4606/*
4607 * Update replay window on new validated record
4608 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004609void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004610{
4611 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4612
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004613 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004614 return;
4615
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004616 if( rec_seqnum > ssl->in_window_top )
4617 {
4618 /* Update window_top and the contents of the window */
4619 uint64_t shift = rec_seqnum - ssl->in_window_top;
4620
4621 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004622 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004623 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004624 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004625 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004626 ssl->in_window |= 1;
4627 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004628
4629 ssl->in_window_top = rec_seqnum;
4630 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004631 else
4632 {
4633 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004634 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004635
4636 if( bit < 64 ) /* Always true, but be extra sure */
4637 ssl->in_window |= (uint64_t) 1 << bit;
4638 }
4639}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004640#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004641
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004642#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004643/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004644static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4645
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004646/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004647 * Without any SSL context, check if a datagram looks like a ClientHello with
4648 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004649 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004650 *
4651 * - if cookie is valid, return 0
4652 * - if ClientHello looks superficially valid but cookie is not,
4653 * fill obuf and set olen, then
4654 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4655 * - otherwise return a specific error code
4656 */
4657static int ssl_check_dtls_clihlo_cookie(
4658 mbedtls_ssl_cookie_write_t *f_cookie_write,
4659 mbedtls_ssl_cookie_check_t *f_cookie_check,
4660 void *p_cookie,
4661 const unsigned char *cli_id, size_t cli_id_len,
4662 const unsigned char *in, size_t in_len,
4663 unsigned char *obuf, size_t buf_len, size_t *olen )
4664{
4665 size_t sid_len, cookie_len;
4666 unsigned char *p;
4667
4668 if( f_cookie_write == NULL || f_cookie_check == NULL )
4669 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4670
4671 /*
4672 * Structure of ClientHello with record and handshake headers,
4673 * and expected values. We don't need to check a lot, more checks will be
4674 * done when actually parsing the ClientHello - skipping those checks
4675 * avoids code duplication and does not make cookie forging any easier.
4676 *
4677 * 0-0 ContentType type; copied, must be handshake
4678 * 1-2 ProtocolVersion version; copied
4679 * 3-4 uint16 epoch; copied, must be 0
4680 * 5-10 uint48 sequence_number; copied
4681 * 11-12 uint16 length; (ignored)
4682 *
4683 * 13-13 HandshakeType msg_type; (ignored)
4684 * 14-16 uint24 length; (ignored)
4685 * 17-18 uint16 message_seq; copied
4686 * 19-21 uint24 fragment_offset; copied, must be 0
4687 * 22-24 uint24 fragment_length; (ignored)
4688 *
4689 * 25-26 ProtocolVersion client_version; (ignored)
4690 * 27-58 Random random; (ignored)
4691 * 59-xx SessionID session_id; 1 byte len + sid_len content
4692 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4693 * ...
4694 *
4695 * Minimum length is 61 bytes.
4696 */
4697 if( in_len < 61 ||
4698 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4699 in[3] != 0 || in[4] != 0 ||
4700 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4701 {
4702 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4703 }
4704
4705 sid_len = in[59];
4706 if( sid_len > in_len - 61 )
4707 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4708
4709 cookie_len = in[60 + sid_len];
4710 if( cookie_len > in_len - 60 )
4711 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4712
4713 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4714 cli_id, cli_id_len ) == 0 )
4715 {
4716 /* Valid cookie */
4717 return( 0 );
4718 }
4719
4720 /*
4721 * If we get here, we've got an invalid cookie, let's prepare HVR.
4722 *
4723 * 0-0 ContentType type; copied
4724 * 1-2 ProtocolVersion version; copied
4725 * 3-4 uint16 epoch; copied
4726 * 5-10 uint48 sequence_number; copied
4727 * 11-12 uint16 length; olen - 13
4728 *
4729 * 13-13 HandshakeType msg_type; hello_verify_request
4730 * 14-16 uint24 length; olen - 25
4731 * 17-18 uint16 message_seq; copied
4732 * 19-21 uint24 fragment_offset; copied
4733 * 22-24 uint24 fragment_length; olen - 25
4734 *
4735 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4736 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4737 *
4738 * Minimum length is 28.
4739 */
4740 if( buf_len < 28 )
4741 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4742
4743 /* Copy most fields and adapt others */
4744 memcpy( obuf, in, 25 );
4745 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4746 obuf[25] = 0xfe;
4747 obuf[26] = 0xff;
4748
4749 /* Generate and write actual cookie */
4750 p = obuf + 28;
4751 if( f_cookie_write( p_cookie,
4752 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4753 {
4754 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4755 }
4756
4757 *olen = p - obuf;
4758
4759 /* Go back and fill length fields */
4760 obuf[27] = (unsigned char)( *olen - 28 );
4761
4762 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4763 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4764 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4765
4766 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4767 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4768
4769 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4770}
4771
4772/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004773 * Handle possible client reconnect with the same UDP quadruplet
4774 * (RFC 6347 Section 4.2.8).
4775 *
4776 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4777 * that looks like a ClientHello.
4778 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779 * - if the input looks like a ClientHello without cookies,
4780 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782 * - if the input looks like a ClientHello with a valid cookie,
4783 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004784 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004785 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004786 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004787 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004788 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4789 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790 */
4791static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4792{
4793 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004794 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004795
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004796 ret = ssl_check_dtls_clihlo_cookie(
4797 ssl->conf->f_cookie_write,
4798 ssl->conf->f_cookie_check,
4799 ssl->conf->p_cookie,
4800 ssl->cli_id, ssl->cli_id_len,
4801 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004802 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004803
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004804 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4805
4806 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004808 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004809 * If the error is permanent we'll catch it later,
4810 * if it's not, then hopefully it'll work next time. */
4811 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4812
4813 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004814 }
4815
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004816 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004817 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004818 /* Got a valid cookie, partially reset context */
4819 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4820 {
4821 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4822 return( ret );
4823 }
4824
4825 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004826 }
4827
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004828 return( ret );
4829}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004830#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004831
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004832static int ssl_check_record_type( uint8_t record_type )
4833{
4834 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4835 record_type != MBEDTLS_SSL_MSG_ALERT &&
4836 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4837 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4838 {
4839 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4840 }
4841
4842 return( 0 );
4843}
4844
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004845/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004846 * ContentType type;
4847 * ProtocolVersion version;
4848 * uint16 epoch; // DTLS only
4849 * uint48 sequence_number; // DTLS only
4850 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004851 *
4852 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004853 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004854 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4855 *
4856 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004857 * 1. proceed with the record if this function returns 0
4858 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4859 * 3. return CLIENT_RECONNECT if this function return that value
4860 * 4. drop the whole datagram if this function returns anything else.
4861 * Point 2 is needed when the peer is resending, and we have already received
4862 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004863 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004864static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004865{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004866 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004867 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004868
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004869 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004870
Paul Bakker5121ce52009-01-03 21:22:43 +00004871 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004872 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004873
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004874 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004875#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4877 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4878 ssl->conf->cid_len != 0 )
4879 {
4880 /* Shift pointers to account for record header including CID
4881 * struct {
4882 * ContentType special_type = tls12_cid;
4883 * ProtocolVersion version;
4884 * uint16 epoch;
4885 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004886 * opaque cid[cid_length]; // Additional field compared to
4887 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004888 * uint16 length;
4889 * opaque enc_content[DTLSCiphertext.length];
4890 * } DTLSCiphertext;
4891 */
4892
4893 /* So far, we only support static CID lengths
4894 * fixed in the configuration. */
4895 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4896 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4897 }
4898 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004899#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004900 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004903
4904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004905 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4906 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004907 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4908#endif /* MBEDTLS_SSL_PROTO_DTLS */
4909 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4910 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004913 }
4914
4915 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004916 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4919 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004920 }
4921
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004922 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4925 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004926 }
4927
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004928 /* Now that the total length of the record header is known, ensure
4929 * that the current datagram is large enough to hold it.
4930 * This would fail, for example, if we received a datagram of
4931 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4932 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4933 if( ret != 0 )
4934 {
4935 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4936 return( ret );
4937 }
4938 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4939
4940 /* Parse and validate record length
4941 * This must happen after the CID parsing because
4942 * its position in the record header depends on
4943 * the presence of a CID. */
4944
4945 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004946 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004947 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4950 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004951 }
4952
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004953 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004954 "version = [%d:%d], msglen = %d",
4955 ssl->in_msgtype,
4956 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004957
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004958 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004959 * DTLS-related tests.
4960 * Check epoch before checking length constraint because
4961 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4962 * message gets duplicated before the corresponding Finished message,
4963 * the second ChangeCipherSpec should be discarded because it belongs
4964 * to an old epoch, but not because its length is shorter than
4965 * the minimum record length for packets using the new record transform.
4966 * Note that these two kinds of failures are handled differently,
4967 * as an unexpected record is silently skipped but an invalid
4968 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004969 */
4970#if defined(MBEDTLS_SSL_PROTO_DTLS)
4971 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4972 {
4973 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4974
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004975 /* Check epoch (and sequence number) with DTLS */
4976 if( rec_epoch != ssl->in_epoch )
4977 {
4978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4979 "expected %d, received %d",
4980 ssl->in_epoch, rec_epoch ) );
4981
4982#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4983 /*
4984 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4985 * access the first byte of record content (handshake type), as we
4986 * have an active transform (possibly iv_len != 0), so use the
4987 * fact that the record header len is 13 instead.
4988 */
4989 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4990 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4991 rec_epoch == 0 &&
4992 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4993 ssl->in_left > 13 &&
4994 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4995 {
4996 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4997 "from the same port" ) );
4998 return( ssl_handle_possible_reconnect( ssl ) );
4999 }
5000 else
5001#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01005002 {
5003 /* Consider buffering the record. */
5004 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
5005 {
5006 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
5007 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
5008 }
5009
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005010 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01005011 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005012 }
5013
5014#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5015 /* Replay detection only works for the current epoch */
5016 if( rec_epoch == ssl->in_epoch &&
5017 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5020 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5021 }
5022#endif
5023 }
5024#endif /* MBEDTLS_SSL_PROTO_DTLS */
5025
Hanno Becker52c6dc62017-05-26 16:07:36 +01005026
5027 /* Check length against bounds of the current transform and version */
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005028 if( ssl->transform_in != NULL )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005029 {
5030 if( ssl->in_msglen < ssl->transform_in->minlen )
5031 {
5032 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5033 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5034 }
Hanno Becker52c6dc62017-05-26 16:07:36 +01005035 }
5036
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005037 return( 0 );
5038}
Paul Bakker5121ce52009-01-03 21:22:43 +00005039
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005040/*
5041 * If applicable, decrypt (and decompress) record content
5042 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005043static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005044{
5045 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005048 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005050#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5051 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005053 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055 ret = mbedtls_ssl_hw_record_read( ssl );
5056 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5059 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005060 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005061
5062 if( ret == 0 )
5063 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005064 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005066 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005067 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005068 mbedtls_record rec;
5069
5070 rec.buf = ssl->in_iv;
5071 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5072 - ( ssl->in_iv - ssl->in_buf );
5073 rec.data_len = ssl->in_msglen;
5074 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005075#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005076 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005077 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005078#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005079
5080 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5081 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5082 ssl->conf->transport, rec.ver );
5083 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005084 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5085 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005086 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005087 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005088
Hanno Beckera0e20d02019-05-15 14:03:01 +01005089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005090 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5091 ssl->conf->ignore_unexpected_cid
5092 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5093 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005094 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005095 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005096 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005097#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005098
Paul Bakker5121ce52009-01-03 21:22:43 +00005099 return( ret );
5100 }
5101
Hanno Becker6430faf2019-05-08 11:57:13 +01005102 if( ssl->in_msgtype != rec.type )
5103 {
5104 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5105 ssl->in_msgtype, rec.type ) );
5106 }
5107
5108 /* The record content type may change during decryption,
5109 * so re-read it. */
5110 ssl->in_msgtype = rec.type;
5111 /* Also update the input buffer, because unfortunately
5112 * the server-side ssl_parse_client_hello() reparses the
5113 * record header when receiving a ClientHello initiating
5114 * a renegotiation. */
5115 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005116 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005117 ssl->in_msglen = rec.data_len;
5118 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5119 ssl->in_len[1] = (unsigned char)( rec.data_len );
5120
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005121 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5122 ssl->in_msg, ssl->in_msglen );
5123
Hanno Beckera0e20d02019-05-15 14:03:01 +01005124#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005125 /* We have already checked the record content type
5126 * in ssl_parse_record_header(), failing or silently
5127 * dropping the record in the case of an unknown type.
5128 *
5129 * Since with the use of CIDs, the record content type
5130 * might change during decryption, re-check the record
5131 * content type, but treat a failure as fatal this time. */
5132 if( ssl_check_record_type( ssl->in_msgtype ) )
5133 {
5134 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5135 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5136 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005137#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005138
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005139 if( ssl->in_msglen == 0 )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005140 {
5141#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5142 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5143 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5144 {
5145 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5147 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5148 }
5149#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5150
5151 ssl->nb_zero++;
5152
5153 /*
5154 * Three or more empty messages may be a DoS attack
5155 * (excessive CPU consumption).
5156 */
5157 if( ssl->nb_zero > 3 )
5158 {
5159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005160 "messages, possible DoS attack" ) );
5161 /* Treat the records as if they were not properly authenticated,
5162 * thereby failing the connection if we see more than allowed
5163 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005164 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5165 }
5166 }
5167 else
5168 ssl->nb_zero = 0;
5169
5170#if defined(MBEDTLS_SSL_PROTO_DTLS)
5171 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5172 {
5173 ; /* in_ctr read from peer, not maintained internally */
5174 }
5175 else
5176#endif
5177 {
5178 unsigned i;
5179 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5180 if( ++ssl->in_ctr[i - 1] != 0 )
5181 break;
5182
5183 /* The loop goes to its end iff the counter is wrapping */
5184 if( i == ssl_ep_len( ssl ) )
5185 {
5186 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5187 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5188 }
5189 }
5190
Paul Bakker5121ce52009-01-03 21:22:43 +00005191 }
5192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005193#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005194 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005195 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005196 {
5197 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005199 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005200 return( ret );
5201 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005202 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005203#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005205#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005206 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005208 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005209 }
5210#endif
5211
Hanno Beckerd96e10b2019-07-09 17:30:02 +01005212 /* Check actual (decrypted) record content length against
5213 * configured maximum. */
5214 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
5215 {
5216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5217 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5218 }
5219
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005220 return( 0 );
5221}
5222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005224
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005225/*
5226 * Read a record.
5227 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005228 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5229 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5230 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005231 */
Hanno Becker1097b342018-08-15 14:09:41 +01005232
5233/* Helper functions for mbedtls_ssl_read_record(). */
5234static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005235static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5236static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005237
Hanno Becker327c93b2018-08-15 13:56:18 +01005238int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005239 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005240{
5241 int ret;
5242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005244
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005245 if( ssl->keep_current_message == 0 )
5246 {
5247 do {
Simon Butcher99000142016-10-13 17:21:01 +01005248
Hanno Becker26994592018-08-15 14:14:59 +01005249 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005250 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005251 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005252
Hanno Beckere74d5562018-08-15 14:26:08 +01005253 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005254 {
Hanno Becker40f50842018-08-15 14:48:01 +01005255#if defined(MBEDTLS_SSL_PROTO_DTLS)
5256 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005257
Hanno Becker40f50842018-08-15 14:48:01 +01005258 /* We only check for buffered messages if the
5259 * current datagram is fully consumed. */
5260 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005261 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005262 {
Hanno Becker40f50842018-08-15 14:48:01 +01005263 if( ssl_load_buffered_message( ssl ) == 0 )
5264 have_buffered = 1;
5265 }
5266
5267 if( have_buffered == 0 )
5268#endif /* MBEDTLS_SSL_PROTO_DTLS */
5269 {
5270 ret = ssl_get_next_record( ssl );
5271 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5272 continue;
5273
5274 if( ret != 0 )
5275 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005276 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005277 return( ret );
5278 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005279 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005280 }
5281
5282 ret = mbedtls_ssl_handle_message_type( ssl );
5283
Hanno Becker40f50842018-08-15 14:48:01 +01005284#if defined(MBEDTLS_SSL_PROTO_DTLS)
5285 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5286 {
5287 /* Buffer future message */
5288 ret = ssl_buffer_message( ssl );
5289 if( ret != 0 )
5290 return( ret );
5291
5292 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5293 }
5294#endif /* MBEDTLS_SSL_PROTO_DTLS */
5295
Hanno Becker90333da2017-10-10 11:27:13 +01005296 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5297 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005298
5299 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005300 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005301 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005302 return( ret );
5303 }
5304
Hanno Becker327c93b2018-08-15 13:56:18 +01005305 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005306 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005307 {
5308 mbedtls_ssl_update_handshake_status( ssl );
5309 }
Simon Butcher99000142016-10-13 17:21:01 +01005310 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005311 else
Simon Butcher99000142016-10-13 17:21:01 +01005312 {
Hanno Becker02f59072018-08-15 14:00:24 +01005313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005314 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005315 }
5316
5317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5318
5319 return( 0 );
5320}
5321
Hanno Becker40f50842018-08-15 14:48:01 +01005322#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005323static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005324{
Hanno Becker40f50842018-08-15 14:48:01 +01005325 if( ssl->in_left > ssl->next_record_offset )
5326 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005327
Hanno Becker40f50842018-08-15 14:48:01 +01005328 return( 0 );
5329}
5330
5331static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5332{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005333 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005334 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005335 int ret = 0;
5336
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005337 if( hs == NULL )
5338 return( -1 );
5339
Hanno Beckere00ae372018-08-20 09:39:42 +01005340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5341
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005342 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5343 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5344 {
5345 /* Check if we have seen a ChangeCipherSpec before.
5346 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005347 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005348 {
5349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5350 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005351 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005352 }
5353
Hanno Becker39b8bc92018-08-28 17:17:13 +01005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005355 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5356 ssl->in_msglen = 1;
5357 ssl->in_msg[0] = 1;
5358
5359 /* As long as they are equal, the exact value doesn't matter. */
5360 ssl->in_left = 0;
5361 ssl->next_record_offset = 0;
5362
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005363 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005364 goto exit;
5365 }
Hanno Becker37f95322018-08-16 13:55:32 +01005366
Hanno Beckerb8f50142018-08-28 10:01:34 +01005367#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005368 /* Debug only */
5369 {
5370 unsigned offset;
5371 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5372 {
5373 hs_buf = &hs->buffering.hs[offset];
5374 if( hs_buf->is_valid == 1 )
5375 {
5376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5377 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005378 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005379 }
5380 }
5381 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005382#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005383
5384 /* Check if we have buffered and/or fully reassembled the
5385 * next handshake message. */
5386 hs_buf = &hs->buffering.hs[0];
5387 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5388 {
5389 /* Synthesize a record containing the buffered HS message. */
5390 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5391 ( hs_buf->data[2] << 8 ) |
5392 hs_buf->data[3];
5393
5394 /* Double-check that we haven't accidentally buffered
5395 * a message that doesn't fit into the input buffer. */
5396 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5397 {
5398 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5399 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5400 }
5401
5402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5403 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5404 hs_buf->data, msg_len + 12 );
5405
5406 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5407 ssl->in_hslen = msg_len + 12;
5408 ssl->in_msglen = msg_len + 12;
5409 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5410
5411 ret = 0;
5412 goto exit;
5413 }
5414 else
5415 {
5416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5417 hs->in_msg_seq ) );
5418 }
5419
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005420 ret = -1;
5421
5422exit:
5423
5424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5425 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005426}
5427
Hanno Beckera02b0b42018-08-21 17:20:27 +01005428static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5429 size_t desired )
5430{
5431 int offset;
5432 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5434 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005435
Hanno Becker01315ea2018-08-21 17:22:17 +01005436 /* Get rid of future records epoch first, if such exist. */
5437 ssl_free_buffered_record( ssl );
5438
5439 /* Check if we have enough space available now. */
5440 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5441 hs->buffering.total_bytes_buffered ) )
5442 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005444 return( 0 );
5445 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005446
Hanno Becker4f432ad2018-08-28 10:02:32 +01005447 /* We don't have enough space to buffer the next expected handshake
5448 * message. Remove buffers used for future messages to gain space,
5449 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005450 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5451 offset >= 0; offset-- )
5452 {
5453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5454 offset ) );
5455
Hanno Beckerb309b922018-08-23 13:18:05 +01005456 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005457
5458 /* Check if we have enough space available now. */
5459 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5460 hs->buffering.total_bytes_buffered ) )
5461 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005463 return( 0 );
5464 }
5465 }
5466
5467 return( -1 );
5468}
5469
Hanno Becker40f50842018-08-15 14:48:01 +01005470static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5471{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005472 int ret = 0;
5473 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5474
5475 if( hs == NULL )
5476 return( 0 );
5477
5478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5479
5480 switch( ssl->in_msgtype )
5481 {
5482 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005484
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005485 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005486 break;
5487
5488 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005489 {
5490 unsigned recv_msg_seq_offset;
5491 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5492 mbedtls_ssl_hs_buffer *hs_buf;
5493 size_t msg_len = ssl->in_hslen - 12;
5494
5495 /* We should never receive an old handshake
5496 * message - double-check nonetheless. */
5497 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5498 {
5499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5500 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5501 }
5502
5503 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5504 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5505 {
5506 /* Silently ignore -- message too far in the future */
5507 MBEDTLS_SSL_DEBUG_MSG( 2,
5508 ( "Ignore future HS message with sequence number %u, "
5509 "buffering window %u - %u",
5510 recv_msg_seq, ssl->handshake->in_msg_seq,
5511 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5512
5513 goto exit;
5514 }
5515
5516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5517 recv_msg_seq, recv_msg_seq_offset ) );
5518
5519 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5520
5521 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005522 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005523 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005524 size_t reassembly_buf_sz;
5525
Hanno Becker37f95322018-08-16 13:55:32 +01005526 hs_buf->is_fragmented =
5527 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5528
5529 /* We copy the message back into the input buffer
5530 * after reassembly, so check that it's not too large.
5531 * This is an implementation-specific limitation
5532 * and not one from the standard, hence it is not
5533 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005534 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005535 {
5536 /* Ignore message */
5537 goto exit;
5538 }
5539
Hanno Beckere0b150f2018-08-21 15:51:03 +01005540 /* Check if we have enough space to buffer the message. */
5541 if( hs->buffering.total_bytes_buffered >
5542 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5543 {
5544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5545 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5546 }
5547
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005548 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5549 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005550
5551 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5552 hs->buffering.total_bytes_buffered ) )
5553 {
5554 if( recv_msg_seq_offset > 0 )
5555 {
5556 /* If we can't buffer a future message because
5557 * of space limitations -- ignore. */
5558 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",
5559 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5560 (unsigned) hs->buffering.total_bytes_buffered ) );
5561 goto exit;
5562 }
Hanno Beckere1801392018-08-21 16:51:05 +01005563 else
5564 {
5565 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",
5566 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5567 (unsigned) hs->buffering.total_bytes_buffered ) );
5568 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005569
Hanno Beckera02b0b42018-08-21 17:20:27 +01005570 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005571 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005572 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",
5573 (unsigned) msg_len,
5574 (unsigned) reassembly_buf_sz,
5575 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005576 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005577 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5578 goto exit;
5579 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005580 }
5581
5582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5583 msg_len ) );
5584
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005585 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5586 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005587 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005588 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005589 goto exit;
5590 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005591 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005592
5593 /* Prepare final header: copy msg_type, length and message_seq,
5594 * then add standardised fragment_offset and fragment_length */
5595 memcpy( hs_buf->data, ssl->in_msg, 6 );
5596 memset( hs_buf->data + 6, 0, 3 );
5597 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5598
5599 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005600
5601 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005602 }
5603 else
5604 {
5605 /* Make sure msg_type and length are consistent */
5606 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5607 {
5608 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5609 /* Ignore */
5610 goto exit;
5611 }
5612 }
5613
Hanno Becker4422bbb2018-08-20 09:40:19 +01005614 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005615 {
5616 size_t frag_len, frag_off;
5617 unsigned char * const msg = hs_buf->data + 12;
5618
5619 /*
5620 * Check and copy current fragment
5621 */
5622
5623 /* Validation of header fields already done in
5624 * mbedtls_ssl_prepare_handshake_record(). */
5625 frag_off = ssl_get_hs_frag_off( ssl );
5626 frag_len = ssl_get_hs_frag_len( ssl );
5627
5628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5629 frag_off, frag_len ) );
5630 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5631
5632 if( hs_buf->is_fragmented )
5633 {
5634 unsigned char * const bitmask = msg + msg_len;
5635 ssl_bitmask_set( bitmask, frag_off, frag_len );
5636 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5637 msg_len ) == 0 );
5638 }
5639 else
5640 {
5641 hs_buf->is_complete = 1;
5642 }
5643
5644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5645 hs_buf->is_complete ? "" : "not yet " ) );
5646 }
5647
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005648 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005649 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005650
5651 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005652 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005653 break;
5654 }
5655
5656exit:
5657
5658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5659 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005660}
5661#endif /* MBEDTLS_SSL_PROTO_DTLS */
5662
Hanno Becker1097b342018-08-15 14:09:41 +01005663static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005664{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005665 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005666 * Consume last content-layer message and potentially
5667 * update in_msglen which keeps track of the contents'
5668 * consumption state.
5669 *
5670 * (1) Handshake messages:
5671 * Remove last handshake message, move content
5672 * and adapt in_msglen.
5673 *
5674 * (2) Alert messages:
5675 * Consume whole record content, in_msglen = 0.
5676 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005677 * (3) Change cipher spec:
5678 * Consume whole record content, in_msglen = 0.
5679 *
5680 * (4) Application data:
5681 * Don't do anything - the record layer provides
5682 * the application data as a stream transport
5683 * and consumes through mbedtls_ssl_read only.
5684 *
5685 */
5686
5687 /* Case (1): Handshake messages */
5688 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005689 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005690 /* Hard assertion to be sure that no application data
5691 * is in flight, as corrupting ssl->in_msglen during
5692 * ssl->in_offt != NULL is fatal. */
5693 if( ssl->in_offt != NULL )
5694 {
5695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5697 }
5698
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005699 /*
5700 * Get next Handshake message in the current record
5701 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005702
Hanno Becker4a810fb2017-05-24 16:27:30 +01005703 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005704 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005705 * current handshake content: If DTLS handshake
5706 * fragmentation is used, that's the fragment
5707 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005708 * size here is faulty and should be changed at
5709 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005710 * (2) While it doesn't seem to cause problems, one
5711 * has to be very careful not to assume that in_hslen
5712 * is always <= in_msglen in a sensible communication.
5713 * Again, it's wrong for DTLS handshake fragmentation.
5714 * The following check is therefore mandatory, and
5715 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005716 * Additionally, ssl->in_hslen might be arbitrarily out of
5717 * bounds after handling a DTLS message with an unexpected
5718 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005719 */
5720 if( ssl->in_hslen < ssl->in_msglen )
5721 {
5722 ssl->in_msglen -= ssl->in_hslen;
5723 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5724 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005725
Hanno Becker4a810fb2017-05-24 16:27:30 +01005726 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5727 ssl->in_msg, ssl->in_msglen );
5728 }
5729 else
5730 {
5731 ssl->in_msglen = 0;
5732 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005733
Hanno Becker4a810fb2017-05-24 16:27:30 +01005734 ssl->in_hslen = 0;
5735 }
5736 /* Case (4): Application data */
5737 else if( ssl->in_offt != NULL )
5738 {
5739 return( 0 );
5740 }
5741 /* Everything else (CCS & Alerts) */
5742 else
5743 {
5744 ssl->in_msglen = 0;
5745 }
5746
Hanno Becker1097b342018-08-15 14:09:41 +01005747 return( 0 );
5748}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005749
Hanno Beckere74d5562018-08-15 14:26:08 +01005750static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5751{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005752 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005753 return( 1 );
5754
5755 return( 0 );
5756}
5757
Hanno Becker5f066e72018-08-16 14:56:31 +01005758#if defined(MBEDTLS_SSL_PROTO_DTLS)
5759
5760static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5761{
5762 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5763 if( hs == NULL )
5764 return;
5765
Hanno Becker01315ea2018-08-21 17:22:17 +01005766 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005767 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005768 hs->buffering.total_bytes_buffered -=
5769 hs->buffering.future_record.len;
5770
5771 mbedtls_free( hs->buffering.future_record.data );
5772 hs->buffering.future_record.data = NULL;
5773 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005774}
5775
5776static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5777{
5778 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5779 unsigned char * rec;
5780 size_t rec_len;
5781 unsigned rec_epoch;
5782
5783 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5784 return( 0 );
5785
5786 if( hs == NULL )
5787 return( 0 );
5788
Hanno Becker5f066e72018-08-16 14:56:31 +01005789 rec = hs->buffering.future_record.data;
5790 rec_len = hs->buffering.future_record.len;
5791 rec_epoch = hs->buffering.future_record.epoch;
5792
5793 if( rec == NULL )
5794 return( 0 );
5795
Hanno Becker4cb782d2018-08-20 11:19:05 +01005796 /* Only consider loading future records if the
5797 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005798 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005799 return( 0 );
5800
Hanno Becker5f066e72018-08-16 14:56:31 +01005801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5802
5803 if( rec_epoch != ssl->in_epoch )
5804 {
5805 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5806 goto exit;
5807 }
5808
5809 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5810
5811 /* Double-check that the record is not too large */
5812 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5813 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5814 {
5815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5816 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5817 }
5818
5819 memcpy( ssl->in_hdr, rec, rec_len );
5820 ssl->in_left = rec_len;
5821 ssl->next_record_offset = 0;
5822
5823 ssl_free_buffered_record( ssl );
5824
5825exit:
5826 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5827 return( 0 );
5828}
5829
5830static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5831{
5832 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5833 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005834 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005835
5836 /* Don't buffer future records outside handshakes. */
5837 if( hs == NULL )
5838 return( 0 );
5839
5840 /* Only buffer handshake records (we are only interested
5841 * in Finished messages). */
5842 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5843 return( 0 );
5844
5845 /* Don't buffer more than one future epoch record. */
5846 if( hs->buffering.future_record.data != NULL )
5847 return( 0 );
5848
Hanno Becker01315ea2018-08-21 17:22:17 +01005849 /* Don't buffer record if there's not enough buffering space remaining. */
5850 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5851 hs->buffering.total_bytes_buffered ) )
5852 {
5853 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",
5854 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5855 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005856 return( 0 );
5857 }
5858
Hanno Becker5f066e72018-08-16 14:56:31 +01005859 /* Buffer record */
5860 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5861 ssl->in_epoch + 1 ) );
5862 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5863 rec_hdr_len + ssl->in_msglen );
5864
5865 /* ssl_parse_record_header() only considers records
5866 * of the next epoch as candidates for buffering. */
5867 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005868 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005869
5870 hs->buffering.future_record.data =
5871 mbedtls_calloc( 1, hs->buffering.future_record.len );
5872 if( hs->buffering.future_record.data == NULL )
5873 {
5874 /* If we run out of RAM trying to buffer a
5875 * record from the next epoch, just ignore. */
5876 return( 0 );
5877 }
5878
Hanno Becker01315ea2018-08-21 17:22:17 +01005879 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005880
Hanno Becker01315ea2018-08-21 17:22:17 +01005881 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005882 return( 0 );
5883}
5884
5885#endif /* MBEDTLS_SSL_PROTO_DTLS */
5886
Hanno Beckere74d5562018-08-15 14:26:08 +01005887static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005888{
5889 int ret;
5890
Hanno Becker5f066e72018-08-16 14:56:31 +01005891#if defined(MBEDTLS_SSL_PROTO_DTLS)
5892 /* We might have buffered a future record; if so,
5893 * and if the epoch matches now, load it.
5894 * On success, this call will set ssl->in_left to
5895 * the length of the buffered record, so that
5896 * the calls to ssl_fetch_input() below will
5897 * essentially be no-ops. */
5898 ret = ssl_load_buffered_record( ssl );
5899 if( ret != 0 )
5900 return( ret );
5901#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005902
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005903 /* Reset in pointers to default state for TLS/DTLS records,
5904 * assuming no CID and no offset between record content and
5905 * record plaintext. */
5906 ssl_update_in_pointers( ssl );
5907
5908 /* Ensure that we have enough space available for the default form
5909 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5910 * with no space for CIDs counted in). */
5911 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5912 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005913 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005914 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005915 return( ret );
5916 }
5917
5918 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005920#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005921 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5922 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005923 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005924 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5925 {
5926 ret = ssl_buffer_future_record( ssl );
5927 if( ret != 0 )
5928 return( ret );
5929
5930 /* Fall through to handling of unexpected records */
5931 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5932 }
5933
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005934 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5935 {
5936 /* Skip unexpected record (but not whole datagram) */
5937 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005938 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005939
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5941 "(header)" ) );
5942 }
5943 else
5944 {
5945 /* Skip invalid record and the rest of the datagram */
5946 ssl->next_record_offset = 0;
5947 ssl->in_left = 0;
5948
5949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5950 "(header)" ) );
5951 }
5952
5953 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005954 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005955 }
5956#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005957 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005958 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005959
5960 /*
5961 * Read and optionally decrypt the message contents
5962 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005963 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005964 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005966 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005967 return( ret );
5968 }
5969
5970 /* Done reading this record, get ready for the next one */
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 )
Hanno Beckere65ce782017-05-22 14:47:48 +01005973 {
Hanno Becker5903de42019-05-03 14:46:38 +01005974 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005975 if( ssl->next_record_offset < ssl->in_left )
5976 {
5977 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5978 }
5979 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005980 else
5981#endif
5982 ssl->in_left = 0;
5983
5984 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005985 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005987 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005988 {
5989 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005990 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005991 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005992 /* Except when waiting for Finished as a bad mac here
5993 * probably means something went wrong in the handshake
5994 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5995 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5996 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5997 {
5998#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5999 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6000 {
6001 mbedtls_ssl_send_alert_message( ssl,
6002 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6003 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6004 }
6005#endif
6006 return( ret );
6007 }
6008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006010 if( ssl->conf->badmac_limit != 0 &&
6011 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006013 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6014 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006015 }
6016#endif
6017
Hanno Becker4a810fb2017-05-24 16:27:30 +01006018 /* As above, invalid records cause
6019 * dismissal of the whole datagram. */
6020
6021 ssl->next_record_offset = 0;
6022 ssl->in_left = 0;
6023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006025 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006026 }
6027
6028 return( ret );
6029 }
6030 else
6031#endif
6032 {
6033 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6035 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 mbedtls_ssl_send_alert_message( ssl,
6038 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6039 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006040 }
6041#endif
6042 return( ret );
6043 }
6044 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006045
Simon Butcher99000142016-10-13 17:21:01 +01006046 return( 0 );
6047}
6048
6049int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6050{
6051 int ret;
6052
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006053 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006054 * Handle particular types of records
6055 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006056 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006057 {
Simon Butcher99000142016-10-13 17:21:01 +01006058 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6059 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006060 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006061 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006062 }
6063
Hanno Beckere678eaa2018-08-21 14:57:46 +01006064 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006065 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006066 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006067 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006068 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6069 ssl->in_msglen ) );
6070 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006071 }
6072
Hanno Beckere678eaa2018-08-21 14:57:46 +01006073 if( ssl->in_msg[0] != 1 )
6074 {
6075 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6076 ssl->in_msg[0] ) );
6077 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6078 }
6079
6080#if defined(MBEDTLS_SSL_PROTO_DTLS)
6081 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6082 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6083 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6084 {
6085 if( ssl->handshake == NULL )
6086 {
6087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6088 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6089 }
6090
6091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6092 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6093 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006094#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006095 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006097 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006098 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006099 if( ssl->in_msglen != 2 )
6100 {
6101 /* Note: Standard allows for more than one 2 byte alert
6102 to be packed in a single message, but Mbed TLS doesn't
6103 currently support this. */
6104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6105 ssl->in_msglen ) );
6106 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6107 }
6108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006110 ssl->in_msg[0], ssl->in_msg[1] ) );
6111
6112 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006113 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006116 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006118 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006119 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006120 }
6121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6123 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6126 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006128
6129#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6130 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6131 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6132 {
Hanno Becker90333da2017-10-10 11:27:13 +01006133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006134 /* Will be handled when trying to parse ServerHello */
6135 return( 0 );
6136 }
6137#endif
6138
6139#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6140 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6141 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6142 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6143 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6144 {
6145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6146 /* Will be handled in mbedtls_ssl_parse_certificate() */
6147 return( 0 );
6148 }
6149#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6150
6151 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006152 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006153 }
6154
Hanno Beckerc76c6192017-06-06 10:03:17 +01006155#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006156 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006157 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006158 /* Drop unexpected ApplicationData records,
6159 * except at the beginning of renegotiations */
6160 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6161 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6162#if defined(MBEDTLS_SSL_RENEGOTIATION)
6163 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6164 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006165#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006166 )
6167 {
6168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6169 return( MBEDTLS_ERR_SSL_NON_FATAL );
6170 }
6171
6172 if( ssl->handshake != NULL &&
6173 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6174 {
6175 ssl_handshake_wrapup_free_hs_transform( ssl );
6176 }
6177 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006178#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006179
Paul Bakker5121ce52009-01-03 21:22:43 +00006180 return( 0 );
6181}
6182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006183int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006184{
6185 int ret;
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6188 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6189 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006190 {
6191 return( ret );
6192 }
6193
6194 return( 0 );
6195}
6196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006198 unsigned char level,
6199 unsigned char message )
6200{
6201 int ret;
6202
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006203 if( ssl == NULL || ssl->conf == NULL )
6204 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006207 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006210 ssl->out_msglen = 2;
6211 ssl->out_msg[0] = level;
6212 ssl->out_msg[1] = message;
6213
Hanno Becker67bc7c32018-08-06 11:33:50 +01006214 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006216 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006217 return( ret );
6218 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006220
6221 return( 0 );
6222}
6223
Hanno Beckerb9d44792019-02-08 07:19:04 +00006224#if defined(MBEDTLS_X509_CRT_PARSE_C)
6225static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6226{
6227#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6228 if( session->peer_cert != NULL )
6229 {
6230 mbedtls_x509_crt_free( session->peer_cert );
6231 mbedtls_free( session->peer_cert );
6232 session->peer_cert = NULL;
6233 }
6234#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6235 if( session->peer_cert_digest != NULL )
6236 {
6237 /* Zeroization is not necessary. */
6238 mbedtls_free( session->peer_cert_digest );
6239 session->peer_cert_digest = NULL;
6240 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6241 session->peer_cert_digest_len = 0;
6242 }
6243#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6244}
6245#endif /* MBEDTLS_X509_CRT_PARSE_C */
6246
Paul Bakker5121ce52009-01-03 21:22:43 +00006247/*
6248 * Handshake functions
6249 */
Hanno Becker21489932019-02-05 13:20:55 +00006250#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006251/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006252int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006253{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006254 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6255 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006258
Hanno Becker7177a882019-02-05 13:36:46 +00006259 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006262 ssl->state++;
6263 return( 0 );
6264 }
6265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6267 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006268}
6269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006271{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006272 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6273 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006276
Hanno Becker7177a882019-02-05 13:36:46 +00006277 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006278 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006280 ssl->state++;
6281 return( 0 );
6282 }
6283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6285 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006286}
Gilles Peskinef9828522017-05-03 12:28:43 +02006287
Hanno Becker21489932019-02-05 13:20:55 +00006288#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006289/* Some certificate support -> implement write and parse */
6290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006294 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006296 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6297 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006300
Hanno Becker7177a882019-02-05 13:36:46 +00006301 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006304 ssl->state++;
6305 return( 0 );
6306 }
6307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006309 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006310 {
6311 if( ssl->client_auth == 0 )
6312 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006314 ssl->state++;
6315 return( 0 );
6316 }
6317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006319 /*
6320 * If using SSLv3 and got no cert, send an Alert message
6321 * (otherwise an empty Certificate message will be sent).
6322 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6324 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006325 {
6326 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6328 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6329 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006332 goto write_msg;
6333 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336#endif /* MBEDTLS_SSL_CLI_C */
6337#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6343 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006344 }
6345 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006349
6350 /*
6351 * 0 . 0 handshake type
6352 * 1 . 3 handshake length
6353 * 4 . 6 length of all certs
6354 * 7 . 9 length of cert. 1
6355 * 10 . n-1 peer certificate
6356 * n . n+2 length of cert. 2
6357 * n+3 . ... upper level cert, etc.
6358 */
6359 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
Paul Bakker29087132010-03-21 21:03:34 +00006362 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006363 {
6364 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006365 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006366 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006367 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006368 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370 }
6371
6372 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6373 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6374 ssl->out_msg[i + 2] = (unsigned char)( n );
6375
6376 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6377 i += n; crt = crt->next;
6378 }
6379
6380 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6381 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6382 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6383
6384 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6386 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006387
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006388#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006389write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006390#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006391
6392 ssl->state++;
6393
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006394 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006395 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006396 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006397 return( ret );
6398 }
6399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006401
Paul Bakkered27a042013-04-18 22:46:23 +02006402 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006403}
6404
Hanno Becker84879e32019-01-31 07:44:03 +00006405#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006406
6407#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006408static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6409 unsigned char *crt_buf,
6410 size_t crt_buf_len )
6411{
6412 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6413
6414 if( peer_crt == NULL )
6415 return( -1 );
6416
6417 if( peer_crt->raw.len != crt_buf_len )
6418 return( -1 );
6419
Hanno Becker46f34d02019-02-08 14:00:04 +00006420 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006421}
Hanno Becker177475a2019-02-05 17:02:46 +00006422#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6423static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6424 unsigned char *crt_buf,
6425 size_t crt_buf_len )
6426{
6427 int ret;
6428 unsigned char const * const peer_cert_digest =
6429 ssl->session->peer_cert_digest;
6430 mbedtls_md_type_t const peer_cert_digest_type =
6431 ssl->session->peer_cert_digest_type;
6432 mbedtls_md_info_t const * const digest_info =
6433 mbedtls_md_info_from_type( peer_cert_digest_type );
6434 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6435 size_t digest_len;
6436
6437 if( peer_cert_digest == NULL || digest_info == NULL )
6438 return( -1 );
6439
6440 digest_len = mbedtls_md_get_size( digest_info );
6441 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6442 return( -1 );
6443
6444 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6445 if( ret != 0 )
6446 return( -1 );
6447
6448 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6449}
6450#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006451#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006452
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006453/*
6454 * Once the certificate message is read, parse it into a cert chain and
6455 * perform basic checks, but leave actual verification to the caller
6456 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006457static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6458 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006459{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006460 int ret;
6461#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6462 int crt_cnt=0;
6463#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006464 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006465 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006467 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006469 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006470 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6471 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006472 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006473 }
6474
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006475 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6476 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006479 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6480 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 }
6483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006485
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006489 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006490
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006491 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006495 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6496 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 }
6499
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006500 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6501 i += 3;
6502
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006503 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 while( i < ssl->in_hslen )
6505 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006506 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006507 if ( i + 3 > ssl->in_hslen ) {
6508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006509 mbedtls_ssl_send_alert_message( ssl,
6510 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6511 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006512 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6513 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006514 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6515 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 if( ssl->in_msg[i] != 0 )
6517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006519 mbedtls_ssl_send_alert_message( ssl,
6520 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6521 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006522 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006523 }
6524
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006525 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006526 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6527 | (unsigned int) ssl->in_msg[i + 2];
6528 i += 3;
6529
6530 if( n < 128 || i + n > ssl->in_hslen )
6531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006533 mbedtls_ssl_send_alert_message( ssl,
6534 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6535 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 }
6538
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006539 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006540#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6541 if( crt_cnt++ == 0 &&
6542 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6543 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006544 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006545 /* During client-side renegotiation, check that the server's
6546 * end-CRTs hasn't changed compared to the initial handshake,
6547 * mitigating the triple handshake attack. On success, reuse
6548 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006549 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6550 if( ssl_check_peer_crt_unchanged( ssl,
6551 &ssl->in_msg[i],
6552 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006553 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006554 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6555 mbedtls_ssl_send_alert_message( ssl,
6556 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6557 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6558 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006559 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006560
6561 /* Now we can safely free the original chain. */
6562 ssl_clear_peer_cert( ssl->session );
6563 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006564#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6565
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006566 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006567#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006568 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006569#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006570 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006571 * it in-place from the input buffer instead of making a copy. */
6572 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6573#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006574 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006575 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006576 case 0: /*ok*/
6577 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6578 /* Ignore certificate with an unknown algorithm: maybe a
6579 prior certificate was already trusted. */
6580 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006581
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006582 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6583 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6584 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006585
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006586 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6587 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6588 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006589
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006590 default:
6591 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6592 crt_parse_der_failed:
6593 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6594 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6595 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006596 }
6597
6598 i += n;
6599 }
6600
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006601 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006602 return( 0 );
6603}
6604
Hanno Becker4a55f632019-02-05 12:49:06 +00006605#if defined(MBEDTLS_SSL_SRV_C)
6606static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6607{
6608 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6609 return( -1 );
6610
6611#if defined(MBEDTLS_SSL_PROTO_SSL3)
6612 /*
6613 * Check if the client sent an empty certificate
6614 */
6615 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6616 {
6617 if( ssl->in_msglen == 2 &&
6618 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6619 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6620 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6621 {
6622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6623 return( 0 );
6624 }
6625
6626 return( -1 );
6627 }
6628#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6629
6630#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6631 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6632 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6633 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6634 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6635 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6636 {
6637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6638 return( 0 );
6639 }
6640
6641 return( -1 );
6642#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6643 MBEDTLS_SSL_PROTO_TLS1_2 */
6644}
6645#endif /* MBEDTLS_SSL_SRV_C */
6646
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006647/* Check if a certificate message is expected.
6648 * Return either
6649 * - SSL_CERTIFICATE_EXPECTED, or
6650 * - SSL_CERTIFICATE_SKIP
6651 * indicating whether a Certificate message is expected or not.
6652 */
6653#define SSL_CERTIFICATE_EXPECTED 0
6654#define SSL_CERTIFICATE_SKIP 1
6655static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6656 int authmode )
6657{
6658 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006659 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006660
6661 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6662 return( SSL_CERTIFICATE_SKIP );
6663
6664#if defined(MBEDTLS_SSL_SRV_C)
6665 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6666 {
6667 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6668 return( SSL_CERTIFICATE_SKIP );
6669
6670 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6671 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006672 ssl->session_negotiate->verify_result =
6673 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6674 return( SSL_CERTIFICATE_SKIP );
6675 }
6676 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006677#else
6678 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006679#endif /* MBEDTLS_SSL_SRV_C */
6680
6681 return( SSL_CERTIFICATE_EXPECTED );
6682}
6683
Hanno Becker68636192019-02-05 14:36:34 +00006684static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6685 int authmode,
6686 mbedtls_x509_crt *chain,
6687 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006688{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006689 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006690 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006691 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006692 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006693
Hanno Becker8927c832019-04-03 12:52:50 +01006694 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6695 void *p_vrfy;
6696
Hanno Becker68636192019-02-05 14:36:34 +00006697 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6698 return( 0 );
6699
Hanno Becker8927c832019-04-03 12:52:50 +01006700 if( ssl->f_vrfy != NULL )
6701 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006702 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006703 f_vrfy = ssl->f_vrfy;
6704 p_vrfy = ssl->p_vrfy;
6705 }
6706 else
6707 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006708 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006709 f_vrfy = ssl->conf->f_vrfy;
6710 p_vrfy = ssl->conf->p_vrfy;
6711 }
6712
Hanno Becker68636192019-02-05 14:36:34 +00006713 /*
6714 * Main check: verify certificate
6715 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006716#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6717 if( ssl->conf->f_ca_cb != NULL )
6718 {
6719 ((void) rs_ctx);
6720 have_ca_chain = 1;
6721
6722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006723 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006724 chain,
6725 ssl->conf->f_ca_cb,
6726 ssl->conf->p_ca_cb,
6727 ssl->conf->cert_profile,
6728 ssl->hostname,
6729 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006730 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006731 }
6732 else
6733#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6734 {
6735 mbedtls_x509_crt *ca_chain;
6736 mbedtls_x509_crl *ca_crl;
6737
6738#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6739 if( ssl->handshake->sni_ca_chain != NULL )
6740 {
6741 ca_chain = ssl->handshake->sni_ca_chain;
6742 ca_crl = ssl->handshake->sni_ca_crl;
6743 }
6744 else
6745#endif
6746 {
6747 ca_chain = ssl->conf->ca_chain;
6748 ca_crl = ssl->conf->ca_crl;
6749 }
6750
6751 if( ca_chain != NULL )
6752 have_ca_chain = 1;
6753
6754 ret = mbedtls_x509_crt_verify_restartable(
6755 chain,
6756 ca_chain, ca_crl,
6757 ssl->conf->cert_profile,
6758 ssl->hostname,
6759 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006760 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006761 }
Hanno Becker68636192019-02-05 14:36:34 +00006762
6763 if( ret != 0 )
6764 {
6765 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6766 }
6767
6768#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6769 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6770 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6771#endif
6772
6773 /*
6774 * Secondary checks: always done, but change 'ret' only if it was 0
6775 */
6776
6777#if defined(MBEDTLS_ECP_C)
6778 {
6779 const mbedtls_pk_context *pk = &chain->pk;
6780
6781 /* If certificate uses an EC key, make sure the curve is OK */
6782 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6783 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6784 {
6785 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6786
6787 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6788 if( ret == 0 )
6789 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6790 }
6791 }
6792#endif /* MBEDTLS_ECP_C */
6793
6794 if( mbedtls_ssl_check_cert_usage( chain,
6795 ciphersuite_info,
6796 ! ssl->conf->endpoint,
6797 &ssl->session_negotiate->verify_result ) != 0 )
6798 {
6799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6800 if( ret == 0 )
6801 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6802 }
6803
6804 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6805 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6806 * with details encoded in the verification flags. All other kinds
6807 * of error codes, including those from the user provided f_vrfy
6808 * functions, are treated as fatal and lead to a failure of
6809 * ssl_parse_certificate even if verification was optional. */
6810 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6811 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6812 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6813 {
6814 ret = 0;
6815 }
6816
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006817 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006818 {
6819 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6820 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6821 }
6822
6823 if( ret != 0 )
6824 {
6825 uint8_t alert;
6826
6827 /* The certificate may have been rejected for several reasons.
6828 Pick one and send the corresponding alert. Which alert to send
6829 may be a subject of debate in some cases. */
6830 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6831 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6832 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6833 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6834 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6835 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6836 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6837 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6838 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6839 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6840 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6841 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6842 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6843 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6845 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6847 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6849 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6850 else
6851 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6852 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6853 alert );
6854 }
6855
6856#if defined(MBEDTLS_DEBUG_C)
6857 if( ssl->session_negotiate->verify_result != 0 )
6858 {
6859 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6860 ssl->session_negotiate->verify_result ) );
6861 }
6862 else
6863 {
6864 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6865 }
6866#endif /* MBEDTLS_DEBUG_C */
6867
6868 return( ret );
6869}
6870
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006871#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6872static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6873 unsigned char *start, size_t len )
6874{
6875 int ret;
6876 /* Remember digest of the peer's end-CRT. */
6877 ssl->session_negotiate->peer_cert_digest =
6878 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6879 if( ssl->session_negotiate->peer_cert_digest == NULL )
6880 {
6881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6882 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6883 mbedtls_ssl_send_alert_message( ssl,
6884 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6885 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6886
6887 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6888 }
6889
6890 ret = mbedtls_md( mbedtls_md_info_from_type(
6891 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6892 start, len,
6893 ssl->session_negotiate->peer_cert_digest );
6894
6895 ssl->session_negotiate->peer_cert_digest_type =
6896 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6897 ssl->session_negotiate->peer_cert_digest_len =
6898 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6899
6900 return( ret );
6901}
6902
6903static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6904 unsigned char *start, size_t len )
6905{
6906 unsigned char *end = start + len;
6907 int ret;
6908
6909 /* Make a copy of the peer's raw public key. */
6910 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6911 ret = mbedtls_pk_parse_subpubkey( &start, end,
6912 &ssl->handshake->peer_pubkey );
6913 if( ret != 0 )
6914 {
6915 /* We should have parsed the public key before. */
6916 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6917 }
6918
6919 return( 0 );
6920}
6921#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6922
Hanno Becker68636192019-02-05 14:36:34 +00006923int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6924{
6925 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006926 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006927#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6928 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6929 ? ssl->handshake->sni_authmode
6930 : ssl->conf->authmode;
6931#else
6932 const int authmode = ssl->conf->authmode;
6933#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006934 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006935 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006936
6937 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6938
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006939 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6940 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006941 {
6942 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006943 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006944 }
6945
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006946#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6947 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006948 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006949 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006950 chain = ssl->handshake->ecrs_peer_cert;
6951 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006952 goto crt_verify;
6953 }
6954#endif
6955
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006956 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006957 {
6958 /* mbedtls_ssl_read_record may have sent an alert already. We
6959 let it decide whether to alert. */
6960 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006961 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006962 }
6963
Hanno Becker4a55f632019-02-05 12:49:06 +00006964#if defined(MBEDTLS_SSL_SRV_C)
6965 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6966 {
6967 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006968
6969 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006970 ret = 0;
6971 else
6972 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006973
Hanno Becker6bdfab22019-02-05 13:11:17 +00006974 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006975 }
6976#endif /* MBEDTLS_SSL_SRV_C */
6977
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006978 /* Clear existing peer CRT structure in case we tried to
6979 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006980 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006981
6982 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6983 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006984 {
6985 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6986 sizeof( mbedtls_x509_crt ) ) );
6987 mbedtls_ssl_send_alert_message( ssl,
6988 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6989 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006990
Hanno Becker3dad3112019-02-05 17:19:52 +00006991 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6992 goto exit;
6993 }
6994 mbedtls_x509_crt_init( chain );
6995
6996 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006997 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006998 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006999
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007000#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7001 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007002 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007003
7004crt_verify:
7005 if( ssl->handshake->ecrs_enabled)
7006 rs_ctx = &ssl->handshake->ecrs_ctx;
7007#endif
7008
Hanno Becker68636192019-02-05 14:36:34 +00007009 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007010 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007011 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007012 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007013
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007014#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007015 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007016 unsigned char *crt_start, *pk_start;
7017 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007018
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007019 /* We parse the CRT chain without copying, so
7020 * these pointers point into the input buffer,
7021 * and are hence still valid after freeing the
7022 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007023
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007024 crt_start = chain->raw.p;
7025 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007026
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007027 pk_start = chain->pk_raw.p;
7028 pk_len = chain->pk_raw.len;
7029
7030 /* Free the CRT structures before computing
7031 * digest and copying the peer's public key. */
7032 mbedtls_x509_crt_free( chain );
7033 mbedtls_free( chain );
7034 chain = NULL;
7035
7036 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007037 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007038 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007039
7040 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7041 if( ret != 0 )
7042 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007043 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007044#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7045 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007046 ssl->session_negotiate->peer_cert = chain;
7047 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007048#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007051
Hanno Becker6bdfab22019-02-05 13:11:17 +00007052exit:
7053
Hanno Becker3dad3112019-02-05 17:19:52 +00007054 if( ret == 0 )
7055 ssl->state++;
7056
7057#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7058 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7059 {
7060 ssl->handshake->ecrs_peer_cert = chain;
7061 chain = NULL;
7062 }
7063#endif
7064
7065 if( chain != NULL )
7066 {
7067 mbedtls_x509_crt_free( chain );
7068 mbedtls_free( chain );
7069 }
7070
Paul Bakker5121ce52009-01-03 21:22:43 +00007071 return( ret );
7072}
Hanno Becker21489932019-02-05 13:20:55 +00007073#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007075int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007076{
7077 int ret;
7078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007081 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007082 ssl->out_msglen = 1;
7083 ssl->out_msg[0] = 1;
7084
Paul Bakker5121ce52009-01-03 21:22:43 +00007085 ssl->state++;
7086
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007087 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007088 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007089 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007090 return( ret );
7091 }
7092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
7095 return( 0 );
7096}
7097
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007099{
7100 int ret;
7101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007103
Hanno Becker327c93b2018-08-15 13:56:18 +01007104 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007107 return( ret );
7108 }
7109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007113 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7114 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007116 }
7117
Hanno Beckere678eaa2018-08-21 14:57:46 +01007118 /* CCS records are only accepted if they have length 1 and content '1',
7119 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007120
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007121 /*
7122 * Switch to our negotiated transform and session parameters for inbound
7123 * data.
7124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007126 ssl->transform_in = ssl->transform_negotiate;
7127 ssl->session_in = ssl->session_negotiate;
7128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007130 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007132#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007133 ssl_dtls_replay_reset( ssl );
7134#endif
7135
7136 /* Increment epoch */
7137 if( ++ssl->in_epoch == 0 )
7138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007140 /* This is highly unlikely to happen for legitimate reasons, so
7141 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007143 }
7144 }
7145 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007147 memset( ssl->in_ctr, 0, 8 );
7148
Hanno Becker79594fd2019-05-08 09:38:41 +01007149 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7152 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007157 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7158 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007160 }
7161 }
7162#endif
7163
Paul Bakker5121ce52009-01-03 21:22:43 +00007164 ssl->state++;
7165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007167
7168 return( 0 );
7169}
7170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7172 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007173{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007174 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007175
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007176#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7177 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7178 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007179 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007180 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007181#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7183#if defined(MBEDTLS_SHA512_C)
7184 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007185 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7186 else
7187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SHA256_C)
7189 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007190 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007191 else
7192#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007193#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007196 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007197 }
Paul Bakker380da532012-04-18 16:10:25 +00007198}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007201{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7203 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007204 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7205 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7208#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007209#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007210 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007211 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7212#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007213 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007214#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007217#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007218 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007219 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007220#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007221 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007222#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007223#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007224#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007225}
7226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007227static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007228 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007229{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7231 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007232 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7233 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007235#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7236#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007237#if defined(MBEDTLS_USE_PSA_CRYPTO)
7238 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7239#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007240 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007241#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007242#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007244#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007245 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007246#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007247 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007248#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007251}
7252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007253#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7254 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7255static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007256 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007257{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007258 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7259 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007260}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007261#endif
Paul Bakker380da532012-04-18 16:10:25 +00007262
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7264#if defined(MBEDTLS_SHA256_C)
7265static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007266 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007267{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007268#if defined(MBEDTLS_USE_PSA_CRYPTO)
7269 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7270#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007271 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007272#endif
Paul Bakker380da532012-04-18 16:10:25 +00007273}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007274#endif
Paul Bakker380da532012-04-18 16:10:25 +00007275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_SHA512_C)
7277static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007278 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007279{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007280#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007281 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007282#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007283 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007284#endif
Paul Bakker380da532012-04-18 16:10:25 +00007285}
Paul Bakker769075d2012-11-24 11:26:46 +01007286#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007287#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007290static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007292{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007293 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007294 mbedtls_md5_context md5;
7295 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007296
Paul Bakker5121ce52009-01-03 21:22:43 +00007297 unsigned char padbuf[48];
7298 unsigned char md5sum[16];
7299 unsigned char sha1sum[20];
7300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007302 if( !session )
7303 session = ssl->session;
7304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007306
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007307 mbedtls_md5_init( &md5 );
7308 mbedtls_sha1_init( &sha1 );
7309
7310 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7311 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007312
7313 /*
7314 * SSLv3:
7315 * hash =
7316 * MD5( master + pad2 +
7317 * MD5( handshake + sender + master + pad1 ) )
7318 * + SHA1( master + pad2 +
7319 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007320 */
7321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007322#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007323 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7324 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007325#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007328 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7329 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007330#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007333 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007334
Paul Bakker1ef83d62012-04-11 12:09:53 +00007335 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007336
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007337 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7338 mbedtls_md5_update_ret( &md5, session->master, 48 );
7339 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7340 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007341
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007342 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7343 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7344 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7345 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007346
Paul Bakker1ef83d62012-04-11 12:09:53 +00007347 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007349 mbedtls_md5_starts_ret( &md5 );
7350 mbedtls_md5_update_ret( &md5, session->master, 48 );
7351 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7352 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7353 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007355 mbedtls_sha1_starts_ret( &sha1 );
7356 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7357 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7358 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7359 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007361 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007363 mbedtls_md5_free( &md5 );
7364 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007365
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007366 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7367 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7368 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007372#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007375static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007377{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007378 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007379 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007380 mbedtls_md5_context md5;
7381 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007382 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007385 if( !session )
7386 session = ssl->session;
7387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007390 mbedtls_md5_init( &md5 );
7391 mbedtls_sha1_init( &sha1 );
7392
7393 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7394 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396 /*
7397 * TLSv1:
7398 * hash = PRF( master, finished_label,
7399 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7400 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007403 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7404 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007405#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007407#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007408 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7409 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007410#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007412 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007413 ? "client finished"
7414 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007415
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007416 mbedtls_md5_finish_ret( &md5, padbuf );
7417 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007418
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007419 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007420 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007423
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007424 mbedtls_md5_free( &md5 );
7425 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007426
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007427 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007433#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7434#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007435static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007437{
7438 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007439 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007440 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007441#if defined(MBEDTLS_USE_PSA_CRYPTO)
7442 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007443 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007444 psa_status_t status;
7445#else
7446 mbedtls_sha256_context sha256;
7447#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007450 if( !session )
7451 session = ssl->session;
7452
Andrzej Kurekeb342242019-01-29 09:14:33 -05007453 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7454 ? "client finished"
7455 : "server finished";
7456
7457#if defined(MBEDTLS_USE_PSA_CRYPTO)
7458 sha256_psa = psa_hash_operation_init();
7459
7460 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7461
7462 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7463 if( status != PSA_SUCCESS )
7464 {
7465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7466 return;
7467 }
7468
7469 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7470 if( status != PSA_SUCCESS )
7471 {
7472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7473 return;
7474 }
7475 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7476#else
7477
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007478 mbedtls_sha256_init( &sha256 );
7479
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007481
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007482 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007483
7484 /*
7485 * TLSv1.2:
7486 * hash = PRF( master, finished_label,
7487 * Hash( handshake ) )[0.11]
7488 */
7489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490#if !defined(MBEDTLS_SHA256_ALT)
7491 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007492 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007493#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007494
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007495 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007496 mbedtls_sha256_free( &sha256 );
7497#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007498
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007499 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007500 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007503
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007504 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007508#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007510#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007511static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007512 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007513{
7514 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007515 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007516 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007517#if defined(MBEDTLS_USE_PSA_CRYPTO)
7518 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007519 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007520 psa_status_t status;
7521#else
7522 mbedtls_sha512_context sha512;
7523#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007526 if( !session )
7527 session = ssl->session;
7528
Andrzej Kurekeb342242019-01-29 09:14:33 -05007529 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7530 ? "client finished"
7531 : "server finished";
7532
7533#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007534 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007535
7536 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7537
Andrzej Kurek972fba52019-01-30 03:29:12 -05007538 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007539 if( status != PSA_SUCCESS )
7540 {
7541 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7542 return;
7543 }
7544
Andrzej Kurek972fba52019-01-30 03:29:12 -05007545 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007546 if( status != PSA_SUCCESS )
7547 {
7548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7549 return;
7550 }
7551 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7552#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007553 mbedtls_sha512_init( &sha512 );
7554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007556
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007557 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007558
7559 /*
7560 * TLSv1.2:
7561 * hash = PRF( master, finished_label,
7562 * Hash( handshake ) )[0.11]
7563 */
7564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007565#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007566 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7567 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007568#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007569
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007570 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007571 mbedtls_sha512_free( &sha512 );
7572#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007573
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007574 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007575 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007578
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007579 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007583#endif /* MBEDTLS_SHA512_C */
7584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007587{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007589
7590 /*
7591 * Free our handshake params
7592 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007593 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007595 ssl->handshake = NULL;
7596
7597 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007598 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007599 */
7600 if( ssl->transform )
7601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 mbedtls_ssl_transform_free( ssl->transform );
7603 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007604 }
7605 ssl->transform = ssl->transform_negotiate;
7606 ssl->transform_negotiate = NULL;
7607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007609}
7610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007612{
7613 int resume = ssl->handshake->resume;
7614
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617#if defined(MBEDTLS_SSL_RENEGOTIATION)
7618 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007621 ssl->renego_records_seen = 0;
7622 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007623#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007624
7625 /*
7626 * Free the previous session and switch in the current one
7627 */
Paul Bakker0a597072012-09-25 21:55:46 +00007628 if( ssl->session )
7629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007631 /* RFC 7366 3.1: keep the EtM state */
7632 ssl->session_negotiate->encrypt_then_mac =
7633 ssl->session->encrypt_then_mac;
7634#endif
7635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 mbedtls_ssl_session_free( ssl->session );
7637 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007638 }
7639 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007640 ssl->session_negotiate = NULL;
7641
Paul Bakker0a597072012-09-25 21:55:46 +00007642 /*
7643 * Add cache entry
7644 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007645 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007646 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007647 resume == 0 )
7648 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007649 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007651 }
Paul Bakker0a597072012-09-25 21:55:46 +00007652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007653#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007654 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007655 ssl->handshake->flight != NULL )
7656 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007657 /* Cancel handshake timer */
7658 ssl_set_timer( ssl, 0 );
7659
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007660 /* Keep last flight around in case we need to resend it:
7661 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007663 }
7664 else
7665#endif
7666 ssl_handshake_wrapup_free_hs_transform( ssl );
7667
Paul Bakker48916f92012-09-16 19:57:18 +00007668 ssl->state++;
7669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007670 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007671}
7672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007673int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007674{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007675 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007678
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007679 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007680
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007681 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007682
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007683 /*
7684 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7685 * may define some other value. Currently (early 2016), no defined
7686 * ciphersuite does this (and this is unlikely to change as activity has
7687 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7688 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007692 ssl->verify_data_len = hash_len;
7693 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007694#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007695
Paul Bakker5121ce52009-01-03 21:22:43 +00007696 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007697 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7698 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
7700 /*
7701 * In case of session resuming, invert the client and server
7702 * ChangeCipherSpec messages order.
7703 */
Paul Bakker0a597072012-09-25 21:55:46 +00007704 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007705 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007707 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007708 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007709#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007711 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007713#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 }
7715 else
7716 ssl->state++;
7717
Paul Bakker48916f92012-09-16 19:57:18 +00007718 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007719 * Switch to our negotiated transform and session parameters for outbound
7720 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007721 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007725 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007726 {
7727 unsigned char i;
7728
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007729 /* Remember current epoch settings for resending */
7730 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007731 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007732
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007733 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007734 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007735
7736 /* Increment epoch */
7737 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007738 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007739 break;
7740
7741 /* The loop goes to its end iff the counter is wrapping */
7742 if( i == 0 )
7743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7745 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007746 }
7747 }
7748 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007749#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007750 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007751
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007752 ssl->transform_out = ssl->transform_negotiate;
7753 ssl->session_out = ssl->session_negotiate;
7754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007755#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7756 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7761 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007762 }
7763 }
7764#endif
7765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007767 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007769#endif
7770
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007771 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007772 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007774 return( ret );
7775 }
7776
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007777#if defined(MBEDTLS_SSL_PROTO_DTLS)
7778 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7779 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7780 {
7781 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7782 return( ret );
7783 }
7784#endif
7785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007786 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007787
7788 return( 0 );
7789}
7790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007791#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007792#define SSL_MAX_HASH_LEN 36
7793#else
7794#define SSL_MAX_HASH_LEN 12
7795#endif
7796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007797int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007798{
Paul Bakker23986e52011-04-24 08:57:21 +00007799 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007800 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007801 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007804
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007805 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007806
Hanno Becker327c93b2018-08-15 13:56:18 +01007807 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007808 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007810 return( ret );
7811 }
7812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007816 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7817 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819 }
7820
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007821 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822#if defined(MBEDTLS_SSL_PROTO_SSL3)
7823 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007824 hash_len = 36;
7825 else
7826#endif
7827 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7830 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007831 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007833 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7834 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007836 }
7837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007839 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007842 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7843 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 }
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007848 ssl->verify_data_len = hash_len;
7849 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007850#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007851
Paul Bakker0a597072012-09-25 21:55:46 +00007852 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007855 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007859 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007861#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 }
7863 else
7864 ssl->state++;
7865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007867 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007869#endif
7870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007872
7873 return( 0 );
7874}
7875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007877{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7881 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7882 mbedtls_md5_init( &handshake->fin_md5 );
7883 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007884 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7885 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7888#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007889#if defined(MBEDTLS_USE_PSA_CRYPTO)
7890 handshake->fin_sha256_psa = psa_hash_operation_init();
7891 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7892#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007894 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007895#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007896#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007897#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007899 handshake->fin_sha384_psa = psa_hash_operation_init();
7900 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007901#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007903 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007904#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007907
7908 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007909
7910#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7911 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7912 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7913#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#if defined(MBEDTLS_DHM_C)
7916 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007917#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#if defined(MBEDTLS_ECDH_C)
7919 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007920#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007921#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007922 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007923#if defined(MBEDTLS_SSL_CLI_C)
7924 handshake->ecjpake_cache = NULL;
7925 handshake->ecjpake_cache_len = 0;
7926#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007927#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007928
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007929#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007930 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007931#endif
7932
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007933#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7934 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7935#endif
Hanno Becker75173122019-02-06 16:18:31 +00007936
7937#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7938 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7939 mbedtls_pk_init( &handshake->peer_pubkey );
7940#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007941}
7942
Hanno Beckera18d1322018-01-03 14:27:32 +00007943void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007944{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007945 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007947 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7948 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007949
Hanno Beckerd56ed242018-01-03 15:32:51 +00007950#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 mbedtls_md_init( &transform->md_ctx_enc );
7952 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007953#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007954}
7955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959}
7960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007962{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007963 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007964 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007969 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007970
7971 /*
7972 * Either the pointers are now NULL or cleared properly and can be freed.
7973 * Now allocate missing structures.
7974 */
7975 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007976 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007977 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007978 }
Paul Bakker48916f92012-09-16 19:57:18 +00007979
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007981 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007982 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007983 }
Paul Bakker48916f92012-09-16 19:57:18 +00007984
Paul Bakker82788fb2014-10-20 13:59:19 +02007985 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007986 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007987 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007988 }
Paul Bakker48916f92012-09-16 19:57:18 +00007989
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007990 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007991 if( ssl->handshake == NULL ||
7992 ssl->transform_negotiate == NULL ||
7993 ssl->session_negotiate == NULL )
7994 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007997 mbedtls_free( ssl->handshake );
7998 mbedtls_free( ssl->transform_negotiate );
7999 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008000
8001 ssl->handshake = NULL;
8002 ssl->transform_negotiate = NULL;
8003 ssl->session_negotiate = NULL;
8004
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008005 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008006 }
8007
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008010 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008011 ssl_handshake_params_init( ssl->handshake );
8012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008014 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8015 {
8016 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008017
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008018 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8019 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8020 else
8021 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008022
8023 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008024 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008025#endif
8026
Paul Bakker48916f92012-09-16 19:57:18 +00008027 return( 0 );
8028}
8029
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008030#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008031/* Dummy cookie callbacks for defaults */
8032static int ssl_cookie_write_dummy( void *ctx,
8033 unsigned char **p, unsigned char *end,
8034 const unsigned char *cli_id, size_t cli_id_len )
8035{
8036 ((void) ctx);
8037 ((void) p);
8038 ((void) end);
8039 ((void) cli_id);
8040 ((void) cli_id_len);
8041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008042 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008043}
8044
8045static int ssl_cookie_check_dummy( void *ctx,
8046 const unsigned char *cookie, size_t cookie_len,
8047 const unsigned char *cli_id, size_t cli_id_len )
8048{
8049 ((void) ctx);
8050 ((void) cookie);
8051 ((void) cookie_len);
8052 ((void) cli_id);
8053 ((void) cli_id_len);
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008056}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008057#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008058
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008059/* Once ssl->out_hdr as the address of the beginning of the
8060 * next outgoing record is set, deduce the other pointers.
8061 *
8062 * Note: For TLS, we save the implicit record sequence number
8063 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8064 * and the caller has to make sure there's space for this.
8065 */
8066
8067static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8068 mbedtls_ssl_transform *transform )
8069{
8070#if defined(MBEDTLS_SSL_PROTO_DTLS)
8071 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8072 {
8073 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008074#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008075 ssl->out_cid = ssl->out_ctr + 8;
8076 ssl->out_len = ssl->out_cid;
8077 if( transform != NULL )
8078 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008079#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008080 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008081#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008082 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008083 }
8084 else
8085#endif
8086 {
8087 ssl->out_ctr = ssl->out_hdr - 8;
8088 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008090 ssl->out_cid = ssl->out_len;
8091#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008092 ssl->out_iv = ssl->out_hdr + 5;
8093 }
8094
8095 /* Adjust out_msg to make space for explicit IV, if used. */
8096 if( transform != NULL &&
8097 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8098 {
8099 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8100 }
8101 else
8102 ssl->out_msg = ssl->out_iv;
8103}
8104
8105/* Once ssl->in_hdr as the address of the beginning of the
8106 * next incoming record is set, deduce the other pointers.
8107 *
8108 * Note: For TLS, we save the implicit record sequence number
8109 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8110 * and the caller has to make sure there's space for this.
8111 */
8112
Hanno Becker79594fd2019-05-08 09:38:41 +01008113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008114{
Hanno Becker79594fd2019-05-08 09:38:41 +01008115 /* This function sets the pointers to match the case
8116 * of unprotected TLS/DTLS records, with both ssl->in_iv
8117 * and ssl->in_msg pointing to the beginning of the record
8118 * content.
8119 *
8120 * When decrypting a protected record, ssl->in_msg
8121 * will be shifted to point to the beginning of the
8122 * record plaintext.
8123 */
8124
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008125#if defined(MBEDTLS_SSL_PROTO_DTLS)
8126 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8127 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008128 /* This sets the header pointers to match records
8129 * without CID. When we receive a record containing
8130 * a CID, the fields are shifted accordingly in
8131 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008132 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008133#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008134 ssl->in_cid = ssl->in_ctr + 8;
8135 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008136#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008137 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008138#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008139 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008140 }
8141 else
8142#endif
8143 {
8144 ssl->in_ctr = ssl->in_hdr - 8;
8145 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008146#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008147 ssl->in_cid = ssl->in_len;
8148#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008149 ssl->in_iv = ssl->in_hdr + 5;
8150 }
8151
Hanno Becker79594fd2019-05-08 09:38:41 +01008152 /* This will be adjusted at record decryption time. */
8153 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008154}
8155
Paul Bakker5121ce52009-01-03 21:22:43 +00008156/*
8157 * Initialize an SSL context
8158 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008159void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8160{
8161 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8162}
8163
8164/*
8165 * Setup an SSL context
8166 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008167
8168static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8169{
8170 /* Set the incoming and outgoing record pointers. */
8171#if defined(MBEDTLS_SSL_PROTO_DTLS)
8172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8173 {
8174 ssl->out_hdr = ssl->out_buf;
8175 ssl->in_hdr = ssl->in_buf;
8176 }
8177 else
8178#endif /* MBEDTLS_SSL_PROTO_DTLS */
8179 {
8180 ssl->out_hdr = ssl->out_buf + 8;
8181 ssl->in_hdr = ssl->in_buf + 8;
8182 }
8183
8184 /* Derive other internal pointers. */
8185 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008186 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008187}
8188
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008189int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008190 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008191{
Paul Bakker48916f92012-09-16 19:57:18 +00008192 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008193
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008194 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008195
8196 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008197 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008198 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008199
8200 /* Set to NULL in case of an error condition */
8201 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008202
Angus Grattond8213d02016-05-25 20:56:48 +10008203 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8204 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008205 {
Angus Grattond8213d02016-05-25 20:56:48 +10008206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008207 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008208 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008209 }
8210
8211 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8212 if( ssl->out_buf == NULL )
8213 {
8214 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008215 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008216 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008217 }
8218
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008219 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008220
Paul Bakker48916f92012-09-16 19:57:18 +00008221 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008222 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008223
8224 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008225
8226error:
8227 mbedtls_free( ssl->in_buf );
8228 mbedtls_free( ssl->out_buf );
8229
8230 ssl->conf = NULL;
8231
8232 ssl->in_buf = NULL;
8233 ssl->out_buf = NULL;
8234
8235 ssl->in_hdr = NULL;
8236 ssl->in_ctr = NULL;
8237 ssl->in_len = NULL;
8238 ssl->in_iv = NULL;
8239 ssl->in_msg = NULL;
8240
8241 ssl->out_hdr = NULL;
8242 ssl->out_ctr = NULL;
8243 ssl->out_len = NULL;
8244 ssl->out_iv = NULL;
8245 ssl->out_msg = NULL;
8246
k-stachowiak9f7798e2018-07-31 16:52:32 +02008247 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008248}
8249
8250/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008251 * Reset an initialized and used SSL context for re-use while retaining
8252 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008253 *
8254 * If partial is non-zero, keep data in the input buffer and client ID.
8255 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008256 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008257static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008258{
Paul Bakker48916f92012-09-16 19:57:18 +00008259 int ret;
8260
Hanno Becker7e772132018-08-10 12:38:21 +01008261#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8262 !defined(MBEDTLS_SSL_SRV_C)
8263 ((void) partial);
8264#endif
8265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008266 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008267
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008268 /* Cancel any possibly running timer */
8269 ssl_set_timer( ssl, 0 );
8270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008271#if defined(MBEDTLS_SSL_RENEGOTIATION)
8272 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008273 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008274
8275 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008276 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8277 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008278#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008280
Paul Bakker7eb013f2011-10-06 12:37:39 +00008281 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008282 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008283
8284 ssl->in_msgtype = 0;
8285 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008287 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008288 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008289#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008291 ssl_dtls_replay_reset( ssl );
8292#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008293
8294 ssl->in_hslen = 0;
8295 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008296
8297 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008298
8299 ssl->out_msgtype = 0;
8300 ssl->out_msglen = 0;
8301 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8303 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008304 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008305#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008306
Hanno Becker19859472018-08-06 09:40:20 +01008307 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8308
Paul Bakker48916f92012-09-16 19:57:18 +00008309 ssl->transform_in = NULL;
8310 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008311
Hanno Becker78640902018-08-13 16:35:15 +01008312 ssl->session_in = NULL;
8313 ssl->session_out = NULL;
8314
Angus Grattond8213d02016-05-25 20:56:48 +10008315 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008316
8317#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008318 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008319#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8320 {
8321 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008322 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008323 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008325#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8326 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8329 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008331 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8332 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008333 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008334 }
8335#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008336
Paul Bakker48916f92012-09-16 19:57:18 +00008337 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339 mbedtls_ssl_transform_free( ssl->transform );
8340 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008341 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008342 }
Paul Bakker48916f92012-09-16 19:57:18 +00008343
Paul Bakkerc0463502013-02-14 11:19:38 +01008344 if( ssl->session )
8345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 mbedtls_ssl_session_free( ssl->session );
8347 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008348 ssl->session = NULL;
8349 }
8350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008352 ssl->alpn_chosen = NULL;
8353#endif
8354
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008355#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008356#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008357 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008358#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008359 {
8360 mbedtls_free( ssl->cli_id );
8361 ssl->cli_id = NULL;
8362 ssl->cli_id_len = 0;
8363 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008364#endif
8365
Paul Bakker48916f92012-09-16 19:57:18 +00008366 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8367 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008368
8369 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008370}
8371
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008372/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008373 * Reset an initialized and used SSL context for re-use while retaining
8374 * all application-set variables, function pointers and data.
8375 */
8376int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8377{
8378 return( ssl_session_reset_int( ssl, 0 ) );
8379}
8380
8381/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008382 * SSL set accessors
8383 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008384void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008385{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008386 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008387}
8388
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008389void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008390{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008391 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008392}
8393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008394#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008395void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008396{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008397 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008398}
8399#endif
8400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008401#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008402void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008403{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008404 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008405}
8406#endif
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008409
Hanno Becker1841b0a2018-08-24 11:13:57 +01008410void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8411 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008412{
8413 ssl->disable_datagram_packing = !allow_packing;
8414}
8415
8416void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8417 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008419 conf->hs_timeout_min = min;
8420 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008421}
8422#endif
8423
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008424void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008425{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008426 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008427}
8428
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008429#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008430void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008431 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008432 void *p_vrfy )
8433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->f_vrfy = f_vrfy;
8435 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008437#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008438
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008439void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008440 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008441 void *p_rng )
8442{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008443 conf->f_rng = f_rng;
8444 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008445}
8446
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008447void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008448 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008449 void *p_dbg )
8450{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008451 conf->f_dbg = f_dbg;
8452 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008453}
8454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008455void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008456 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008457 mbedtls_ssl_send_t *f_send,
8458 mbedtls_ssl_recv_t *f_recv,
8459 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008460{
8461 ssl->p_bio = p_bio;
8462 ssl->f_send = f_send;
8463 ssl->f_recv = f_recv;
8464 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008465}
8466
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008467#if defined(MBEDTLS_SSL_PROTO_DTLS)
8468void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8469{
8470 ssl->mtu = mtu;
8471}
8472#endif
8473
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008474void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008475{
8476 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008477}
8478
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008479void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8480 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008481 mbedtls_ssl_set_timer_t *f_set_timer,
8482 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008483{
8484 ssl->p_timer = p_timer;
8485 ssl->f_set_timer = f_set_timer;
8486 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008487
8488 /* Make sure we start with no timer running */
8489 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008490}
8491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008492#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008493void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008494 void *p_cache,
8495 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8496 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008497{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008498 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008499 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008500 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008502#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504#if defined(MBEDTLS_SSL_CLI_C)
8505int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008506{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008507 int ret;
8508
8509 if( ssl == NULL ||
8510 session == NULL ||
8511 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008512 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008515 }
8516
Hanno Becker52055ae2019-02-06 14:30:46 +00008517 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8518 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008519 return( ret );
8520
Paul Bakker0a597072012-09-25 21:55:46 +00008521 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008522
8523 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008524}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008525#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008526
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008527void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008528 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008529{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008530 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8531 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8532 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8533 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008534}
8535
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008536void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008537 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008538 int major, int minor )
8539{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008541 return;
8542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008543 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008544 return;
8545
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008546 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008547}
8548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008550void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008551 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008552{
8553 conf->cert_profile = profile;
8554}
8555
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008556/* Append a new keycert entry to a (possibly empty) list */
8557static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8558 mbedtls_x509_crt *cert,
8559 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008560{
niisato8ee24222018-06-25 19:05:48 +09008561 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008562
niisato8ee24222018-06-25 19:05:48 +09008563 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8564 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008565 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008566
niisato8ee24222018-06-25 19:05:48 +09008567 new_cert->cert = cert;
8568 new_cert->key = key;
8569 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008570
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008571 /* Update head is the list was null, else add to the end */
8572 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008573 {
niisato8ee24222018-06-25 19:05:48 +09008574 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008575 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008576 else
8577 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008578 mbedtls_ssl_key_cert *cur = *head;
8579 while( cur->next != NULL )
8580 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008581 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008582 }
8583
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008584 return( 0 );
8585}
8586
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008587int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008588 mbedtls_x509_crt *own_cert,
8589 mbedtls_pk_context *pk_key )
8590{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008591 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008592}
8593
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008594void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008595 mbedtls_x509_crt *ca_chain,
8596 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008597{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008598 conf->ca_chain = ca_chain;
8599 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008600
8601#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8602 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8603 * cannot be used together. */
8604 conf->f_ca_cb = NULL;
8605 conf->p_ca_cb = NULL;
8606#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008607}
Hanno Becker5adaad92019-03-27 16:54:37 +00008608
8609#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8610void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008611 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008612 void *p_ca_cb )
8613{
8614 conf->f_ca_cb = f_ca_cb;
8615 conf->p_ca_cb = p_ca_cb;
8616
8617 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8618 * cannot be used together. */
8619 conf->ca_chain = NULL;
8620 conf->ca_crl = NULL;
8621}
8622#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008623#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008624
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008625#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8626int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8627 mbedtls_x509_crt *own_cert,
8628 mbedtls_pk_context *pk_key )
8629{
8630 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8631 own_cert, pk_key ) );
8632}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008633
8634void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8635 mbedtls_x509_crt *ca_chain,
8636 mbedtls_x509_crl *ca_crl )
8637{
8638 ssl->handshake->sni_ca_chain = ca_chain;
8639 ssl->handshake->sni_ca_crl = ca_crl;
8640}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008641
8642void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8643 int authmode )
8644{
8645 ssl->handshake->sni_authmode = authmode;
8646}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008647#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8648
Hanno Becker8927c832019-04-03 12:52:50 +01008649#if defined(MBEDTLS_X509_CRT_PARSE_C)
8650void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8651 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8652 void *p_vrfy )
8653{
8654 ssl->f_vrfy = f_vrfy;
8655 ssl->p_vrfy = p_vrfy;
8656}
8657#endif
8658
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008659#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008660/*
8661 * Set EC J-PAKE password for current handshake
8662 */
8663int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8664 const unsigned char *pw,
8665 size_t pw_len )
8666{
8667 mbedtls_ecjpake_role role;
8668
Janos Follath8eb64132016-06-03 15:40:57 +01008669 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008670 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8671
8672 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8673 role = MBEDTLS_ECJPAKE_SERVER;
8674 else
8675 role = MBEDTLS_ECJPAKE_CLIENT;
8676
8677 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8678 role,
8679 MBEDTLS_MD_SHA256,
8680 MBEDTLS_ECP_DP_SECP256R1,
8681 pw, pw_len ) );
8682}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008683#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008685#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008686
8687static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8688{
8689 /* Remove reference to existing PSK, if any. */
8690#if defined(MBEDTLS_USE_PSA_CRYPTO)
8691 if( conf->psk_opaque != 0 )
8692 {
8693 /* The maintenance of the PSK key slot is the
8694 * user's responsibility. */
8695 conf->psk_opaque = 0;
8696 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008697 /* This and the following branch should never
8698 * be taken simultaenously as we maintain the
8699 * invariant that raw and opaque PSKs are never
8700 * configured simultaneously. As a safeguard,
8701 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008702#endif /* MBEDTLS_USE_PSA_CRYPTO */
8703 if( conf->psk != NULL )
8704 {
8705 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8706
8707 mbedtls_free( conf->psk );
8708 conf->psk = NULL;
8709 conf->psk_len = 0;
8710 }
8711
8712 /* Remove reference to PSK identity, if any. */
8713 if( conf->psk_identity != NULL )
8714 {
8715 mbedtls_free( conf->psk_identity );
8716 conf->psk_identity = NULL;
8717 conf->psk_identity_len = 0;
8718 }
8719}
8720
Hanno Becker7390c712018-11-15 13:33:04 +00008721/* This function assumes that PSK identity in the SSL config is unset.
8722 * It checks that the provided identity is well-formed and attempts
8723 * to make a copy of it in the SSL config.
8724 * On failure, the PSK identity in the config remains unset. */
8725static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8726 unsigned char const *psk_identity,
8727 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008728{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008729 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008730 if( psk_identity == NULL ||
8731 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008732 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008733 {
8734 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8735 }
8736
Hanno Becker7390c712018-11-15 13:33:04 +00008737 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8738 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008739 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008740
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008741 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008742 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008743
8744 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008745}
8746
Hanno Becker7390c712018-11-15 13:33:04 +00008747int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8748 const unsigned char *psk, size_t psk_len,
8749 const unsigned char *psk_identity, size_t psk_identity_len )
8750{
8751 int ret;
8752 /* Remove opaque/raw PSK + PSK Identity */
8753 ssl_conf_remove_psk( conf );
8754
8755 /* Check and set raw PSK */
8756 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8757 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8758 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8759 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8760 conf->psk_len = psk_len;
8761 memcpy( conf->psk, psk, conf->psk_len );
8762
8763 /* Check and set PSK Identity */
8764 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8765 if( ret != 0 )
8766 ssl_conf_remove_psk( conf );
8767
8768 return( ret );
8769}
8770
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008771static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8772{
8773#if defined(MBEDTLS_USE_PSA_CRYPTO)
8774 if( ssl->handshake->psk_opaque != 0 )
8775 {
8776 ssl->handshake->psk_opaque = 0;
8777 }
8778 else
8779#endif /* MBEDTLS_USE_PSA_CRYPTO */
8780 if( ssl->handshake->psk != NULL )
8781 {
8782 mbedtls_platform_zeroize( ssl->handshake->psk,
8783 ssl->handshake->psk_len );
8784 mbedtls_free( ssl->handshake->psk );
8785 ssl->handshake->psk_len = 0;
8786 }
8787}
8788
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008789int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8790 const unsigned char *psk, size_t psk_len )
8791{
8792 if( psk == NULL || ssl->handshake == NULL )
8793 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8794
8795 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8796 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8797
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008798 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008799
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008800 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008801 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008802
8803 ssl->handshake->psk_len = psk_len;
8804 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8805
8806 return( 0 );
8807}
8808
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008809#if defined(MBEDTLS_USE_PSA_CRYPTO)
8810int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008811 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008812 const unsigned char *psk_identity,
8813 size_t psk_identity_len )
8814{
Hanno Becker7390c712018-11-15 13:33:04 +00008815 int ret;
8816 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008817 ssl_conf_remove_psk( conf );
8818
Hanno Becker7390c712018-11-15 13:33:04 +00008819 /* Check and set opaque PSK */
8820 if( psk_slot == 0 )
8821 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008822 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008823
8824 /* Check and set PSK Identity */
8825 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8826 psk_identity_len );
8827 if( ret != 0 )
8828 ssl_conf_remove_psk( conf );
8829
8830 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008831}
8832
8833int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008834 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008835{
8836 if( psk_slot == 0 || ssl->handshake == NULL )
8837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8838
8839 ssl_remove_psk( ssl );
8840 ssl->handshake->psk_opaque = psk_slot;
8841 return( 0 );
8842}
8843#endif /* MBEDTLS_USE_PSA_CRYPTO */
8844
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008845void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008846 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008847 size_t),
8848 void *p_psk )
8849{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008850 conf->f_psk = f_psk;
8851 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008852}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008853#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008854
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008855#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008856
8857#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008858int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008859{
8860 int ret;
8861
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008862 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8863 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8864 {
8865 mbedtls_mpi_free( &conf->dhm_P );
8866 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008867 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008869
8870 return( 0 );
8871}
Hanno Becker470a8c42017-10-04 15:28:46 +01008872#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008873
Hanno Beckera90658f2017-10-04 15:29:08 +01008874int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8875 const unsigned char *dhm_P, size_t P_len,
8876 const unsigned char *dhm_G, size_t G_len )
8877{
8878 int ret;
8879
8880 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8881 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8882 {
8883 mbedtls_mpi_free( &conf->dhm_P );
8884 mbedtls_mpi_free( &conf->dhm_G );
8885 return( ret );
8886 }
8887
8888 return( 0 );
8889}
Paul Bakker5121ce52009-01-03 21:22:43 +00008890
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008891int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008892{
8893 int ret;
8894
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008895 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8896 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8897 {
8898 mbedtls_mpi_free( &conf->dhm_P );
8899 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008900 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008901 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008902
8903 return( 0 );
8904}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008905#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008906
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008907#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8908/*
8909 * Set the minimum length for Diffie-Hellman parameters
8910 */
8911void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8912 unsigned int bitlen )
8913{
8914 conf->dhm_min_bitlen = bitlen;
8915}
8916#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8917
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008918#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008919/*
8920 * Set allowed/preferred hashes for handshake signatures
8921 */
8922void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8923 const int *hashes )
8924{
8925 conf->sig_hashes = hashes;
8926}
Hanno Becker947194e2017-04-07 13:25:49 +01008927#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008928
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008929#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008930/*
8931 * Set the allowed elliptic curves
8932 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008933void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008934 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008935{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008936 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008937}
Hanno Becker947194e2017-04-07 13:25:49 +01008938#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008939
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008940#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008941int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008942{
Hanno Becker947194e2017-04-07 13:25:49 +01008943 /* Initialize to suppress unnecessary compiler warning */
8944 size_t hostname_len = 0;
8945
8946 /* Check if new hostname is valid before
8947 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008948 if( hostname != NULL )
8949 {
8950 hostname_len = strlen( hostname );
8951
8952 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8953 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8954 }
8955
8956 /* Now it's clear that we will overwrite the old hostname,
8957 * so we can free it safely */
8958
8959 if( ssl->hostname != NULL )
8960 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008961 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008962 mbedtls_free( ssl->hostname );
8963 }
8964
8965 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008966
Paul Bakker5121ce52009-01-03 21:22:43 +00008967 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008968 {
8969 ssl->hostname = NULL;
8970 }
8971 else
8972 {
8973 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008974 if( ssl->hostname == NULL )
8975 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008976
Hanno Becker947194e2017-04-07 13:25:49 +01008977 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008978
Hanno Becker947194e2017-04-07 13:25:49 +01008979 ssl->hostname[hostname_len] = '\0';
8980 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008981
8982 return( 0 );
8983}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008984#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008985
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008986#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008987void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008988 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008989 const unsigned char *, size_t),
8990 void *p_sni )
8991{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008992 conf->f_sni = f_sni;
8993 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008994}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008995#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008998int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008999{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009000 size_t cur_len, tot_len;
9001 const char **p;
9002
9003 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009004 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9005 * MUST NOT be truncated."
9006 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009007 */
9008 tot_len = 0;
9009 for( p = protos; *p != NULL; p++ )
9010 {
9011 cur_len = strlen( *p );
9012 tot_len += cur_len;
9013
9014 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009015 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009016 }
9017
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009018 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009019
9020 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009021}
9022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009023const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009024{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009025 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009026}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009028
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009029void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009030{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009031 conf->max_major_ver = major;
9032 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009033}
9034
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009035void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009036{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009037 conf->min_major_ver = major;
9038 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009039}
9040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009042void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009043{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009044 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009045}
9046#endif
9047
Janos Follath088ce432017-04-10 12:42:31 +01009048#if defined(MBEDTLS_SSL_SRV_C)
9049void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9050 char cert_req_ca_list )
9051{
9052 conf->cert_req_ca_list = cert_req_ca_list;
9053}
9054#endif
9055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009057void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009058{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009059 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009060}
9061#endif
9062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009063#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009064void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009065{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009066 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009067}
9068#endif
9069
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009070#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009071void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009072{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009073 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009074}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009075#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009078int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009079{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009080 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009081 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009083 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009084 }
9085
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009086 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009087
9088 return( 0 );
9089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009093void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009095 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009096}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009100void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009101{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009102 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009103}
9104#endif
9105
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009106void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009108 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009109}
9110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009113{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009114 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009115}
9116
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009117void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009118{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009119 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009120}
9121
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009122void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009123 const unsigned char period[8] )
9124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009125 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009126}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009129#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009130#if defined(MBEDTLS_SSL_CLI_C)
9131void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009132{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009133 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009134}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009135#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009136
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009137#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009138void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9139 mbedtls_ssl_ticket_write_t *f_ticket_write,
9140 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9141 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009142{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009143 conf->f_ticket_write = f_ticket_write;
9144 conf->f_ticket_parse = f_ticket_parse;
9145 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009146}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009147#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009148#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009149
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009150#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9151void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9152 mbedtls_ssl_export_keys_t *f_export_keys,
9153 void *p_export_keys )
9154{
9155 conf->f_export_keys = f_export_keys;
9156 conf->p_export_keys = p_export_keys;
9157}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009158
9159void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9160 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9161 void *p_export_keys )
9162{
9163 conf->f_export_keys_ext = f_export_keys_ext;
9164 conf->p_export_keys = p_export_keys;
9165}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009166#endif
9167
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009168#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009169void mbedtls_ssl_conf_async_private_cb(
9170 mbedtls_ssl_config *conf,
9171 mbedtls_ssl_async_sign_t *f_async_sign,
9172 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9173 mbedtls_ssl_async_resume_t *f_async_resume,
9174 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009175 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009176{
9177 conf->f_async_sign_start = f_async_sign;
9178 conf->f_async_decrypt_start = f_async_decrypt;
9179 conf->f_async_resume = f_async_resume;
9180 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009181 conf->p_async_config_data = async_config_data;
9182}
9183
Gilles Peskine8f97af72018-04-26 11:46:10 +02009184void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9185{
9186 return( conf->p_async_config_data );
9187}
9188
Gilles Peskine1febfef2018-04-30 11:54:39 +02009189void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009190{
9191 if( ssl->handshake == NULL )
9192 return( NULL );
9193 else
9194 return( ssl->handshake->user_async_ctx );
9195}
9196
Gilles Peskine1febfef2018-04-30 11:54:39 +02009197void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009198 void *ctx )
9199{
9200 if( ssl->handshake != NULL )
9201 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009202}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009203#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009204
Paul Bakker5121ce52009-01-03 21:22:43 +00009205/*
9206 * SSL get accessors
9207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009208size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009209{
9210 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9211}
9212
Hanno Becker8b170a02017-10-10 11:51:19 +01009213int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9214{
9215 /*
9216 * Case A: We're currently holding back
9217 * a message for further processing.
9218 */
9219
9220 if( ssl->keep_current_message == 1 )
9221 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009223 return( 1 );
9224 }
9225
9226 /*
9227 * Case B: Further records are pending in the current datagram.
9228 */
9229
9230#if defined(MBEDTLS_SSL_PROTO_DTLS)
9231 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9232 ssl->in_left > ssl->next_record_offset )
9233 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009235 return( 1 );
9236 }
9237#endif /* MBEDTLS_SSL_PROTO_DTLS */
9238
9239 /*
9240 * Case C: A handshake message is being processed.
9241 */
9242
Hanno Becker8b170a02017-10-10 11:51:19 +01009243 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9244 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009245 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009246 return( 1 );
9247 }
9248
9249 /*
9250 * Case D: An application data message is being processed
9251 */
9252 if( ssl->in_offt != NULL )
9253 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009255 return( 1 );
9256 }
9257
9258 /*
9259 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009260 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 * we implement support for multiple alerts in single records.
9262 */
9263
9264 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9265 return( 0 );
9266}
9267
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009268uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009269{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009270 if( ssl->session != NULL )
9271 return( ssl->session->verify_result );
9272
9273 if( ssl->session_negotiate != NULL )
9274 return( ssl->session_negotiate->verify_result );
9275
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009276 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009277}
9278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009279const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009280{
Paul Bakker926c8e42013-03-06 10:23:34 +01009281 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009282 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009284 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009285}
9286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009287const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009288{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009289#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009290 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009291 {
9292 switch( ssl->minor_ver )
9293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009295 return( "DTLSv1.0" );
9296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009298 return( "DTLSv1.2" );
9299
9300 default:
9301 return( "unknown (DTLS)" );
9302 }
9303 }
9304#endif
9305
Paul Bakker43ca69c2011-01-15 17:35:19 +00009306 switch( ssl->minor_ver )
9307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009309 return( "SSLv3.0" );
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009312 return( "TLSv1.0" );
9313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009314 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315 return( "TLSv1.1" );
9316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009318 return( "TLSv1.2" );
9319
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009321 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009322 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009323}
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009326{
Hanno Becker3136ede2018-08-17 15:28:19 +01009327 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009329 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009330
Hanno Becker5903de42019-05-03 14:46:38 +01009331 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9332
Hanno Becker78640902018-08-13 16:35:15 +01009333 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009334 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009336#if defined(MBEDTLS_ZLIB_SUPPORT)
9337 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9338 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009339#endif
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 case MBEDTLS_MODE_GCM:
9344 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009345 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009346 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009347 transform_expansion = transform->minlen;
9348 break;
9349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009351
9352 block_size = mbedtls_cipher_get_block_size(
9353 &transform->cipher_ctx_enc );
9354
Hanno Becker3136ede2018-08-17 15:28:19 +01009355 /* Expansion due to the addition of the MAC. */
9356 transform_expansion += transform->maclen;
9357
9358 /* Expansion due to the addition of CBC padding;
9359 * Theoretically up to 256 bytes, but we never use
9360 * more than the block size of the underlying cipher. */
9361 transform_expansion += block_size;
9362
9363 /* For TLS 1.1 or higher, an explicit IV is added
9364 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009365#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9366 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009367 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009368#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009369
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009370 break;
9371
9372 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009373 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009374 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009375 }
9376
Hanno Beckera0e20d02019-05-15 14:03:01 +01009377#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009378 if( transform->out_cid_len != 0 )
9379 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009380#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009381
Hanno Becker5903de42019-05-03 14:46:38 +01009382 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009383}
9384
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009385#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9386size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9387{
9388 size_t max_len;
9389
9390 /*
9391 * Assume mfl_code is correct since it was checked when set
9392 */
Angus Grattond8213d02016-05-25 20:56:48 +10009393 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009394
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009395 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009396 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009397 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009398 {
Angus Grattond8213d02016-05-25 20:56:48 +10009399 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009400 }
9401
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009402 /* During a handshake, use the value being negotiated */
9403 if( ssl->session_negotiate != NULL &&
9404 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9405 {
9406 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9407 }
9408
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009409 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009410}
9411#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9412
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009413#if defined(MBEDTLS_SSL_PROTO_DTLS)
9414static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9415{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009416 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9417 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9418 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9419 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9420 return ( 0 );
9421
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009422 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9423 return( ssl->mtu );
9424
9425 if( ssl->mtu == 0 )
9426 return( ssl->handshake->mtu );
9427
9428 return( ssl->mtu < ssl->handshake->mtu ?
9429 ssl->mtu : ssl->handshake->mtu );
9430}
9431#endif /* MBEDTLS_SSL_PROTO_DTLS */
9432
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009433int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9434{
9435 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9436
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009437#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9438 !defined(MBEDTLS_SSL_PROTO_DTLS)
9439 (void) ssl;
9440#endif
9441
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009442#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9443 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9444
9445 if( max_len > mfl )
9446 max_len = mfl;
9447#endif
9448
9449#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009450 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009451 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009452 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009453 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9454 const size_t overhead = (size_t) ret;
9455
9456 if( ret < 0 )
9457 return( ret );
9458
9459 if( mtu <= overhead )
9460 {
9461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9462 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9463 }
9464
9465 if( max_len > mtu - overhead )
9466 max_len = mtu - overhead;
9467 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009468#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009469
Hanno Becker0defedb2018-08-10 12:35:02 +01009470#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9471 !defined(MBEDTLS_SSL_PROTO_DTLS)
9472 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009473#endif
9474
9475 return( (int) max_len );
9476}
9477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009478#if defined(MBEDTLS_X509_CRT_PARSE_C)
9479const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009480{
9481 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009482 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009483
Hanno Beckere6824572019-02-07 13:18:46 +00009484#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009485 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009486#else
9487 return( NULL );
9488#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009489}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009493int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9494 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009495{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009496 if( ssl == NULL ||
9497 dst == NULL ||
9498 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009499 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009500 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009502 }
9503
Hanno Becker52055ae2019-02-06 14:30:46 +00009504 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009507
Paul Bakker5121ce52009-01-03 21:22:43 +00009508/*
Paul Bakker1961b702013-01-25 14:49:24 +01009509 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009512{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009514
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009515 if( ssl == NULL || ssl->conf == NULL )
9516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009519 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009521#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009523 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009525#endif
9526
Paul Bakker1961b702013-01-25 14:49:24 +01009527 return( ret );
9528}
9529
9530/*
9531 * Perform the SSL handshake
9532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009534{
9535 int ret = 0;
9536
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009537 if( ssl == NULL || ssl->conf == NULL )
9538 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009544 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009545
9546 if( ret != 0 )
9547 break;
9548 }
9549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009551
9552 return( ret );
9553}
9554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555#if defined(MBEDTLS_SSL_RENEGOTIATION)
9556#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009557/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009558 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009561{
9562 int ret;
9563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009565
9566 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9568 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009569
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009570 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009571 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009572 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009573 return( ret );
9574 }
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577
9578 return( 0 );
9579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009581
9582/*
9583 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009584 * - any side: calling mbedtls_ssl_renegotiate(),
9585 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9586 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009587 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009588 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009590 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009592{
9593 int ret;
9594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009596
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009597 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9598 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009599
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009600 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9601 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009603 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009605 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009606 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009607 ssl->handshake->out_msg_seq = 1;
9608 else
9609 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009610 }
9611#endif
9612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9614 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009619 return( ret );
9620 }
9621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009623
9624 return( 0 );
9625}
9626
9627/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009628 * Renegotiate current connection on client,
9629 * or request renegotiation on server
9630 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009632{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009634
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009635 if( ssl == NULL || ssl->conf == NULL )
9636 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009639 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009640 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9643 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009646
9647 /* Did we already try/start sending HelloRequest? */
9648 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009650
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009651 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009652 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009656 /*
9657 * On client, either start the renegotiation process or,
9658 * if already in progress, continue the handshake
9659 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009661 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9663 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009664
9665 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009668 return( ret );
9669 }
9670 }
9671 else
9672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009676 return( ret );
9677 }
9678 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009681 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009682}
9683
9684/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009685 * Check record counters and renegotiate if they're above the limit.
9686 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009688{
Andres AG2196c7f2016-12-15 17:01:16 +00009689 size_t ep_len = ssl_ep_len( ssl );
9690 int in_ctr_cmp;
9691 int out_ctr_cmp;
9692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9694 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009695 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009696 {
9697 return( 0 );
9698 }
9699
Andres AG2196c7f2016-12-15 17:01:16 +00009700 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9701 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009702 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009703 ssl->conf->renego_period + ep_len, 8 - ep_len );
9704
9705 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009706 {
9707 return( 0 );
9708 }
9709
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009711 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009713#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009714
9715/*
9716 * Receive application data decrypted from the SSL layer
9717 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009718int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009719{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009720 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009721 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009722
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009723 if( ssl == NULL || ssl->conf == NULL )
9724 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9725
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009729 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009730 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009732 return( ret );
9733
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009734 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009735 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009736 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009737 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009738 return( ret );
9739 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009740 }
9741#endif
9742
Hanno Becker4a810fb2017-05-24 16:27:30 +01009743 /*
9744 * Check if renegotiation is necessary and/or handshake is
9745 * in process. If yes, perform/continue, and fall through
9746 * if an unexpected packet is received while the client
9747 * is waiting for the ServerHello.
9748 *
9749 * (There is no equivalent to the last condition on
9750 * the server-side as it is not treated as within
9751 * a handshake while waiting for the ClientHello
9752 * after a renegotiation request.)
9753 */
9754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009755#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009756 ret = ssl_check_ctr_renegotiate( ssl );
9757 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9758 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009760 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009761 return( ret );
9762 }
9763#endif
9764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009765 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009768 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9769 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009772 return( ret );
9773 }
9774 }
9775
Hanno Beckere41158b2017-10-23 13:30:32 +01009776 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009777 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009778 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009779 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009780 if( ssl->f_get_timer != NULL &&
9781 ssl->f_get_timer( ssl->p_timer ) == -1 )
9782 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009783 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009784 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009785
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 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009788 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9789 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009790
Hanno Becker4a810fb2017-05-24 16:27:30 +01009791 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9792 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009793 }
9794
9795 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009796 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009797 {
9798 /*
9799 * OpenSSL sends empty messages to randomize the IV
9800 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009801 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009802 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009803 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009804 return( 0 );
9805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009807 return( ret );
9808 }
9809 }
9810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009812 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009814
Hanno Becker4a810fb2017-05-24 16:27:30 +01009815 /*
9816 * - For client-side, expect SERVER_HELLO_REQUEST.
9817 * - For server-side, expect CLIENT_HELLO.
9818 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9819 */
9820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009822 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009824 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009827
9828 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009830 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009831 {
9832 continue;
9833 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009834#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009836 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009837#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009838
Hanno Becker4a810fb2017-05-24 16:27:30 +01009839#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009840 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009844
9845 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009847 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009848 {
9849 continue;
9850 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009853 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009854#endif /* MBEDTLS_SSL_SRV_C */
9855
Hanno Becker21df7f92017-10-17 11:03:26 +01009856#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009857 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009858 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9859 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9860 ssl->conf->allow_legacy_renegotiation ==
9861 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9862 {
9863 /*
9864 * Accept renegotiation request
9865 */
Paul Bakker48916f92012-09-16 19:57:18 +00009866
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009867 /* DTLS clients need to know renego is server-initiated */
9868#if defined(MBEDTLS_SSL_PROTO_DTLS)
9869 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9870 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9871 {
9872 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9873 }
9874#endif
9875 ret = ssl_start_renegotiation( ssl );
9876 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9877 ret != 0 )
9878 {
9879 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9880 return( ret );
9881 }
9882 }
9883 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009884#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009885 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009886 /*
9887 * Refuse renegotiation
9888 */
9889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009890 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009892#if defined(MBEDTLS_SSL_PROTO_SSL3)
9893 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009894 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009895 /* SSLv3 does not have a "no_renegotiation" warning, so
9896 we send a fatal alert and abort the connection. */
9897 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9898 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9899 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009900 }
9901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9903#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9904 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9905 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009906 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9908 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9909 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009910 {
9911 return( ret );
9912 }
Paul Bakker48916f92012-09-16 19:57:18 +00009913 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009914 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9916 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009917 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9919 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009920 }
Paul Bakker48916f92012-09-16 19:57:18 +00009921 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009922
Hanno Becker90333da2017-10-10 11:27:13 +01009923 /* At this point, we don't know whether the renegotiation has been
9924 * completed or not. The cases to consider are the following:
9925 * 1) The renegotiation is complete. In this case, no new record
9926 * has been read yet.
9927 * 2) The renegotiation is incomplete because the client received
9928 * an application data record while awaiting the ServerHello.
9929 * 3) The renegotiation is incomplete because the client received
9930 * a non-handshake, non-application data message while awaiting
9931 * the ServerHello.
9932 * In each of these case, looping will be the proper action:
9933 * - For 1), the next iteration will read a new record and check
9934 * if it's application data.
9935 * - For 2), the loop condition isn't satisfied as application data
9936 * is present, hence continue is the same as break
9937 * - For 3), the loop condition is satisfied and read_record
9938 * will re-deliver the message that was held back by the client
9939 * when expecting the ServerHello.
9940 */
9941 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009942 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009943#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009945 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009946 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009947 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009948 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009949 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009950 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009951 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009952 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009953 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009954 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9959 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009960 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009962 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009963 }
9964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009966 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9968 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009969 }
9970
9971 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009972
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009973 /* We're going to return something now, cancel timer,
9974 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009976 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009977
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009978#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009979 /* If we requested renego but received AppData, resend HelloRequest.
9980 * Do it now, after setting in_offt, to avoid taking this branch
9981 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009983 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009985 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009986 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009987 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009989 return( ret );
9990 }
9991 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009992#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009993#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009994 }
9995
9996 n = ( len < ssl->in_msglen )
9997 ? len : ssl->in_msglen;
9998
9999 memcpy( buf, ssl->in_offt, n );
10000 ssl->in_msglen -= n;
10001
10002 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010003 {
10004 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010005 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010006 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010007 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010008 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010009 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010010 /* more data available */
10011 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010012 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010014 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010015
Paul Bakker23986e52011-04-24 08:57:21 +000010016 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010017}
10018
10019/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010020 * Send application data to be encrypted by the SSL layer, taking care of max
10021 * fragment length and buffer size.
10022 *
10023 * According to RFC 5246 Section 6.2.1:
10024 *
10025 * Zero-length fragments of Application data MAY be sent as they are
10026 * potentially useful as a traffic analysis countermeasure.
10027 *
10028 * Therefore, it is possible that the input message length is 0 and the
10029 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010030 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010031static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010032 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010033{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010034 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10035 const size_t max_len = (size_t) ret;
10036
10037 if( ret < 0 )
10038 {
10039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10040 return( ret );
10041 }
10042
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010043 if( len > max_len )
10044 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010046 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010047 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010049 "maximum fragment length: %d > %d",
10050 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010051 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010052 }
10053 else
10054#endif
10055 len = max_len;
10056 }
Paul Bakker887bd502011-06-08 13:10:54 +000010057
Paul Bakker5121ce52009-01-03 21:22:43 +000010058 if( ssl->out_left != 0 )
10059 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010060 /*
10061 * The user has previously tried to send the data and
10062 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10063 * written. In this case, we expect the high-level write function
10064 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10065 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010068 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010069 return( ret );
10070 }
10071 }
Paul Bakker887bd502011-06-08 13:10:54 +000010072 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010073 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010074 /*
10075 * The user is trying to send a message the first time, so we need to
10076 * copy the data into the internal buffers and setup the data structure
10077 * to keep track of partial writes
10078 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010079 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010081 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010082
Hanno Becker67bc7c32018-08-06 11:33:50 +010010083 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010085 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010086 return( ret );
10087 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010088 }
10089
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010090 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010091}
10092
10093/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010094 * Write application data, doing 1/n-1 splitting if necessary.
10095 *
10096 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010097 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010098 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010099 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010101static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010102 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010103{
10104 int ret;
10105
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010106 if( ssl->conf->cbc_record_splitting ==
10107 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010108 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10110 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10111 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010112 {
10113 return( ssl_write_real( ssl, buf, len ) );
10114 }
10115
10116 if( ssl->split_done == 0 )
10117 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010118 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010119 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010120 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010121 }
10122
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010123 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10124 return( ret );
10125 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010126
10127 return( ret + 1 );
10128}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010129#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010130
10131/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010132 * Write application data (public-facing wrapper)
10133 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010134int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010135{
10136 int ret;
10137
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010139
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010140 if( ssl == NULL || ssl->conf == NULL )
10141 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10142
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010143#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010144 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10145 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010146 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010147 return( ret );
10148 }
10149#endif
10150
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010151 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010152 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010153 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010154 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010155 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010156 return( ret );
10157 }
10158 }
10159
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010160#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010161 ret = ssl_write_split( ssl, buf, len );
10162#else
10163 ret = ssl_write_real( ssl, buf, len );
10164#endif
10165
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010166 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010167
10168 return( ret );
10169}
10170
10171/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010172 * Notify the peer that the connection is being closed
10173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010175{
10176 int ret;
10177
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010178 if( ssl == NULL || ssl->conf == NULL )
10179 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010182
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010183 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010184 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10189 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10190 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010191 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010193 return( ret );
10194 }
10195 }
10196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010198
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010199 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010200}
10201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010203{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010204 if( transform == NULL )
10205 return;
10206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010208 deflateEnd( &transform->ctx_deflate );
10209 inflateEnd( &transform->ctx_inflate );
10210#endif
10211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10213 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010214
Hanno Beckerd56ed242018-01-03 15:32:51 +000010215#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 mbedtls_md_free( &transform->md_ctx_enc );
10217 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010218#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010219
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010220 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010221}
10222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223#if defined(MBEDTLS_X509_CRT_PARSE_C)
10224static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010225{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010227
10228 while( cur != NULL )
10229 {
10230 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010232 cur = next;
10233 }
10234}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010236
Hanno Becker0271f962018-08-16 13:23:47 +010010237#if defined(MBEDTLS_SSL_PROTO_DTLS)
10238
10239static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10240{
10241 unsigned offset;
10242 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10243
10244 if( hs == NULL )
10245 return;
10246
Hanno Becker283f5ef2018-08-24 09:34:47 +010010247 ssl_free_buffered_record( ssl );
10248
Hanno Becker0271f962018-08-16 13:23:47 +010010249 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010250 ssl_buffering_free_slot( ssl, offset );
10251}
10252
10253static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10254 uint8_t slot )
10255{
10256 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10257 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010258
10259 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10260 return;
10261
Hanno Beckere605b192018-08-21 15:59:07 +010010262 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010263 {
Hanno Beckere605b192018-08-21 15:59:07 +010010264 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010265 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010266 mbedtls_free( hs_buf->data );
10267 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010268 }
10269}
10270
10271#endif /* MBEDTLS_SSL_PROTO_DTLS */
10272
Gilles Peskine9b562d52018-04-25 20:32:43 +020010273void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010274{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010275 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10276
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010277 if( handshake == NULL )
10278 return;
10279
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010280#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10281 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10282 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010283 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010284 handshake->async_in_progress = 0;
10285 }
10286#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10287
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010288#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10289 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10290 mbedtls_md5_free( &handshake->fin_md5 );
10291 mbedtls_sha1_free( &handshake->fin_sha1 );
10292#endif
10293#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10294#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010295#if defined(MBEDTLS_USE_PSA_CRYPTO)
10296 psa_hash_abort( &handshake->fin_sha256_psa );
10297#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010298 mbedtls_sha256_free( &handshake->fin_sha256 );
10299#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010300#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010301#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010302#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010303 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010304#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010305 mbedtls_sha512_free( &handshake->fin_sha512 );
10306#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010307#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010308#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010310#if defined(MBEDTLS_DHM_C)
10311 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010313#if defined(MBEDTLS_ECDH_C)
10314 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010315#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010316#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010317 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010318#if defined(MBEDTLS_SSL_CLI_C)
10319 mbedtls_free( handshake->ecjpake_cache );
10320 handshake->ecjpake_cache = NULL;
10321 handshake->ecjpake_cache_len = 0;
10322#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010323#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010324
Janos Follath4ae5c292016-02-10 11:27:43 +000010325#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10326 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010327 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010329#endif
10330
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010331#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10332 if( handshake->psk != NULL )
10333 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010334 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010335 mbedtls_free( handshake->psk );
10336 }
10337#endif
10338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010339#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10340 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010341 /*
10342 * Free only the linked list wrapper, not the keys themselves
10343 * since the belong to the SNI callback
10344 */
10345 if( handshake->sni_key_cert != NULL )
10346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010347 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010348
10349 while( cur != NULL )
10350 {
10351 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010353 cur = next;
10354 }
10355 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010357
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010358#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010359 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010360 if( handshake->ecrs_peer_cert != NULL )
10361 {
10362 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10363 mbedtls_free( handshake->ecrs_peer_cert );
10364 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010365#endif
10366
Hanno Becker75173122019-02-06 16:18:31 +000010367#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10368 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10369 mbedtls_pk_free( &handshake->peer_pubkey );
10370#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372#if defined(MBEDTLS_SSL_PROTO_DTLS)
10373 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010374 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010375 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010376#endif
10377
Hanno Becker4a63ed42019-01-08 11:39:35 +000010378#if defined(MBEDTLS_ECDH_C) && \
10379 defined(MBEDTLS_USE_PSA_CRYPTO)
10380 psa_destroy_key( handshake->ecdh_psa_privkey );
10381#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10382
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010383 mbedtls_platform_zeroize( handshake,
10384 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010385}
10386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010388{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010389 if( session == NULL )
10390 return;
10391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010392#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010393 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010394#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010395
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010396#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010397 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010398#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010399
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010400 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010401}
10402
Paul Bakker5121ce52009-01-03 21:22:43 +000010403/*
10404 * Free an SSL context
10405 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010407{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010408 if( ssl == NULL )
10409 return;
10410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010412
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010413 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010414 {
Angus Grattond8213d02016-05-25 20:56:48 +100010415 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010416 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010417 }
10418
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010419 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010420 {
Angus Grattond8213d02016-05-25 20:56:48 +100010421 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010423 }
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010426 if( ssl->compress_buf != NULL )
10427 {
Angus Grattond8213d02016-05-25 20:56:48 +100010428 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010430 }
10431#endif
10432
Paul Bakker48916f92012-09-16 19:57:18 +000010433 if( ssl->transform )
10434 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_ssl_transform_free( ssl->transform );
10436 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010437 }
10438
10439 if( ssl->handshake )
10440 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010441 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10443 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445 mbedtls_free( ssl->handshake );
10446 mbedtls_free( ssl->transform_negotiate );
10447 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010448 }
10449
Paul Bakkerc0463502013-02-14 11:19:38 +010010450 if( ssl->session )
10451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010452 mbedtls_ssl_session_free( ssl->session );
10453 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010454 }
10455
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010456#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010457 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010458 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010459 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010461 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010462#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10465 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10468 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010469 }
10470#endif
10471
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010472#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010474#endif
10475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010477
Paul Bakker86f04f42013-02-14 11:20:09 +010010478 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010479 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010480}
10481
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010482/*
10483 * Initialze mbedtls_ssl_config
10484 */
10485void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10486{
10487 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10488}
10489
Simon Butcherc97b6972015-12-27 23:48:17 +000010490#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010491static int ssl_preset_default_hashes[] = {
10492#if defined(MBEDTLS_SHA512_C)
10493 MBEDTLS_MD_SHA512,
10494 MBEDTLS_MD_SHA384,
10495#endif
10496#if defined(MBEDTLS_SHA256_C)
10497 MBEDTLS_MD_SHA256,
10498 MBEDTLS_MD_SHA224,
10499#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010500#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010501 MBEDTLS_MD_SHA1,
10502#endif
10503 MBEDTLS_MD_NONE
10504};
Simon Butcherc97b6972015-12-27 23:48:17 +000010505#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010506
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010507static int ssl_preset_suiteb_ciphersuites[] = {
10508 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10509 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10510 0
10511};
10512
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010513#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010514static int ssl_preset_suiteb_hashes[] = {
10515 MBEDTLS_MD_SHA256,
10516 MBEDTLS_MD_SHA384,
10517 MBEDTLS_MD_NONE
10518};
10519#endif
10520
10521#if defined(MBEDTLS_ECP_C)
10522static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010523#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010524 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010525#endif
10526#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010527 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010528#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010529 MBEDTLS_ECP_DP_NONE
10530};
10531#endif
10532
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010533/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010534 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010535 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010536int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010537 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010538{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010539#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010540 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010541#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010542
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010543 /* Use the functions here so that they are covered in tests,
10544 * but otherwise access member directly for efficiency */
10545 mbedtls_ssl_conf_endpoint( conf, endpoint );
10546 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010548 /*
10549 * Things that are common to all presets
10550 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010551#if defined(MBEDTLS_SSL_CLI_C)
10552 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10553 {
10554 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10555#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10556 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10557#endif
10558 }
10559#endif
10560
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010561#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010562 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010563#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010564
10565#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10566 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10567#endif
10568
10569#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10570 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10571#endif
10572
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010573#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10574 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10575#endif
10576
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010577#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010578 conf->f_cookie_write = ssl_cookie_write_dummy;
10579 conf->f_cookie_check = ssl_cookie_check_dummy;
10580#endif
10581
10582#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10583 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10584#endif
10585
Janos Follath088ce432017-04-10 12:42:31 +010010586#if defined(MBEDTLS_SSL_SRV_C)
10587 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10588#endif
10589
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010590#if defined(MBEDTLS_SSL_PROTO_DTLS)
10591 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10592 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10593#endif
10594
10595#if defined(MBEDTLS_SSL_RENEGOTIATION)
10596 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010597 memset( conf->renego_period, 0x00, 2 );
10598 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010599#endif
10600
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010601#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10602 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10603 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010604 const unsigned char dhm_p[] =
10605 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10606 const unsigned char dhm_g[] =
10607 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10608
Hanno Beckera90658f2017-10-04 15:29:08 +010010609 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10610 dhm_p, sizeof( dhm_p ),
10611 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010612 {
10613 return( ret );
10614 }
10615 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010616#endif
10617
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010618 /*
10619 * Preset-specific defaults
10620 */
10621 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010622 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010623 /*
10624 * NSA Suite B
10625 */
10626 case MBEDTLS_SSL_PRESET_SUITEB:
10627 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10628 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10629 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10630 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10631
10632 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10633 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10634 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10635 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10636 ssl_preset_suiteb_ciphersuites;
10637
10638#if defined(MBEDTLS_X509_CRT_PARSE_C)
10639 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010640#endif
10641
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010642#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010643 conf->sig_hashes = ssl_preset_suiteb_hashes;
10644#endif
10645
10646#if defined(MBEDTLS_ECP_C)
10647 conf->curve_list = ssl_preset_suiteb_curves;
10648#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010649 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010650
10651 /*
10652 * Default
10653 */
10654 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010655 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10656 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10657 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10658 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10659 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10660 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10661 MBEDTLS_SSL_MIN_MINOR_VERSION :
10662 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010663 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10664 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10665
10666#if defined(MBEDTLS_SSL_PROTO_DTLS)
10667 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10668 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10669#endif
10670
10671 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10672 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10673 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10674 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10675 mbedtls_ssl_list_ciphersuites();
10676
10677#if defined(MBEDTLS_X509_CRT_PARSE_C)
10678 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10679#endif
10680
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010681#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010682 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010683#endif
10684
10685#if defined(MBEDTLS_ECP_C)
10686 conf->curve_list = mbedtls_ecp_grp_id_list();
10687#endif
10688
10689#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10690 conf->dhm_min_bitlen = 1024;
10691#endif
10692 }
10693
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010694 return( 0 );
10695}
10696
10697/*
10698 * Free mbedtls_ssl_config
10699 */
10700void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10701{
10702#if defined(MBEDTLS_DHM_C)
10703 mbedtls_mpi_free( &conf->dhm_P );
10704 mbedtls_mpi_free( &conf->dhm_G );
10705#endif
10706
10707#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10708 if( conf->psk != NULL )
10709 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010710 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010711 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010712 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010713 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010714 }
10715
10716 if( conf->psk_identity != NULL )
10717 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010718 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010719 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010720 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721 conf->psk_identity_len = 0;
10722 }
10723#endif
10724
10725#if defined(MBEDTLS_X509_CRT_PARSE_C)
10726 ssl_key_cert_free( conf->key_cert );
10727#endif
10728
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010729 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730}
10731
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010732#if defined(MBEDTLS_PK_C) && \
10733 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010734/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010735 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010737unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010738{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010739#if defined(MBEDTLS_RSA_C)
10740 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10741 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010742#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010743#if defined(MBEDTLS_ECDSA_C)
10744 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10745 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010746#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010748}
10749
Hanno Becker7e5437a2017-04-28 17:15:26 +010010750unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10751{
10752 switch( type ) {
10753 case MBEDTLS_PK_RSA:
10754 return( MBEDTLS_SSL_SIG_RSA );
10755 case MBEDTLS_PK_ECDSA:
10756 case MBEDTLS_PK_ECKEY:
10757 return( MBEDTLS_SSL_SIG_ECDSA );
10758 default:
10759 return( MBEDTLS_SSL_SIG_ANON );
10760 }
10761}
10762
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010764{
10765 switch( sig )
10766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010767#if defined(MBEDTLS_RSA_C)
10768 case MBEDTLS_SSL_SIG_RSA:
10769 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010770#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010771#if defined(MBEDTLS_ECDSA_C)
10772 case MBEDTLS_SSL_SIG_ECDSA:
10773 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010774#endif
10775 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010777 }
10778}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010779#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010780
Hanno Becker7e5437a2017-04-28 17:15:26 +010010781#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10782 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10783
10784/* Find an entry in a signature-hash set matching a given hash algorithm. */
10785mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10786 mbedtls_pk_type_t sig_alg )
10787{
10788 switch( sig_alg )
10789 {
10790 case MBEDTLS_PK_RSA:
10791 return( set->rsa );
10792 case MBEDTLS_PK_ECDSA:
10793 return( set->ecdsa );
10794 default:
10795 return( MBEDTLS_MD_NONE );
10796 }
10797}
10798
10799/* Add a signature-hash-pair to a signature-hash set */
10800void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10801 mbedtls_pk_type_t sig_alg,
10802 mbedtls_md_type_t md_alg )
10803{
10804 switch( sig_alg )
10805 {
10806 case MBEDTLS_PK_RSA:
10807 if( set->rsa == MBEDTLS_MD_NONE )
10808 set->rsa = md_alg;
10809 break;
10810
10811 case MBEDTLS_PK_ECDSA:
10812 if( set->ecdsa == MBEDTLS_MD_NONE )
10813 set->ecdsa = md_alg;
10814 break;
10815
10816 default:
10817 break;
10818 }
10819}
10820
10821/* Allow exactly one hash algorithm for each signature. */
10822void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10823 mbedtls_md_type_t md_alg )
10824{
10825 set->rsa = md_alg;
10826 set->ecdsa = md_alg;
10827}
10828
10829#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10830 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10831
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010832/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010833 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010834 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010835mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010836{
10837 switch( hash )
10838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010839#if defined(MBEDTLS_MD5_C)
10840 case MBEDTLS_SSL_HASH_MD5:
10841 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010842#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010843#if defined(MBEDTLS_SHA1_C)
10844 case MBEDTLS_SSL_HASH_SHA1:
10845 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847#if defined(MBEDTLS_SHA256_C)
10848 case MBEDTLS_SSL_HASH_SHA224:
10849 return( MBEDTLS_MD_SHA224 );
10850 case MBEDTLS_SSL_HASH_SHA256:
10851 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_SHA512_C)
10854 case MBEDTLS_SSL_HASH_SHA384:
10855 return( MBEDTLS_MD_SHA384 );
10856 case MBEDTLS_SSL_HASH_SHA512:
10857 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010858#endif
10859 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010860 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010861 }
10862}
10863
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010864/*
10865 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10866 */
10867unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10868{
10869 switch( md )
10870 {
10871#if defined(MBEDTLS_MD5_C)
10872 case MBEDTLS_MD_MD5:
10873 return( MBEDTLS_SSL_HASH_MD5 );
10874#endif
10875#if defined(MBEDTLS_SHA1_C)
10876 case MBEDTLS_MD_SHA1:
10877 return( MBEDTLS_SSL_HASH_SHA1 );
10878#endif
10879#if defined(MBEDTLS_SHA256_C)
10880 case MBEDTLS_MD_SHA224:
10881 return( MBEDTLS_SSL_HASH_SHA224 );
10882 case MBEDTLS_MD_SHA256:
10883 return( MBEDTLS_SSL_HASH_SHA256 );
10884#endif
10885#if defined(MBEDTLS_SHA512_C)
10886 case MBEDTLS_MD_SHA384:
10887 return( MBEDTLS_SSL_HASH_SHA384 );
10888 case MBEDTLS_MD_SHA512:
10889 return( MBEDTLS_SSL_HASH_SHA512 );
10890#endif
10891 default:
10892 return( MBEDTLS_SSL_HASH_NONE );
10893 }
10894}
10895
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010896#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010897/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010898 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010899 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010900 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010901int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010902{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010904
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010905 if( ssl->conf->curve_list == NULL )
10906 return( -1 );
10907
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010908 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010909 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010910 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010912 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010913}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010914#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010915
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010916#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010917/*
10918 * Check if a hash proposed by the peer is in our list.
10919 * Return 0 if we're willing to use it, -1 otherwise.
10920 */
10921int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10922 mbedtls_md_type_t md )
10923{
10924 const int *cur;
10925
10926 if( ssl->conf->sig_hashes == NULL )
10927 return( -1 );
10928
10929 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10930 if( *cur == (int) md )
10931 return( 0 );
10932
10933 return( -1 );
10934}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010935#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010936
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010937#if defined(MBEDTLS_X509_CRT_PARSE_C)
10938int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10939 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010940 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010941 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010942{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010943 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010944#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010945 int usage = 0;
10946#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010948 const char *ext_oid;
10949 size_t ext_len;
10950#endif
10951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010952#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10953 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010954 ((void) cert);
10955 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010956 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010957#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10960 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010961 {
10962 /* Server part of the key exchange */
10963 switch( ciphersuite->key_exchange )
10964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010965 case MBEDTLS_KEY_EXCHANGE_RSA:
10966 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010967 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010968 break;
10969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10971 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10972 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10973 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010974 break;
10975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010976 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10977 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010978 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010979 break;
10980
10981 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982 case MBEDTLS_KEY_EXCHANGE_NONE:
10983 case MBEDTLS_KEY_EXCHANGE_PSK:
10984 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10985 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010986 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010987 usage = 0;
10988 }
10989 }
10990 else
10991 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10993 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010994 }
10995
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010996 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010997 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010998 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010999 ret = -1;
11000 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011001#else
11002 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11006 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11009 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011010 }
11011 else
11012 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11014 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011015 }
11016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011018 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011019 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011020 ret = -1;
11021 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011023
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011024 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011025}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011027
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011028/*
11029 * Convert version numbers to/from wire format
11030 * and, for DTLS, to/from TLS equivalent.
11031 *
11032 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011033 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011034 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11035 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11036 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011038 unsigned char ver[2] )
11039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#if defined(MBEDTLS_SSL_PROTO_DTLS)
11041 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011042 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011043 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011044 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11045
11046 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11047 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11048 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011049 else
11050#else
11051 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011052#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011053 {
11054 ver[0] = (unsigned char) major;
11055 ver[1] = (unsigned char) minor;
11056 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011057}
11058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011059void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011060 const unsigned char ver[2] )
11061{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011062#if defined(MBEDTLS_SSL_PROTO_DTLS)
11063 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011064 {
11065 *major = 255 - ver[0] + 2;
11066 *minor = 255 - ver[1] + 1;
11067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011069 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11070 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011071 else
11072#else
11073 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011074#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011075 {
11076 *major = ver[0];
11077 *minor = ver[1];
11078 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011079}
11080
Simon Butcher99000142016-10-13 17:21:01 +010011081int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11082{
11083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11084 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11085 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11086
11087 switch( md )
11088 {
11089#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11090#if defined(MBEDTLS_MD5_C)
11091 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011092 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011093#endif
11094#if defined(MBEDTLS_SHA1_C)
11095 case MBEDTLS_SSL_HASH_SHA1:
11096 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11097 break;
11098#endif
11099#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11100#if defined(MBEDTLS_SHA512_C)
11101 case MBEDTLS_SSL_HASH_SHA384:
11102 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11103 break;
11104#endif
11105#if defined(MBEDTLS_SHA256_C)
11106 case MBEDTLS_SSL_HASH_SHA256:
11107 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11108 break;
11109#endif
11110 default:
11111 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11112 }
11113
11114 return 0;
11115#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11116 (void) ssl;
11117 (void) md;
11118
11119 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11120#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11121}
11122
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011123#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11124 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11125int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11126 unsigned char *output,
11127 unsigned char *data, size_t data_len )
11128{
11129 int ret = 0;
11130 mbedtls_md5_context mbedtls_md5;
11131 mbedtls_sha1_context mbedtls_sha1;
11132
11133 mbedtls_md5_init( &mbedtls_md5 );
11134 mbedtls_sha1_init( &mbedtls_sha1 );
11135
11136 /*
11137 * digitally-signed struct {
11138 * opaque md5_hash[16];
11139 * opaque sha_hash[20];
11140 * };
11141 *
11142 * md5_hash
11143 * MD5(ClientHello.random + ServerHello.random
11144 * + ServerParams);
11145 * sha_hash
11146 * SHA(ClientHello.random + ServerHello.random
11147 * + ServerParams);
11148 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011149 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011150 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011151 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011152 goto exit;
11153 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011154 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011155 ssl->handshake->randbytes, 64 ) ) != 0 )
11156 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011157 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011158 goto exit;
11159 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011160 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011161 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011162 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011163 goto exit;
11164 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011165 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011166 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 goto exit;
11169 }
11170
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011174 goto exit;
11175 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 ssl->handshake->randbytes, 64 ) ) != 0 )
11178 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 goto exit;
11181 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011182 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011183 data_len ) ) != 0 )
11184 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 goto exit;
11187 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 output + 16 ) ) != 0 )
11190 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011192 goto exit;
11193 }
11194
11195exit:
11196 mbedtls_md5_free( &mbedtls_md5 );
11197 mbedtls_sha1_free( &mbedtls_sha1 );
11198
11199 if( ret != 0 )
11200 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11201 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11202
11203 return( ret );
11204
11205}
11206#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11207 MBEDTLS_SSL_PROTO_TLS1_1 */
11208
11209#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11210 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011211
11212#if defined(MBEDTLS_USE_PSA_CRYPTO)
11213int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11214 unsigned char *hash, size_t *hashlen,
11215 unsigned char *data, size_t data_len,
11216 mbedtls_md_type_t md_alg )
11217{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011218 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011219 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011220 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11221
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011223
11224 if( ( status = psa_hash_setup( &hash_operation,
11225 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011226 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011227 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011228 goto exit;
11229 }
11230
Andrzej Kurek814feff2019-01-14 04:35:19 -050011231 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11232 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011233 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011234 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011235 goto exit;
11236 }
11237
Andrzej Kurek814feff2019-01-14 04:35:19 -050011238 if( ( status = psa_hash_update( &hash_operation,
11239 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011240 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011241 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011242 goto exit;
11243 }
11244
Andrzej Kurek814feff2019-01-14 04:35:19 -050011245 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11246 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011248 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011249 goto exit;
11250 }
11251
11252exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011253 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 {
11255 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11256 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011257 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011258 {
11259 case PSA_ERROR_NOT_SUPPORTED:
11260 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011261 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011262 case PSA_ERROR_BUFFER_TOO_SMALL:
11263 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11264 case PSA_ERROR_INSUFFICIENT_MEMORY:
11265 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11266 default:
11267 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11268 }
11269 }
11270 return( 0 );
11271}
11272
11273#else
11274
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011275int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011276 unsigned char *hash, size_t *hashlen,
11277 unsigned char *data, size_t data_len,
11278 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011279{
11280 int ret = 0;
11281 mbedtls_md_context_t ctx;
11282 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011283 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011284
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011285 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011286
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011287 mbedtls_md_init( &ctx );
11288
11289 /*
11290 * digitally-signed struct {
11291 * opaque client_random[32];
11292 * opaque server_random[32];
11293 * ServerDHParams params;
11294 * };
11295 */
11296 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11297 {
11298 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11299 goto exit;
11300 }
11301 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11302 {
11303 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11304 goto exit;
11305 }
11306 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11307 {
11308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11309 goto exit;
11310 }
11311 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11312 {
11313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11314 goto exit;
11315 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011316 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011317 {
11318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11319 goto exit;
11320 }
11321
11322exit:
11323 mbedtls_md_free( &ctx );
11324
11325 if( ret != 0 )
11326 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11327 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11328
11329 return( ret );
11330}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011331#endif /* MBEDTLS_USE_PSA_CRYPTO */
11332
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011333#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11334 MBEDTLS_SSL_PROTO_TLS1_2 */
11335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011336#endif /* MBEDTLS_SSL_TLS_C */