blob: 56e9c8b4465df588194793f5628943c42cc0311a [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
115#define SSL_DONT_FORCE_FLUSH 0
116#define SSL_FORCE_FLUSH 1
117
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100119
Hanno Beckera0e20d02019-05-15 14:03:01 +0100120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100121/* Top-level Connection ID API */
122
Hanno Becker8367ccc2019-05-14 11:30:10 +0100123int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
124 size_t len,
125 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100126{
127 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
129
Hanno Becker611ac772019-05-14 11:45:26 +0100130 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
131 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
132 {
133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
134 }
135
136 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100137 conf->cid_len = len;
138 return( 0 );
139}
140
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100141int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
142 int enable,
143 unsigned char const *own_cid,
144 size_t own_cid_len )
145{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100146 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
148
Hanno Beckerca092242019-04-25 16:01:49 +0100149 ssl->negotiate_cid = enable;
150 if( enable == MBEDTLS_SSL_CID_DISABLED )
151 {
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
153 return( 0 );
154 }
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100156 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100157
Hanno Beckerad4a1372019-05-03 13:06:44 +0100158 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100159 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
161 (unsigned) own_cid_len,
162 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
164 }
165
166 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100167 /* Truncation is not an issue here because
168 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
169 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171 return( 0 );
172}
173
174int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
175 int *enabled,
176 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
177 size_t *peer_cid_len )
178{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100179 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180
Hanno Becker76a79ab2019-05-03 14:38:32 +0100181 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
182 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
183 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100185 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100186
Hanno Beckerc5f24222019-05-03 12:54:52 +0100187 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
188 * were used, but client and server requested the empty CID.
189 * This is indistinguishable from not using the CID extension
190 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100191 if( ssl->transform_in->in_cid_len == 0 &&
192 ssl->transform_in->out_cid_len == 0 )
193 {
194 return( 0 );
195 }
196
Hanno Becker615ef172019-05-22 16:50:35 +0100197 if( peer_cid_len != NULL )
198 {
199 *peer_cid_len = ssl->transform_in->out_cid_len;
200 if( peer_cid != NULL )
201 {
202 memcpy( peer_cid, ssl->transform_in->out_cid,
203 ssl->transform_in->out_cid_len );
204 }
205 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100206
207 *enabled = MBEDTLS_SSL_CID_ENABLED;
208
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209 return( 0 );
210}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100211#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100212
Hanno Beckerd5847772018-08-28 10:09:23 +0100213/* Forward declarations for functions related to message buffering. */
214static void ssl_buffering_free( mbedtls_ssl_context *ssl );
215static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
216 uint8_t slot );
217static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
218static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
219static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
220static int ssl_buffer_message( mbedtls_ssl_context *ssl );
221static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100222static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100223
Hanno Beckera67dee22018-08-22 10:05:20 +0100224static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100225static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100226{
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100230 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100231
232 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
233}
234
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
236{
Hanno Becker11682cc2018-08-22 14:41:02 +0100237 size_t const bytes_written = ssl->out_left;
238 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100239
240 /* Double-check that the write-index hasn't gone
241 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100242 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100243 {
244 /* Should never happen... */
245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
246 }
247
248 return( (int) ( mtu - bytes_written ) );
249}
250
251static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
252{
253 int ret;
254 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400255 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100256
257#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
258 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
259
260 if( max_len > mfl )
261 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100262
263 /* By the standard (RFC 6066 Sect. 4), the MFL extension
264 * only limits the maximum record payload size, so in theory
265 * we would be allowed to pack multiple records of payload size
266 * MFL into a single datagram. However, this would mean that there's
267 * no way to explicitly communicate MTU restrictions to the peer.
268 *
269 * The following reduction of max_len makes sure that we never
270 * write datagrams larger than MFL + Record Expansion Overhead.
271 */
272 if( max_len <= ssl->out_left )
273 return( 0 );
274
275 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100276#endif
277
278 ret = ssl_get_remaining_space_in_datagram( ssl );
279 if( ret < 0 )
280 return( ret );
281 remaining = (size_t) ret;
282
283 ret = mbedtls_ssl_get_record_expansion( ssl );
284 if( ret < 0 )
285 return( ret );
286 expansion = (size_t) ret;
287
288 if( remaining <= expansion )
289 return( 0 );
290
291 remaining -= expansion;
292 if( remaining >= max_len )
293 remaining = max_len;
294
295 return( (int) remaining );
296}
297
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200298/*
299 * Double the retransmit timeout value, within the allowed range,
300 * returning -1 if the maximum value has already been reached.
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303{
304 uint32_t new_timeout;
305
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200306 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 return( -1 );
308
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200309 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
310 * in the following way: after the initial transmission and a first
311 * retransmission, back off to a temporary estimated MTU of 508 bytes.
312 * This value is guaranteed to be deliverable (if not guaranteed to be
313 * delivered) of any compliant IPv4 (and IPv6) network, and should work
314 * on most non-IP stacks too. */
315 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400316 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
319 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200320
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200321 new_timeout = 2 * ssl->handshake->retransmit_timeout;
322
323 /* Avoid arithmetic overflow and range overflow */
324 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200325 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200326 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200327 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200328 }
329
330 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200332 ssl->handshake->retransmit_timeout ) );
333
334 return( 0 );
335}
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200339 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341 ssl->handshake->retransmit_timeout ) );
342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200346/*
347 * Convert max_fragment_length codes to length.
348 * RFC 6066 says:
349 * enum{
350 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
351 * } MaxFragmentLength;
352 * and we add 0 -> extension unused
353 */
Angus Grattond8213d02016-05-25 20:56:48 +1000354static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200355{
Angus Grattond8213d02016-05-25 20:56:48 +1000356 switch( mfl )
357 {
358 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
359 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
360 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
361 return 512;
362 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
363 return 1024;
364 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
365 return 2048;
366 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
367 return 4096;
368 default:
369 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
370 }
371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200373
Hanno Becker52055ae2019-02-06 14:30:46 +0000374int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
375 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_ssl_session_free( dst );
378 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000381
382#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200383 if( src->peer_cert != NULL )
384 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200385 int ret;
386
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200387 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200388 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200389 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200394 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397 dst->peer_cert = NULL;
398 return( ret );
399 }
400 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000401#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000402 if( src->peer_cert_digest != NULL )
403 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000404 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000405 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000406 if( dst->peer_cert_digest == NULL )
407 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
408
409 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
410 src->peer_cert_digest_len );
411 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000412 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000413 }
414#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200417
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200418#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200419 if( src->ticket != NULL )
420 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200421 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200422 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200423 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 memcpy( dst->ticket, src->ticket, src->ticket_len );
426 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200427#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200428
429 return( 0 );
430}
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
433int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200434 const unsigned char *key_enc, const unsigned char *key_dec,
435 size_t keylen,
436 const unsigned char *iv_enc, const unsigned char *iv_dec,
437 size_t ivlen,
438 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200439 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
441int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
442int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
443int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
444int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
445#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000446
Paul Bakker5121ce52009-01-03 21:22:43 +0000447/*
448 * Key material generation
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200451static int ssl3_prf( const unsigned char *secret, size_t slen,
452 const char *label,
453 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 unsigned char *dstbuf, size_t dlen )
455{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100456 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200458 mbedtls_md5_context md5;
459 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000460 unsigned char padding[16];
461 unsigned char sha1sum[20];
462 ((void)label);
463
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200464 mbedtls_md5_init( &md5 );
465 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200466
Paul Bakker5f70b252012-09-13 14:23:06 +0000467 /*
468 * SSLv3:
469 * block =
470 * MD5( secret + SHA1( 'A' + secret + random ) ) +
471 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
472 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
473 * ...
474 */
475 for( i = 0; i < dlen / 16; i++ )
476 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200477 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100485 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100486 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000489
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100494 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100495 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100496 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000498 }
499
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100500exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200501 mbedtls_md5_free( &md5 );
502 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500504 mbedtls_platform_zeroize( padding, sizeof( padding ) );
505 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100507 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200512static int tls1_prf( const unsigned char *secret, size_t slen,
513 const char *label,
514 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000515 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516{
Paul Bakker23986e52011-04-24 08:57:21 +0000517 size_t nb, hs;
518 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200519 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300520 unsigned char *tmp;
521 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 const mbedtls_md_info_t *md_info;
524 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100525 int ret;
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Ron Eldor3b350852019-05-07 18:31:49 +0300529 tmp_len = 20 + strlen( label ) + rlen;
530 tmp = mbedtls_calloc( 1, tmp_len );
531 if( tmp == NULL )
532 {
533 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
534 goto exit;
535 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537 hs = ( slen + 1 ) / 2;
538 S1 = secret;
539 S2 = secret + slen - hs;
540
541 nb = strlen( label );
542 memcpy( tmp + 20, label, nb );
543 memcpy( tmp + 20 + nb, random, rlen );
544 nb += rlen;
545
546 /*
547 * First compute P_md5(secret,label+random)[0..dlen]
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300550 {
551 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
552 goto exit;
553 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300556 {
557 goto exit;
558 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
561 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
562 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 for( i = 0; i < dlen; i += 16 )
565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_hmac_reset ( &md_ctx );
567 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
568 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_md_hmac_reset ( &md_ctx );
571 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
572 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
575
576 for( j = 0; j < k; j++ )
577 dstbuf[i + j] = h_i[j];
578 }
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 /*
583 * XOR out with P_sha1(secret,label+random)[0..dlen]
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300586 {
587 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
588 goto exit;
589 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300592 {
593 goto exit;
594 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
597 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
598 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600 for( i = 0; i < dlen; i += 20 )
601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_hmac_reset ( &md_ctx );
603 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
604 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_md_hmac_reset ( &md_ctx );
607 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
608 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
611
612 for( j = 0; j < k; j++ )
613 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
614 }
615
Ron Eldor3b350852019-05-07 18:31:49 +0300616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618
Ron Eldor3b350852019-05-07 18:31:49 +0300619 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500620 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Ron Eldor3b350852019-05-07 18:31:49 +0300622 mbedtls_free( tmp );
623 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500628#if defined(MBEDTLS_USE_PSA_CRYPTO)
629static int tls_prf_generic( mbedtls_md_type_t md_type,
630 const unsigned char *secret, size_t slen,
631 const char *label,
632 const unsigned char *random, size_t rlen,
633 unsigned char *dstbuf, size_t dlen )
634{
635 psa_status_t status;
636 psa_algorithm_t alg;
637 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500638 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500639 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
640
Andrzej Kurek2f760752019-01-28 08:08:15 -0500641 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500642 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500643
Andrzej Kurekc929a822019-01-14 03:51:11 -0500644 if( md_type == MBEDTLS_MD_SHA384 )
645 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
646 else
647 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
648
Andrzej Kurek2f760752019-01-28 08:08:15 -0500649 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650 psa_key_policy_set_usage( &policy,
651 PSA_KEY_USAGE_DERIVE,
652 alg );
653 status = psa_set_key_policy( master_slot, &policy );
654 if( status != PSA_SUCCESS )
655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
656
657 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
658 if( status != PSA_SUCCESS )
659 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
660
661 status = psa_key_derivation( &generator,
662 master_slot, alg,
663 random, rlen,
664 (unsigned char const *) label,
665 (size_t) strlen( label ),
666 dlen );
667 if( status != PSA_SUCCESS )
668 {
669 psa_generator_abort( &generator );
670 psa_destroy_key( master_slot );
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672 }
673
674 status = psa_generator_read( &generator, dstbuf, dlen );
675 if( status != PSA_SUCCESS )
676 {
677 psa_generator_abort( &generator );
678 psa_destroy_key( master_slot );
679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
680 }
681
682 status = psa_generator_abort( &generator );
683 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500684 {
685 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500686 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500687 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500688
689 status = psa_destroy_key( master_slot );
690 if( status != PSA_SUCCESS )
691 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
692
Andrzej Kurek33171262019-01-15 03:25:18 -0500693 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694}
695
696#else /* MBEDTLS_USE_PSA_CRYPTO */
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100699 const unsigned char *secret, size_t slen,
700 const char *label,
701 const unsigned char *random, size_t rlen,
702 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703{
704 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100705 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300706 unsigned char *tmp;
707 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
709 const mbedtls_md_info_t *md_info;
710 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711 int ret;
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100719
Ron Eldor3b350852019-05-07 18:31:49 +0300720 tmp_len = md_len + strlen( label ) + rlen;
721 tmp = mbedtls_calloc( 1, tmp_len );
722 if( tmp == NULL )
723 {
724 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
725 goto exit;
726 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727
728 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729 memcpy( tmp + md_len, label, nb );
730 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731 nb += rlen;
732
733 /*
734 * Compute P_<hash>(secret, label + random)[0..dlen]
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300737 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
740 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
741 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100742
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100743 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_md_hmac_reset ( &md_ctx );
746 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
747 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_md_hmac_reset ( &md_ctx );
750 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
751 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000752
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100753 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000754
755 for( j = 0; j < k; j++ )
756 dstbuf[i + j] = h_i[j];
757 }
758
Ron Eldor3b350852019-05-07 18:31:49 +0300759exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100761
Ron Eldor3b350852019-05-07 18:31:49 +0300762 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500763 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764
Ron Eldor3b350852019-05-07 18:31:49 +0300765 mbedtls_free( tmp );
766
767 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000768}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500769#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771static int tls_prf_sha256( const unsigned char *secret, size_t slen,
772 const char *label,
773 const unsigned char *random, size_t rlen,
774 unsigned char *dstbuf, size_t dlen )
775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100777 label, random, rlen, dstbuf, dlen ) );
778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200782static int tls_prf_sha384( const unsigned char *secret, size_t slen,
783 const char *label,
784 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 unsigned char *dstbuf, size_t dlen )
786{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100788 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_SHA512_C */
791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
796 defined(MBEDTLS_SSL_PROTO_TLS1_1)
797static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200798#endif
Paul Bakker380da532012-04-18 16:10:25 +0000799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_SSL_PROTO_SSL3)
801static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
802static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803#endif
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
806static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
807static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
811#if defined(MBEDTLS_SHA256_C)
812static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
813static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
814static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SHA512_C)
818static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
819static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
820static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000823
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100824#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100825 defined(MBEDTLS_USE_PSA_CRYPTO)
826static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
827{
828 if( ssl->conf->f_psk != NULL )
829 {
830 /* If we've used a callback to select the PSK,
831 * the static configuration is irrelevant. */
832 if( ssl->handshake->psk_opaque != 0 )
833 return( 1 );
834
835 return( 0 );
836 }
837
838 if( ssl->conf->psk_opaque != 0 )
839 return( 1 );
840
841 return( 0 );
842}
843#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100844 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100845
Ron Eldorcf280092019-05-14 20:19:13 +0300846#if defined(MBEDTLS_SSL_EXPORT_KEYS)
847static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
848{
849#if defined(MBEDTLS_SSL_PROTO_SSL3)
850 if( tls_prf == ssl3_prf )
851 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300852 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300853 }
854 else
855#endif
856#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
857 if( tls_prf == tls1_prf )
858 {
859 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
860 }
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
864#if defined(MBEDTLS_SHA512_C)
865 if( tls_prf == tls_prf_sha384 )
866 {
867 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
868 }
869 else
870#endif
871#if defined(MBEDTLS_SHA256_C)
872 if( tls_prf == tls_prf_sha256 )
873 {
874 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
875 }
876 else
877#endif
878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
879 return( MBEDTLS_SSL_TLS_PRF_NONE );
880}
881#endif /* MBEDTLS_SSL_EXPORT_KEYS */
882
Ron Eldor51d3ab52019-05-12 14:54:30 +0300883int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
884 const unsigned char *secret, size_t slen,
885 const char *label,
886 const unsigned char *random, size_t rlen,
887 unsigned char *dstbuf, size_t dlen )
888{
889 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
890
891 switch( prf )
892 {
893#if defined(MBEDTLS_SSL_PROTO_SSL3)
894 case MBEDTLS_SSL_TLS_PRF_SSL3:
895 tls_prf = ssl3_prf;
896 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300897#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300898#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
899 case MBEDTLS_SSL_TLS_PRF_TLS1:
900 tls_prf = tls1_prf;
901 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300902#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
903
904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300905#if defined(MBEDTLS_SHA512_C)
906 case MBEDTLS_SSL_TLS_PRF_SHA384:
907 tls_prf = tls_prf_sha384;
908 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300909#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300910#if defined(MBEDTLS_SHA256_C)
911 case MBEDTLS_SSL_TLS_PRF_SHA256:
912 tls_prf = tls_prf_sha256;
913 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300914#endif /* MBEDTLS_SHA256_C */
915#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300916 default:
917 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
918 }
919
920 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
921}
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200925 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000926#if defined(MBEDTLS_USE_PSA_CRYPTO)
927 int psa_fallthrough;
928#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 unsigned char keyblk[256];
931 unsigned char *key1;
932 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100933 unsigned char *mac_enc;
934 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000935 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200936 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000937 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000938 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 const mbedtls_cipher_info_t *cipher_info;
940 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100941
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000942 /* cf. RFC 5246, Section 8.1:
943 * "The master secret is always exactly 48 bytes in length." */
944 size_t const master_secret_len = 48;
945
Hanno Becker35b23c72018-10-23 12:10:41 +0100946#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
947 unsigned char session_hash[48];
948#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_ssl_session *session = ssl->session_negotiate;
951 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
952 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000956#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
957 transform->encrypt_then_mac = session->encrypt_then_mac;
958#endif
959 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000960
961 ciphersuite_info = handshake->ciphersuite_info;
962 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100963 if( cipher_info == NULL )
964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000966 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100968 }
969
Hanno Beckere694c3e2017-12-27 21:34:08 +0000970 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100971 if( md_info == NULL )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000974 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100976 }
977
Hanno Beckera0e20d02019-05-15 14:03:01 +0100978#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100979 /* Copy own and peer's CID if the use of the CID
980 * extension has been negotiated. */
981 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100984
Hanno Becker05154c32019-05-03 15:23:51 +0100985 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100986 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100987 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100988 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100989
990 transform->out_cid_len = ssl->handshake->peer_cid_len;
991 memcpy( transform->out_cid, ssl->handshake->peer_cid,
992 ssl->handshake->peer_cid_len );
993 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
994 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100995 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100996#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +0100997
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000999 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SSL_PROTO_SSL3)
1002 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001003 {
Paul Bakker48916f92012-09-16 19:57:18 +00001004 handshake->tls_prf = ssl3_prf;
1005 handshake->calc_verify = ssl_calc_verify_ssl;
1006 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001007 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001008 else
1009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1011 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 {
Paul Bakker48916f92012-09-16 19:57:18 +00001013 handshake->tls_prf = tls1_prf;
1014 handshake->calc_verify = ssl_calc_verify_tls;
1015 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001017 else
1018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1020#if defined(MBEDTLS_SHA512_C)
1021 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001022 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001023 {
Paul Bakker48916f92012-09-16 19:57:18 +00001024 handshake->tls_prf = tls_prf_sha384;
1025 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1026 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001027 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001028 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SHA256_C)
1031 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 {
Paul Bakker48916f92012-09-16 19:57:18 +00001033 handshake->tls_prf = tls_prf_sha256;
1034 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1035 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001036 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001037 else
1038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001043 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001044
1045 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 * SSLv3:
1047 * master =
1048 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1049 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1050 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001051 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001052 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 * master = PRF( premaster, "master secret", randbytes )[0..47]
1054 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001055 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1058 }
1059 else
1060 {
1061 /* The label for the KDF used for key expansion.
1062 * This is either "master secret" or "extended master secret"
1063 * depending on whether the Extended Master Secret extension
1064 * is used. */
1065 char const *lbl = "master secret";
1066
1067 /* The salt for the KDF used for key expansion.
1068 * - If the Extended Master Secret extension is not used,
1069 * this is ClientHello.Random + ServerHello.Random
1070 * (see Sect. 8.1 in RFC 5246).
1071 * - If the Extended Master Secret extension is used,
1072 * this is the transcript of the handshake so far.
1073 * (see Sect. 4 in RFC 7627). */
1074 unsigned char const *salt = handshake->randbytes;
1075 size_t salt_len = 64;
1076
1077#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001081
Hanno Becker35b23c72018-10-23 12:10:41 +01001082 lbl = "extended master secret";
1083 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001084 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001089 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1090 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001091 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001092 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001093 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001094#endif /* MBEDTLS_SHA512_C */
1095 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001096 }
1097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001099 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100
Hanno Becker35b23c72018-10-23 12:10:41 +01001101 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001102 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001103#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1104
Hanno Becker7d0a5692018-10-23 15:26:22 +01001105#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1106 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1107 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1108 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1109 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001110 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001111 /* Perform PSK-to-MS expansion in a single step. */
1112 psa_status_t status;
1113 psa_algorithm_t alg;
1114 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001115 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001116
1117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1118
1119 psk = ssl->conf->psk_opaque;
1120 if( ssl->handshake->psk_opaque != 0 )
1121 psk = ssl->handshake->psk_opaque;
1122
Hanno Becker22bf1452019-04-05 11:21:08 +01001123 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1125 else
1126 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1127
1128 status = psa_key_derivation( &generator, psk, alg,
1129 salt, salt_len,
1130 (unsigned char const *) lbl,
1131 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001132 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001133 if( status != PSA_SUCCESS )
1134 {
1135 psa_generator_abort( &generator );
1136 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1137 }
1138
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001139 status = psa_generator_read( &generator, session->master,
1140 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 if( status != PSA_SUCCESS )
1142 {
1143 psa_generator_abort( &generator );
1144 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1145 }
1146
1147 status = psa_generator_abort( &generator );
1148 if( status != PSA_SUCCESS )
1149 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001150 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001151 else
1152#endif
1153 {
1154 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1155 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001156 session->master,
1157 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001158 if( ret != 0 )
1159 {
1160 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1161 return( ret );
1162 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001163
Hanno Becker7d0a5692018-10-23 15:26:22 +01001164 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1165 handshake->premaster,
1166 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001167
Hanno Becker7d0a5692018-10-23 15:26:22 +01001168 mbedtls_platform_zeroize( handshake->premaster,
1169 sizeof(handshake->premaster) );
1170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
1173 /*
1174 * Swap the client and server random values.
1175 */
Paul Bakker48916f92012-09-16 19:57:18 +00001176 memcpy( tmp, handshake->randbytes, 64 );
1177 memcpy( handshake->randbytes, tmp + 32, 32 );
1178 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001179 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
1181 /*
1182 * SSLv3:
1183 * key block =
1184 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1185 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1186 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1187 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1188 * ...
1189 *
1190 * TLSv1:
1191 * key block = PRF( master, "key expansion", randbytes )
1192 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001193 ret = handshake->tls_prf( session->master, 48, "key expansion",
1194 handshake->randbytes, 64, keyblk, 256 );
1195 if( ret != 0 )
1196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001198 return( ret );
1199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1202 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1203 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1204 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1205 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 /*
1208 * Determine the appropriate key, IV and MAC length.
1209 */
Paul Bakker68884e32013-01-07 18:20:04 +01001210
Hanno Becker88aaf652017-12-27 08:17:40 +00001211 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001212
Hanno Becker8031d062018-01-03 15:32:31 +00001213#if defined(MBEDTLS_GCM_C) || \
1214 defined(MBEDTLS_CCM_C) || \
1215 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001217 cipher_info->mode == MBEDTLS_MODE_CCM ||
1218 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001220 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001221
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001222 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001223 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001224 transform->taglen =
1225 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001226
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001227 /* All modes haves 96-bit IVs;
1228 * GCM and CCM has 4 implicit and 8 explicit bytes
1229 * ChachaPoly has all 12 bytes implicit
1230 */
Paul Bakker68884e32013-01-07 18:20:04 +01001231 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001232 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1233 transform->fixed_ivlen = 12;
1234 else
1235 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001236
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001237 /* Minimum length of encrypted record */
1238 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001239 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 }
1241 else
Hanno Becker8031d062018-01-03 15:32:31 +00001242#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1243#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1244 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1245 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001246 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001247 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1249 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001252 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001255 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001256 mac_key_len = mbedtls_md_get_size( md_info );
1257 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001260 /*
1261 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1262 * (rfc 6066 page 13 or rfc 2104 section 4),
1263 * so we only need to adjust the length here.
1264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001268
1269#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1270 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001271 * HMAC implementation which also truncates the key
1272 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001273 mac_key_len = transform->maclen;
1274#endif
1275 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001277
1278 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001279 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001281 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001283 transform->minlen = transform->maclen;
1284 else
Paul Bakker68884e32013-01-07 18:20:04 +01001285 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001286 /*
1287 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001288 * 1. if EtM is in use: one block plus MAC
1289 * otherwise: * first multiple of blocklen greater than maclen
1290 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1293 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001294 {
1295 transform->minlen = transform->maclen
1296 + cipher_info->block_size;
1297 }
1298 else
1299#endif
1300 {
1301 transform->minlen = transform->maclen
1302 + cipher_info->block_size
1303 - transform->maclen % cipher_info->block_size;
1304 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1307 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1308 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001309 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001310 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1313 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1314 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001315 {
1316 transform->minlen += transform->ivlen;
1317 }
1318 else
1319#endif
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001322 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1323 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324 }
Paul Bakker68884e32013-01-07 18:20:04 +01001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
Hanno Becker8031d062018-01-03 15:32:31 +00001327 else
1328#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1329 {
1330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Hanno Becker88aaf652017-12-27 08:17:40 +00001334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1335 (unsigned) keylen,
1336 (unsigned) transform->minlen,
1337 (unsigned) transform->ivlen,
1338 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 /*
1341 * Finally setup the cipher contexts, IVs and MAC secrets.
1342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001346 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001347 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Paul Bakker68884e32013-01-07 18:20:04 +01001349 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001350 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001352 /*
1353 * This is not used in TLS v1.1.
1354 */
Paul Bakker48916f92012-09-16 19:57:18 +00001355 iv_copy_len = ( transform->fixed_ivlen ) ?
1356 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001357 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1358 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 }
1361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_SSL_CLI_C */
1363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001366 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001367 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Hanno Becker81c7b182017-11-09 18:39:33 +00001369 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001370 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001372 /*
1373 * This is not used in TLS v1.1.
1374 */
Paul Bakker48916f92012-09-16 19:57:18 +00001375 iv_copy_len = ( transform->fixed_ivlen ) ?
1376 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001377 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1378 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001379 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001381 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001385 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1386 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Hanno Beckerd56ed242018-01-03 15:32:51 +00001389#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_SSL_PROTO_SSL3)
1391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001392 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001393 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001396 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1397 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001398 }
1399
Hanno Becker81c7b182017-11-09 18:39:33 +00001400 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1401 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001402 }
1403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1405#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1406 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1407 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001408 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001409 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1410 For AEAD-based ciphersuites, there is nothing to do here. */
1411 if( mac_key_len != 0 )
1412 {
1413 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1414 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1415 }
Paul Bakker68884e32013-01-07 18:20:04 +01001416 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001417 else
1418#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001421 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1422 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001423 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001424#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1427 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001428 {
1429 int ret = 0;
1430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001432
Hanno Becker88aaf652017-12-27 08:17:40 +00001433 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001434 transform->iv_enc, transform->iv_dec,
1435 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001436 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001437 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001440 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1441 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001442 }
1443 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001444#else
1445 ((void) mac_dec);
1446 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001448
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001449#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1450 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001451 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001452 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1453 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001454 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001455 iv_copy_len );
1456 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001457
1458 if( ssl->conf->f_export_keys_ext != NULL )
1459 {
1460 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1461 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001462 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001463 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001464 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001465 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001466 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001467 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001468#endif
1469
Hanno Beckerf704bef2018-11-16 15:21:18 +00001470#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001471
1472 /* Only use PSA-based ciphers for TLS-1.2.
1473 * That's relevant at least for TLS-1.0, where
1474 * we assume that mbedtls_cipher_crypt() updates
1475 * the structure field for the IV, which the PSA-based
1476 * implementation currently doesn't. */
1477#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1478 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001479 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001481 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001482 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1483 {
1484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001486 }
1487
1488 if( ret == 0 )
1489 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001491 psa_fallthrough = 0;
1492 }
1493 else
1494 {
1495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1496 psa_fallthrough = 1;
1497 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 psa_fallthrough = 1;
1501#else
1502 psa_fallthrough = 1;
1503#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001504
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001506#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001507 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001508 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001511 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001512 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001513
Hanno Beckerf704bef2018-11-16 15:21:18 +00001514#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001515 /* Only use PSA-based ciphers for TLS-1.2.
1516 * That's relevant at least for TLS-1.0, where
1517 * we assume that mbedtls_cipher_crypt() updates
1518 * the structure field for the IV, which the PSA-based
1519 * implementation currently doesn't. */
1520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1521 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001522 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001523 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001524 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001525 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1526 {
1527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001528 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001529 }
1530
1531 if( ret == 0 )
1532 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001534 psa_fallthrough = 0;
1535 }
1536 else
1537 {
1538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1539 psa_fallthrough = 1;
1540 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001542 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001543 psa_fallthrough = 1;
1544#else
1545 psa_fallthrough = 1;
1546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001547
Hanno Beckercb1cc802018-11-17 22:27:38 +00001548 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001549#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001550 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001551 cipher_info ) ) != 0 )
1552 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001554 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001555 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001558 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001562 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001566 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001570 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_CIPHER_MODE_CBC)
1574 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1577 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001580 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001581 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1584 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001587 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594 // Initialize compression
1595 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597 {
Paul Bakker16770332013-10-11 09:59:44 +02001598 if( ssl->compress_buf == NULL )
1599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001601 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001602 if( ssl->compress_buf == NULL )
1603 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001605 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001606 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1607 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001608 }
1609 }
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612
Paul Bakker48916f92012-09-16 19:57:18 +00001613 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1614 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001616 if( deflateInit( &transform->ctx_deflate,
1617 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001618 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001621 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1622 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001623 }
1624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001628end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001629 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1630 mbedtls_platform_zeroize( handshake->randbytes,
1631 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001632 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001633}
1634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_PROTO_SSL3)
1636void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001637{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001638 mbedtls_md5_context md5;
1639 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 unsigned char pad_1[48];
1641 unsigned char pad_2[48];
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001645 mbedtls_md5_init( &md5 );
1646 mbedtls_sha1_init( &sha1 );
1647
1648 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1649 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Paul Bakker380da532012-04-18 16:10:25 +00001651 memset( pad_1, 0x36, 48 );
1652 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001654 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1655 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1656 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001658 mbedtls_md5_starts_ret( &md5 );
1659 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1660 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1661 mbedtls_md5_update_ret( &md5, hash, 16 );
1662 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001664 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1665 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1666 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001667
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001668 mbedtls_sha1_starts_ret( &sha1 );
1669 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1670 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1671 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1672 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001676
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001677 mbedtls_md5_free( &md5 );
1678 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001679
Paul Bakker380da532012-04-18 16:10:25 +00001680 return;
1681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1685void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001686{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001687 mbedtls_md5_context md5;
1688 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001691
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001692 mbedtls_md5_init( &md5 );
1693 mbedtls_sha1_init( &sha1 );
1694
1695 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1696 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001697
Andrzej Kurekeb342242019-01-29 09:14:33 -05001698 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001699 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001703
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001704 mbedtls_md5_free( &md5 );
1705 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001706
Paul Bakker380da532012-04-18 16:10:25 +00001707 return;
1708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1712#if defined(MBEDTLS_SHA256_C)
1713void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001714{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001715#if defined(MBEDTLS_USE_PSA_CRYPTO)
1716 size_t hash_size;
1717 psa_status_t status;
1718 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1719
1720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1721 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1722 if( status != PSA_SUCCESS )
1723 {
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1725 return;
1726 }
1727
1728 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1729 if( status != PSA_SUCCESS )
1730 {
1731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1732 return;
1733 }
1734 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1736#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001737 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001738
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001739 mbedtls_sha256_init( &sha256 );
1740
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001742
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001743 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001744 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001748
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001749 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001750#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001751 return;
1752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_SHA512_C)
1756void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001757{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001758#if defined(MBEDTLS_USE_PSA_CRYPTO)
1759 size_t hash_size;
1760 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001761 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001762
1763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001764 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001765 if( status != PSA_SUCCESS )
1766 {
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1768 return;
1769 }
1770
Andrzej Kurek972fba52019-01-30 03:29:12 -05001771 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001772 if( status != PSA_SUCCESS )
1773 {
1774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1775 return;
1776 }
1777 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1779#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001780 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001781
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001782 mbedtls_sha512_init( &sha512 );
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001785
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001786 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001787 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001792 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001793#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 return;
1795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#endif /* MBEDTLS_SHA512_C */
1797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1800int 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 +02001801{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001802 unsigned char *p = ssl->handshake->premaster;
1803 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001804 const unsigned char *psk = ssl->conf->psk;
1805 size_t psk_len = ssl->conf->psk_len;
1806
1807 /* If the psk callback was called, use its result */
1808 if( ssl->handshake->psk != NULL )
1809 {
1810 psk = ssl->handshake->psk;
1811 psk_len = ssl->handshake->psk_len;
1812 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001813
1814 /*
1815 * PMS = struct {
1816 * opaque other_secret<0..2^16-1>;
1817 * opaque psk<0..2^16-1>;
1818 * };
1819 * with "other_secret" depending on the particular key exchange
1820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1822 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001823 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001824 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001826
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001827 *(p++) = (unsigned char)( psk_len >> 8 );
1828 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001829
1830 if( end < p || (size_t)( end - p ) < psk_len )
1831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1832
1833 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001834 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001835 }
1836 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1838#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1839 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001840 {
1841 /*
1842 * other_secret already set by the ClientKeyExchange message,
1843 * and is 48 bytes long
1844 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001845 if( end - p < 2 )
1846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1847
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001848 *p++ = 0;
1849 *p++ = 48;
1850 p += 48;
1851 }
1852 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1854#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1855 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001856 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001857 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001858 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001860 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001862 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001863 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001866 return( ret );
1867 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001868 *(p++) = (unsigned char)( len >> 8 );
1869 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 p += len;
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 }
1874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1876#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1877 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001878 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001879 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001880 size_t zlen;
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001883 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001884 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001887 return( ret );
1888 }
1889
1890 *(p++) = (unsigned char)( zlen >> 8 );
1891 *(p++) = (unsigned char)( zlen );
1892 p += zlen;
1893
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001894 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1895 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896 }
1897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001902 }
1903
1904 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001905 if( end - p < 2 )
1906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001907
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001908 *(p++) = (unsigned char)( psk_len >> 8 );
1909 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001910
1911 if( end < p || (size_t)( end - p ) < psk_len )
1912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1913
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001914 memcpy( p, psk, psk_len );
1915 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001916
1917 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1918
1919 return( 0 );
1920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001924/*
1925 * SSLv3.0 MAC functions
1926 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001927#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001928static void ssl_mac( mbedtls_md_context_t *md_ctx,
1929 const unsigned char *secret,
1930 const unsigned char *buf, size_t len,
1931 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001932 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001933{
1934 unsigned char header[11];
1935 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001936 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1938 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001939
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001940 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001942 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001943 else
Paul Bakker68884e32013-01-07 18:20:04 +01001944 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946 memcpy( header, ctr, 8 );
1947 header[ 8] = (unsigned char) type;
1948 header[ 9] = (unsigned char)( len >> 8 );
1949 header[10] = (unsigned char)( len );
1950
Paul Bakker68884e32013-01-07 18:20:04 +01001951 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mbedtls_md_starts( md_ctx );
1953 mbedtls_md_update( md_ctx, secret, md_size );
1954 mbedtls_md_update( md_ctx, padding, padlen );
1955 mbedtls_md_update( md_ctx, header, 11 );
1956 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001957 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Paul Bakker68884e32013-01-07 18:20:04 +01001959 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 mbedtls_md_starts( md_ctx );
1961 mbedtls_md_update( md_ctx, secret, md_size );
1962 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001963 mbedtls_md_update( md_ctx, out, md_size );
1964 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001967
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001968/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001969 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001970#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001971 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1972 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1973 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1974/* This function makes sure every byte in the memory region is accessed
1975 * (in ascending addresses order) */
1976static void ssl_read_memory( unsigned char *p, size_t len )
1977{
1978 unsigned char acc = 0;
1979 volatile unsigned char force;
1980
1981 for( ; len != 0; p++, len-- )
1982 acc ^= *p;
1983
1984 force = acc;
1985 (void) force;
1986}
1987#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1988
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001989/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001991 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001992
Hanno Beckera0e20d02019-05-15 14:03:01 +01001993#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001994/* This functions transforms a DTLS plaintext fragment and a record content
1995 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001996 *
1997 * struct {
1998 * opaque content[DTLSPlaintext.length];
1999 * ContentType real_type;
2000 * uint8 zeros[length_of_padding];
2001 * } DTLSInnerPlaintext;
2002 *
2003 * Input:
2004 * - `content`: The beginning of the buffer holding the
2005 * plaintext to be wrapped.
2006 * - `*content_size`: The length of the plaintext in Bytes.
2007 * - `max_len`: The number of Bytes available starting from
2008 * `content`. This must be `>= *content_size`.
2009 * - `rec_type`: The desired record content type.
2010 *
2011 * Output:
2012 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2013 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2014 *
2015 * Returns:
2016 * - `0` on success.
2017 * - A negative error code if `max_len` didn't offer enough space
2018 * for the expansion.
2019 */
2020static int ssl_cid_build_inner_plaintext( unsigned char *content,
2021 size_t *content_size,
2022 size_t remaining,
2023 uint8_t rec_type )
2024{
2025 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002026 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2027 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2028 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002029
2030 /* Write real content type */
2031 if( remaining == 0 )
2032 return( -1 );
2033 content[ len ] = rec_type;
2034 len++;
2035 remaining--;
2036
2037 if( remaining < pad )
2038 return( -1 );
2039 memset( content + len, 0, pad );
2040 len += pad;
2041 remaining -= pad;
2042
2043 *content_size = len;
2044 return( 0 );
2045}
2046
Hanno Becker07dc97d2019-05-20 15:08:01 +01002047/* This function parses a DTLSInnerPlaintext structure.
2048 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002049static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2050 size_t *content_size,
2051 uint8_t *rec_type )
2052{
2053 size_t remaining = *content_size;
2054
2055 /* Determine length of padding by skipping zeroes from the back. */
2056 do
2057 {
2058 if( remaining == 0 )
2059 return( -1 );
2060 remaining--;
2061 } while( content[ remaining ] == 0 );
2062
2063 *content_size = remaining;
2064 *rec_type = content[ remaining ];
2065
2066 return( 0 );
2067}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002068#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002069
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002070/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002071 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002073 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002074 mbedtls_record *rec )
2075{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002076 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002077 *
2078 * additional_data = seq_num + TLSCompressed.type +
2079 * TLSCompressed.version + TLSCompressed.length;
2080 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002081 * For the CID extension, this is extended as follows
2082 * (quoting draft-ietf-tls-dtls-connection-id-05,
2083 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002084 *
2085 * additional_data = seq_num + DTLSPlaintext.type +
2086 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002087 * cid +
2088 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002089 * length_of_DTLSInnerPlaintext;
2090 */
2091
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2093 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002094 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002095
Hanno Beckera0e20d02019-05-15 14:03:01 +01002096#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002097 if( rec->cid_len != 0 )
2098 {
2099 memcpy( add_data + 11, rec->cid, rec->cid_len );
2100 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2101 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2102 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2103 *add_data_len = 13 + 1 + rec->cid_len;
2104 }
2105 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002106#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002107 {
2108 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2109 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2110 *add_data_len = 13;
2111 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002112}
2113
Hanno Beckera18d1322018-01-03 14:27:32 +00002114int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2115 mbedtls_ssl_transform *transform,
2116 mbedtls_record *rec,
2117 int (*f_rng)(void *, unsigned char *, size_t),
2118 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002123 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002124 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125 size_t post_avail;
2126
2127 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002128#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002129 ((void) ssl);
2130#endif
2131
2132 /* The PRNG is used for dynamic IV generation that's used
2133 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2134#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2135 ( defined(MBEDTLS_AES_C) || \
2136 defined(MBEDTLS_ARIA_C) || \
2137 defined(MBEDTLS_CAMELLIA_C) ) && \
2138 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2139 ((void) f_rng);
2140 ((void) p_rng);
2141#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002145 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002146 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2149 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002150 if( rec == NULL
2151 || rec->buf == NULL
2152 || rec->buf_len < rec->data_offset
2153 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002154#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002155 || rec->cid_len != 0
2156#endif
2157 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 {
2159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002161 }
2162
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002164 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002166 data, rec->data_len );
2167
2168 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2169
2170 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2173 (unsigned) rec->data_len,
2174 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2176 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002177
Hanno Beckera0e20d02019-05-15 14:03:01 +01002178#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002179 /*
2180 * Add CID information
2181 */
2182 rec->cid_len = transform->out_cid_len;
2183 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2184 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002185
2186 if( rec->cid_len != 0 )
2187 {
2188 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002189 * Wrap plaintext into DTLSInnerPlaintext structure.
2190 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002191 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002192 * Note that this changes `rec->data_len`, and hence
2193 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002194 */
2195 if( ssl_cid_build_inner_plaintext( data,
2196 &rec->data_len,
2197 post_avail,
2198 rec->type ) != 0 )
2199 {
2200 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2201 }
2202
2203 rec->type = MBEDTLS_SSL_MSG_CID;
2204 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002206
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002207 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2208
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002210 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 */
Hanno Becker52344c22018-01-03 15:24:20 +00002212#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( mode == MBEDTLS_MODE_STREAM ||
2214 ( mode == MBEDTLS_MODE_CBC
2215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002217#endif
2218 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002220 if( post_avail < transform->maclen )
2221 {
2222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2223 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2224 }
2225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002229 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2231 data, rec->data_len, rec->ctr, rec->type, mac );
2232 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 }
2234 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2237 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002238 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 {
Hanno Becker992b6872017-11-09 18:57:39 +00002240 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2241
Hanno Beckercab87e62019-04-29 13:52:53 +01002242 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002243
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002245 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2247 data, rec->data_len );
2248 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2249 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2250
2251 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 }
2253 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002254#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002258 }
2259
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2261 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002262
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002263 rec->data_len += transform->maclen;
2264 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002265 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002266 }
Hanno Becker52344c22018-01-03 15:24:20 +00002267#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002269 /*
2270 * Encrypt
2271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2273 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002275 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002278 "including %d bytes of padding",
2279 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002281 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2282 transform->iv_enc, transform->ivlen,
2283 data, rec->data_len,
2284 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002287 return( ret );
2288 }
2289
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002290 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
Paul Bakker68884e32013-01-07 18:20:04 +01002296 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002298
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002299#if defined(MBEDTLS_GCM_C) || \
2300 defined(MBEDTLS_CCM_C) || \
2301 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002303 mode == MBEDTLS_MODE_CCM ||
2304 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002305 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002306 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002307 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002308 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002309
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002310 /* Check that there's space for both the authentication tag
2311 * and the explicit IV before and after the record content. */
2312 if( post_avail < transform->taglen ||
2313 rec->data_offset < explicit_iv_len )
2314 {
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2316 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2317 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002318
Paul Bakker68884e32013-01-07 18:20:04 +01002319 /*
2320 * Generate IV
2321 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2323 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002324 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002326 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2327 explicit_iv_len );
2328 /* Prefix record content with explicit IV. */
2329 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 }
2331 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2332 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002333 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 unsigned char i;
2335
2336 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2337
2338 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 }
2341 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002342 {
2343 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002346 }
2347
Hanno Beckercab87e62019-04-29 13:52:53 +01002348 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002349
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2351 iv, transform->ivlen );
2352 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353 data - explicit_iv_len, explicit_iv_len );
2354 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002355 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002358 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002359
Paul Bakker68884e32013-01-07 18:20:04 +01002360 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002361 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002362 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002364 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002366 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002367 data, rec->data_len, /* source */
2368 data, &rec->data_len, /* destination */
2369 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002372 return( ret );
2373 }
2374
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2376 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002377
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 rec->data_len += transform->taglen + explicit_iv_len;
2379 rec->data_offset -= explicit_iv_len;
2380 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002381 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2385#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002386 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002389 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 size_t padlen, i;
2391 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002392
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002393 /* Currently we're always using minimal padding
2394 * (up to 255 bytes would be allowed). */
2395 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2396 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 padlen = 0;
2398
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 /* Check there's enough space in the buffer for the padding. */
2400 if( post_avail < padlen + 1 )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2403 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2404 }
2405
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002407 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 rec->data_len += padlen + 1;
2410 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002414 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2415 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002417 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 if( f_rng == NULL )
2420 {
2421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2423 }
2424
2425 if( rec->data_offset < transform->ivlen )
2426 {
2427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2428 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2429 }
2430
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 /*
2432 * Generate IV
2433 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002435 if( ret != 0 )
2436 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002437
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 memcpy( data - transform->ivlen, transform->iv_enc,
2439 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002440
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002445 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002447 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2450 transform->iv_enc,
2451 transform->ivlen,
2452 data, rec->data_len,
2453 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002456 return( ret );
2457 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002458
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002463 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002466 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002467 {
2468 /*
2469 * Save IV in SSL3 and TLS1
2470 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002471 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2472 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002475#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476 {
2477 data -= transform->ivlen;
2478 rec->data_offset -= transform->ivlen;
2479 rec->data_len += transform->ivlen;
2480 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002483 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002484 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002485 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2486
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487 /*
2488 * MAC(MAC_write_key, seq_num +
2489 * TLSCipherText.type +
2490 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002491 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 * IV + // except for TLS 1.0
2493 * ENC(content + padding + padding_length));
2494 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002495
2496 if( post_avail < transform->maclen)
2497 {
2498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2499 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2500 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501
Hanno Beckercab87e62019-04-29 13:52:53 +01002502 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002506 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002507
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002509 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2511 data, rec->data_len );
2512 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2513 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002515 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002516
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 rec->data_len += transform->maclen;
2518 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002519 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002523 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002525 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002531 /* Make extra sure authentication was performed, exactly once */
2532 if( auth_done != 1 )
2533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002536 }
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 return( 0 );
2541}
2542
Hanno Beckera18d1322018-01-03 14:27:32 +00002543int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2544 mbedtls_ssl_transform *transform,
2545 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002546{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002547 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002549 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002550#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002551 size_t padlen = 0, correct = 1;
2552#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002554 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002555 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002556
Hanno Beckera18d1322018-01-03 14:27:32 +00002557#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002558 ((void) ssl);
2559#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002563 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2566 }
2567 if( rec == NULL ||
2568 rec->buf == NULL ||
2569 rec->buf_len < rec->data_offset ||
2570 rec->buf_len - rec->data_offset < rec->data_len )
2571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002574 }
2575
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002576 data = rec->buf + rec->data_offset;
2577 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Beckera0e20d02019-05-15 14:03:01 +01002579#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002580 /*
2581 * Match record's CID with incoming CID.
2582 */
Hanno Becker938489a2019-05-08 13:02:22 +01002583 if( rec->cid_len != transform->in_cid_len ||
2584 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2585 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002586 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002587 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2591 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002592 {
2593 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002594 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2595 transform->iv_dec,
2596 transform->ivlen,
2597 data, rec->data_len,
2598 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002601 return( ret );
2602 }
2603
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002604 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
Paul Bakker68884e32013-01-07 18:20:04 +01002610 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612#if defined(MBEDTLS_GCM_C) || \
2613 defined(MBEDTLS_CCM_C) || \
2614 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002616 mode == MBEDTLS_MODE_CCM ||
2617 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002618 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002621
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 /*
2623 * Compute and update sizes
2624 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002628 "+ taglen (%d)", rec->data_len,
2629 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002631 }
Paul Bakker68884e32013-01-07 18:20:04 +01002632
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 /*
2634 * Prepare IV
2635 */
2636 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2637 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002638 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002640 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002641
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 }
2643 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2644 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002645 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002646 unsigned char i;
2647
2648 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2649
2650 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002651 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 }
2653 else
2654 {
2655 /* Reminder if we ever add an AEAD mode with a different size */
2656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2658 }
2659
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660 data += explicit_iv_len;
2661 rec->data_offset += explicit_iv_len;
2662 rec->data_len -= explicit_iv_len + transform->taglen;
2663
Hanno Beckercab87e62019-04-29 13:52:53 +01002664 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002666 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667
2668 memcpy( transform->iv_dec + transform->fixed_ivlen,
2669 data - explicit_iv_len, explicit_iv_len );
2670
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002673 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002674
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002675
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002676 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002677 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002678 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002679 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2680 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002681 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 data, rec->data_len,
2683 data, &olen,
2684 data + rec->data_len,
2685 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2690 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002691
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002692 return( ret );
2693 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002694 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002695
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002696 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002700 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2704#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002705 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002708 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002709
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 /*
Paul Bakker45829992013-01-03 14:52:21 +01002711 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2715 {
2716 /* The ciphertext is prefixed with the CBC IV. */
2717 minlen += transform->ivlen;
2718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif
Paul Bakker45829992013-01-03 14:52:21 +01002720
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002721 /* Size considerations:
2722 *
2723 * - The CBC cipher text must not be empty and hence
2724 * at least of size transform->ivlen.
2725 *
2726 * Together with the potential IV-prefix, this explains
2727 * the first of the two checks below.
2728 *
2729 * - The record must contain a MAC, either in plain or
2730 * encrypted, depending on whether Encrypt-then-MAC
2731 * is used or not.
2732 * - If it is, the message contains the IV-prefix,
2733 * the CBC ciphertext, and the MAC.
2734 * - If it is not, the padded plaintext, and hence
2735 * the CBC ciphertext, has at least length maclen + 1
2736 * because there is at least the padding length byte.
2737 *
2738 * As the CBC ciphertext is not empty, both cases give the
2739 * lower bound minlen + maclen + 1 on the record size, which
2740 * we test for in the second check below.
2741 */
2742 if( rec->data_len < minlen + transform->ivlen ||
2743 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002746 "+ 1 ) ( + expl IV )", rec->data_len,
2747 transform->ivlen,
2748 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002750 }
2751
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002752 /*
2753 * Authenticate before decrypt if enabled
2754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002756 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002757 {
Hanno Becker992b6872017-11-09 18:57:39 +00002758 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002761
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002762 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2763 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002764
Hanno Beckercab87e62019-04-29 13:52:53 +01002765 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002766
Hanno Beckercab87e62019-04-29 13:52:53 +01002767 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2768 add_data_len );
2769 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2770 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2772 data, rec->data_len );
2773 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2774 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002775
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2777 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002778 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002779 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002781 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2782 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002787 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002790
2791 /*
2792 * Check length sanity
2793 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002797 rec->data_len, transform->ivlen ) );
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 }
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002802 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002803 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002806 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 /* This is safe because data_len >= minlen + maclen + 1 initially,
2808 * and at this point we have at most subtracted maclen (note that
2809 * minlen == transform->ivlen here). */
2810 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002812 data += transform->ivlen;
2813 rec->data_offset += transform->ivlen;
2814 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2819 transform->iv_dec, transform->ivlen,
2820 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002823 return( ret );
2824 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002825
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002830 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002833 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002834 {
2835 /*
2836 * Save IV in SSL3 and TLS1
2837 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2839 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 /* Safe since data_len >= minlen + maclen + 1, so after having
2844 * subtracted at most minlen and maclen up to this point,
2845 * data_len > 0. */
2846 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002847
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 if( auth_done == 1 )
2849 {
2850 correct *= ( rec->data_len >= padlen + 1 );
2851 padlen *= ( rec->data_len >= padlen + 1 );
2852 }
2853 else
Paul Bakker45829992013-01-03 14:52:21 +01002854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 if( rec->data_len < transform->maclen + padlen + 1 )
2857 {
2858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2859 rec->data_len,
2860 transform->maclen,
2861 padlen + 1 ) );
2862 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002863#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864
2865 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2866 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 padlen++;
2870
2871 /* Regardless of the validity of the padding,
2872 * we have data_len >= padlen here. */
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002877 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_SSL_DEBUG_ALL)
2880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 "should be no more than %d",
2882 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002883#endif
Paul Bakker45829992013-01-03 14:52:21 +01002884 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 }
2886 }
2887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2889#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2890 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002893 /* The padding check involves a series of up to 256
2894 * consecutive memory reads at the end of the record
2895 * plaintext buffer. In order to hide the length and
2896 * validity of the padding, always perform exactly
2897 * `min(256,plaintext_len)` reads (but take into account
2898 * only the last `padlen` bytes for the padding check). */
2899 size_t pad_count = 0;
2900 size_t real_count = 0;
2901 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002902
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 /* Index of first padding byte; it has been ensured above
2904 * that the subtraction is safe. */
2905 size_t const padding_idx = rec->data_len - padlen;
2906 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2907 size_t const start_idx = rec->data_len - num_checks;
2908 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002909
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002910 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002911 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002912 real_count |= ( idx >= padding_idx );
2913 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002914 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002918 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002920#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002921 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2925 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002929 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002930
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002931 /* If the padding was found to be invalid, padlen == 0
2932 * and the subtraction is safe. If the padding was found valid,
2933 * padlen hasn't been changed and the previous assertion
2934 * data_len >= padlen still holds. */
2935 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002937 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002939 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2942 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002945#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002947 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002948#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
2950 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002951 * Authenticate if not done yet.
2952 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 */
Hanno Becker52344c22018-01-03 15:24:20 +00002954#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002955 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002956 {
Hanno Becker992b6872017-11-09 18:57:39 +00002957 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002958
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002959 /* If the initial value of padlen was such that
2960 * data_len < maclen + padlen + 1, then padlen
2961 * got reset to 1, and the initial check
2962 * data_len >= minlen + maclen + 1
2963 * guarantees that at this point we still
2964 * have at least data_len >= maclen.
2965 *
2966 * If the initial value of padlen was such that
2967 * data_len >= maclen + padlen + 1, then we have
2968 * subtracted either padlen + 1 (if the padding was correct)
2969 * or 0 (if the padding was incorrect) since then,
2970 * hence data_len >= maclen in any case.
2971 */
2972 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Hanno Beckercab87e62019-04-29 13:52:53 +01002974 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002978 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 ssl_mac( &transform->md_ctx_dec,
2980 transform->mac_dec,
2981 data, rec->data_len,
2982 rec->ctr, rec->type,
2983 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002984 }
2985 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2987#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2988 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002989 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002990 {
2991 /*
2992 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002993 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002994 *
2995 * Known timing attacks:
2996 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2997 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002998 * To compensate for different timings for the MAC calculation
2999 * depending on how much padding was removed (which is determined
3000 * by padlen), process extra_run more blocks through the hash
3001 * function.
3002 *
3003 * The formula in the paper is
3004 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3005 * where L1 is the size of the header plus the decrypted message
3006 * plus CBC padding and L2 is the size of the header plus the
3007 * decrypted message. This is for an underlying hash function
3008 * with 64-byte blocks.
3009 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3010 * correctly. We round down instead of up, so -56 is the correct
3011 * value for our calculations instead of -55.
3012 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003013 * Repeat the formula rather than defining a block_size variable.
3014 * This avoids requiring division by a variable at runtime
3015 * (which would be marginally less efficient and would require
3016 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003017 */
3018 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003019 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003020
3021 /*
3022 * The next two sizes are the minimum and maximum values of
3023 * in_msglen over all padlen values.
3024 *
3025 * They're independent of padlen, since we previously did
3026 * in_msglen -= padlen.
3027 *
3028 * Note that max_len + maclen is never more than the buffer
3029 * length, as we previously did in_msglen -= maclen too.
3030 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003031 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003032 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3033
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003034 memset( tmp, 0, sizeof( tmp ) );
3035
3036 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003038#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3039 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 case MBEDTLS_MD_MD5:
3041 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003044 extra_run =
3045 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3046 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 break;
3048#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003049#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003050 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003051 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003052 extra_run =
3053 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3054 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003055 break;
3056#endif
3057 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3060 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003061
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003062 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003063
Hanno Beckercab87e62019-04-29 13:52:53 +01003064 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3065 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003066 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3067 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003068 /* Make sure we access everything even when padlen > 0. This
3069 * makes the synchronisation requirements for just-in-time
3070 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 ssl_read_memory( data + rec->data_len, padlen );
3072 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003073
3074 /* Call mbedtls_md_process at least once due to cache attacks
3075 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003076 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003078
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003080
3081 /* Make sure we access all the memory that could contain the MAC,
3082 * before we check it in the next code block. This makes the
3083 * synchronisation requirements for just-in-time Prime+Probe
3084 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003085 ssl_read_memory( data + min_len,
3086 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 }
3088 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3090 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003096#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003097 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3098 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003099#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003101 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3102 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#if defined(MBEDTLS_SSL_DEBUG_ALL)
3105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003106#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003107 correct = 0;
3108 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003109 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003110 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003111
3112 /*
3113 * Finally check the correct flag
3114 */
3115 if( correct == 0 )
3116 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003117#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003118
3119 /* Make extra sure authentication was performed, exactly once */
3120 if( auth_done != 1 )
3121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003124 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
Hanno Beckera0e20d02019-05-15 14:03:01 +01003126#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003127 if( rec->cid_len != 0 )
3128 {
3129 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3130 &rec->type );
3131 if( ret != 0 )
3132 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3133 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003134#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
3138 return( 0 );
3139}
3140
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003141#undef MAC_NONE
3142#undef MAC_PLAINTEXT
3143#undef MAC_CIPHERTEXT
3144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146/*
3147 * Compression/decompression functions
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150{
3151 int ret;
3152 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003153 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003154 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003155 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003159 if( len_pre == 0 )
3160 return( 0 );
3161
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162 memcpy( msg_pre, ssl->out_msg, len_pre );
3163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003165 ssl->out_msglen ) );
3166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 ssl->out_msg, ssl->out_msglen );
3169
Paul Bakker48916f92012-09-16 19:57:18 +00003170 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3171 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3172 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003173 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174
Paul Bakker48916f92012-09-16 19:57:18 +00003175 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003176 if( ret != Z_OK )
3177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3179 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003180 }
3181
Angus Grattond8213d02016-05-25 20:56:48 +10003182 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003183 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186 ssl->out_msglen ) );
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189 ssl->out_msg, ssl->out_msglen );
3190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003192
3193 return( 0 );
3194}
3195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197{
3198 int ret;
3199 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003200 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003202 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003206 if( len_pre == 0 )
3207 return( 0 );
3208
Paul Bakker2770fbd2012-07-03 13:30:23 +00003209 memcpy( msg_pre, ssl->in_msg, len_pre );
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212 ssl->in_msglen ) );
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215 ssl->in_msg, ssl->in_msglen );
3216
Paul Bakker48916f92012-09-16 19:57:18 +00003217 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3218 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3219 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003220 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003221 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224 if( ret != Z_OK )
3225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3227 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228 }
3229
Angus Grattond8213d02016-05-25 20:56:48 +10003230 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003231 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003234 ssl->in_msglen ) );
3235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237 ssl->in_msg, ssl->in_msglen );
3238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003240
3241 return( 0 );
3242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3246static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#if defined(MBEDTLS_SSL_PROTO_DTLS)
3249static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003250{
3251 /* If renegotiation is not enforced, retransmit until we would reach max
3252 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003253 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003254 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003255 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003256 unsigned char doublings = 1;
3257
3258 while( ratio != 0 )
3259 {
3260 ++doublings;
3261 ratio >>= 1;
3262 }
3263
3264 if( ++ssl->renego_records_seen > doublings )
3265 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003267 return( 0 );
3268 }
3269 }
3270
3271 return( ssl_write_hello_request( ssl ) );
3272}
3273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003275
Paul Bakker5121ce52009-01-03 21:22:43 +00003276/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003277 * Fill the input message buffer by appending data to it.
3278 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003279 *
3280 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3281 * available (from this read and/or a previous one). Otherwise, an error code
3282 * is returned (possibly EOF or WANT_READ).
3283 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003284 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3285 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3286 * since we always read a whole datagram at once.
3287 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003288 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003289 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003292{
Paul Bakker23986e52011-04-24 08:57:21 +00003293 int ret;
3294 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003298 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003301 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003303 }
3304
Angus Grattond8213d02016-05-25 20:56:48 +10003305 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003309 }
3310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003314 uint32_t timeout;
3315
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003316 /* Just to be sure */
3317 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3318 {
3319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3320 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3322 }
3323
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003324 /*
3325 * The point is, we need to always read a full datagram at once, so we
3326 * sometimes read more then requested, and handle the additional data.
3327 * It could be the rest of the current record (while fetching the
3328 * header) and/or some other records in the same datagram.
3329 */
3330
3331 /*
3332 * Move to the next record in the already read datagram if applicable
3333 */
3334 if( ssl->next_record_offset != 0 )
3335 {
3336 if( ssl->in_left < ssl->next_record_offset )
3337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003340 }
3341
3342 ssl->in_left -= ssl->next_record_offset;
3343
3344 if( ssl->in_left != 0 )
3345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003347 ssl->next_record_offset ) );
3348 memmove( ssl->in_hdr,
3349 ssl->in_hdr + ssl->next_record_offset,
3350 ssl->in_left );
3351 }
3352
3353 ssl->next_record_offset = 0;
3354 }
3355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358
3359 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003360 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003361 */
3362 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003365 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003366 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003367
3368 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003369 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003370 * are not at the beginning of a new record, the caller did something
3371 * wrong.
3372 */
3373 if( ssl->in_left != 0 )
3374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003377 }
3378
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003379 /*
3380 * Don't even try to read if time's out already.
3381 * This avoids by-passing the timer when repeatedly receiving messages
3382 * that will end up being dropped.
3383 */
3384 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003385 {
3386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003387 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003388 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003389 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003390 {
Angus Grattond8213d02016-05-25 20:56:48 +10003391 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003394 timeout = ssl->handshake->retransmit_timeout;
3395 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003396 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003400 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003401 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3402 timeout );
3403 else
3404 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407
3408 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003410 }
3411
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003412 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003415 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003418 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003419 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003422 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003423 }
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003428 return( ret );
3429 }
3430
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003431 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003434 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003436 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003437 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003440 return( ret );
3441 }
3442
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003443 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003446 }
3447
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 if( ret < 0 )
3449 return( ret );
3450
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003451 ssl->in_left = ret;
3452 }
3453 else
3454#endif
3455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003457 ssl->in_left, nb_want ) );
3458
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003459 while( ssl->in_left < nb_want )
3460 {
3461 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003462
3463 if( ssl_check_timer( ssl ) != 0 )
3464 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3465 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003466 {
3467 if( ssl->f_recv_timeout != NULL )
3468 {
3469 ret = ssl->f_recv_timeout( ssl->p_bio,
3470 ssl->in_hdr + ssl->in_left, len,
3471 ssl->conf->read_timeout );
3472 }
3473 else
3474 {
3475 ret = ssl->f_recv( ssl->p_bio,
3476 ssl->in_hdr + ssl->in_left, len );
3477 }
3478 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003481 ssl->in_left, nb_want ) );
3482 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003483
3484 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003486
3487 if( ret < 0 )
3488 return( ret );
3489
mohammad160352aecb92018-03-28 23:41:40 -07003490 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003491 {
Darryl Green11999bb2018-03-13 15:22:58 +00003492 MBEDTLS_SSL_DEBUG_MSG( 1,
3493 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003494 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3496 }
3497
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003498 ssl->in_left += ret;
3499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 }
3501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
3504 return( 0 );
3505}
3506
3507/*
3508 * Flush any data not yet written
3509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003511{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003512 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003513 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003517 if( ssl->f_send == NULL )
3518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003520 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003522 }
3523
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003524 /* Avoid incrementing counter if data is flushed */
3525 if( ssl->out_left == 0 )
3526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003528 return( 0 );
3529 }
3530
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 while( ssl->out_left > 0 )
3532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003534 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Hanno Becker2b1e3542018-08-06 11:19:13 +01003536 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003537 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
3541 if( ret <= 0 )
3542 return( ret );
3543
mohammad160352aecb92018-03-28 23:41:40 -07003544 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003545 {
Darryl Green11999bb2018-03-13 15:22:58 +00003546 MBEDTLS_SSL_DEBUG_MSG( 1,
3547 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003548 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3550 }
3551
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 ssl->out_left -= ret;
3553 }
3554
Hanno Becker2b1e3542018-08-06 11:19:13 +01003555#if defined(MBEDTLS_SSL_PROTO_DTLS)
3556 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003557 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003558 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003559 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003560 else
3561#endif
3562 {
3563 ssl->out_hdr = ssl->out_buf + 8;
3564 }
3565 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
3569 return( 0 );
3570}
3571
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003572/*
3573 * Functions to handle the DTLS retransmission state machine
3574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003576/*
3577 * Append current handshake message to current outgoing flight
3578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3583 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3584 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585
3586 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003587 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003588 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 }
3593
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003594 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003595 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003598 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003599 }
3600
3601 /* Copy current handshake message with headers */
3602 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3603 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003604 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 msg->next = NULL;
3606
3607 /* Append to the current flight */
3608 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003610 else
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003613 while( cur->next != NULL )
3614 cur = cur->next;
3615 cur->next = msg;
3616 }
3617
Hanno Becker3b235902018-08-06 09:54:53 +01003618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619 return( 0 );
3620}
3621
3622/*
3623 * Free the current flight of handshake messages
3624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 mbedtls_ssl_flight_item *cur = flight;
3628 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003629
3630 while( cur != NULL )
3631 {
3632 next = cur->next;
3633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 mbedtls_free( cur->p );
3635 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003636
3637 cur = next;
3638 }
3639}
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3642static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003643#endif
3644
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003645/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003646 * Swap transform_out and out_ctr with the alternative ones
3647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003651 unsigned char tmp_out_ctr[8];
3652
3653 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656 return;
3657 }
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003660
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003661 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662 tmp_transform = ssl->transform_out;
3663 ssl->transform_out = ssl->handshake->alt_transform_out;
3664 ssl->handshake->alt_transform_out = tmp_transform;
3665
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003667 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3668 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003669 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003670
3671 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003672 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3675 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3680 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003681 }
3682 }
3683#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003684}
3685
3686/*
3687 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003688 */
3689int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3690{
3691 int ret = 0;
3692
3693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3694
3695 ret = mbedtls_ssl_flight_transmit( ssl );
3696
3697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3698
3699 return( ret );
3700}
3701
3702/*
3703 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704 *
3705 * Need to remember the current message in case flush_output returns
3706 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003707 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003708 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003709int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003710{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003711 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003715 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003717
3718 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003719 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003720 ssl_swap_epochs( ssl );
3721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003723 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003724
3725 while( ssl->handshake->cur_msg != NULL )
3726 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003727 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003728 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003729
Hanno Beckere1dcb032018-08-17 16:47:58 +01003730 int const is_finished =
3731 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3732 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3733
Hanno Becker04da1892018-08-14 13:22:10 +01003734 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3735 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3736
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003737 /* Swap epochs before sending Finished: we can't do it after
3738 * sending ChangeCipherSpec, in case write returns WANT_READ.
3739 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003740 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003741 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003743 ssl_swap_epochs( ssl );
3744 }
3745
Hanno Becker67bc7c32018-08-06 11:33:50 +01003746 ret = ssl_get_remaining_payload_in_datagram( ssl );
3747 if( ret < 0 )
3748 return( ret );
3749 max_frag_len = (size_t) ret;
3750
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003751 /* CCS is copied as is, while HS messages may need fragmentation */
3752 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3753 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003754 if( max_frag_len == 0 )
3755 {
3756 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3757 return( ret );
3758
3759 continue;
3760 }
3761
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003762 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003764 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003765
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003766 /* Update position inside current message */
3767 ssl->handshake->cur_msg_p += cur->len;
3768 }
3769 else
3770 {
3771 const unsigned char * const p = ssl->handshake->cur_msg_p;
3772 const size_t hs_len = cur->len - 12;
3773 const size_t frag_off = p - ( cur->p + 12 );
3774 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003775 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003776
Hanno Beckere1dcb032018-08-17 16:47:58 +01003777 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003778 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003779 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003780 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781
Hanno Becker67bc7c32018-08-06 11:33:50 +01003782 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3783 return( ret );
3784
3785 continue;
3786 }
3787 max_hs_frag_len = max_frag_len - 12;
3788
3789 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3790 max_hs_frag_len : rem_len;
3791
3792 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003793 {
3794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003795 (unsigned) cur_hs_frag_len,
3796 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003797 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003798
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003799 /* Messages are stored with handshake headers as if not fragmented,
3800 * copy beginning of headers then fill fragmentation fields.
3801 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3802 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003803
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3805 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3806 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3807
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3809 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3810 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003811
3812 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3813
Hanno Becker3f7b9732018-08-28 09:53:25 +01003814 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003815 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3816 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003817 ssl->out_msgtype = cur->type;
3818
3819 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003821 }
3822
3823 /* If done with the current message move to the next one if any */
3824 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3825 {
3826 if( cur->next != NULL )
3827 {
3828 ssl->handshake->cur_msg = cur->next;
3829 ssl->handshake->cur_msg_p = cur->next->p + 12;
3830 }
3831 else
3832 {
3833 ssl->handshake->cur_msg = NULL;
3834 ssl->handshake->cur_msg_p = NULL;
3835 }
3836 }
3837
3838 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003839 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842 return( ret );
3843 }
3844 }
3845
Hanno Becker67bc7c32018-08-06 11:33:50 +01003846 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3847 return( ret );
3848
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003849 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3851 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003852 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003855 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3856 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003857
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003859
3860 return( 0 );
3861}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003862
3863/*
3864 * To be called when the last message of an incoming flight is received.
3865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867{
3868 /* We won't need to resend that one any more */
3869 ssl_flight_free( ssl->handshake->flight );
3870 ssl->handshake->flight = NULL;
3871 ssl->handshake->cur_msg = NULL;
3872
3873 /* The next incoming flight will start with this msg_seq */
3874 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3875
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003876 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003877 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003878
Hanno Becker0271f962018-08-16 13:23:47 +01003879 /* Clear future message buffering structure. */
3880 ssl_buffering_free( ssl );
3881
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003882 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003883 ssl_set_timer( ssl, 0 );
3884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3886 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003889 }
3890 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003892}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003893
3894/*
3895 * To be called when the last message of an outgoing flight is send.
3896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003899 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003900 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3903 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003906 }
3907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003911
Paul Bakker5121ce52009-01-03 21:22:43 +00003912/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003913 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003915
3916/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003917 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003918 *
3919 * - fill in handshake headers
3920 * - update handshake checksum
3921 * - DTLS: save message for resending
3922 * - then pass to the record layer
3923 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003924 * DTLS: except for HelloRequest, messages are only queued, and will only be
3925 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003926 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003927 * Inputs:
3928 * - ssl->out_msglen: 4 + actual handshake message len
3929 * (4 is the size of handshake headers for TLS)
3930 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3931 * - ssl->out_msg + 4: the handshake message body
3932 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003933 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003934 * - ssl->out_msglen: the length of the record contents
3935 * (including handshake headers but excluding record headers)
3936 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003938int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003940 int ret;
3941 const size_t hs_len = ssl->out_msglen - 4;
3942 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003943
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3945
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003946 /*
3947 * Sanity checks
3948 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003949 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003950 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3951 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003952 /* In SSLv3, the client might send a NoCertificate alert. */
3953#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3954 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3955 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3956 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3957#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3958 {
3959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3961 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003964 /* Whenever we send anything different from a
3965 * HelloRequest we should be in a handshake - double check. */
3966 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3967 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003968 ssl->handshake == NULL )
3969 {
3970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003975 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003976 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003982#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003983
Hanno Beckerb50a2532018-08-06 11:52:54 +01003984 /* Double-check that we did not exceed the bounds
3985 * of the outgoing record buffer.
3986 * This should never fail as the various message
3987 * writing functions must obey the bounds of the
3988 * outgoing record buffer, but better be safe.
3989 *
3990 * Note: We deliberately do not check for the MTU or MFL here.
3991 */
3992 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3993 {
3994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3995 "size %u, maximum %u",
3996 (unsigned) ssl->out_msglen,
3997 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3999 }
4000
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004001 /*
4002 * Fill handshake headers
4003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004006 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4007 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4008 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004010 /*
4011 * DTLS has additional fields in the Handshake layer,
4012 * between the length field and the actual payload:
4013 * uint16 message_seq;
4014 * uint24 fragment_offset;
4015 * uint24 fragment_length;
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004018 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004019 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004020 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004021 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004022 {
4023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4024 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004025 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004026 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4028 }
4029
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004030 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004031 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004033 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004034 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004035 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004036 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4037 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4038 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004039 }
4040 else
4041 {
4042 ssl->out_msg[4] = 0;
4043 ssl->out_msg[5] = 0;
4044 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004045
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004046 /* Handshake hashes are computed without fragmentation,
4047 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004048 memset( ssl->out_msg + 6, 0x00, 3 );
4049 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004052
Hanno Becker0207e532018-08-28 10:28:28 +01004053 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004054 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4055 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 }
4057
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004058 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004061 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4062 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004063 {
4064 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067 return( ret );
4068 }
4069 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004070 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004071#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004072 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004073 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004074 {
4075 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4076 return( ret );
4077 }
4078 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004079
4080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4081
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004082 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004083}
4084
4085/*
4086 * Record layer functions
4087 */
4088
4089/*
4090 * Write current record.
4091 *
4092 * Uses:
4093 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4094 * - ssl->out_msglen: length of the record content (excl headers)
4095 * - ssl->out_msg: record content
4096 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004097int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004098{
4099 int ret, done = 0;
4100 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004101 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004102
4103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004106 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004108 {
4109 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112 return( ret );
4113 }
4114
4115 len = ssl->out_msglen;
4116 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4120 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 ret = mbedtls_ssl_hw_record_write( ssl );
4125 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4128 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004129 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004130
4131 if( ret == 0 )
4132 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004135 if( !done )
4136 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004137 unsigned i;
4138 size_t protected_record_size;
4139
Hanno Becker6430faf2019-05-08 11:57:13 +01004140 /* Skip writing the record content type to after the encryption,
4141 * as it may change when using the CID extension. */
4142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004144 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004145
Hanno Becker19859472018-08-06 09:40:20 +01004146 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004147 ssl->out_len[0] = (unsigned char)( len >> 8 );
4148 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004149
Paul Bakker48916f92012-09-16 19:57:18 +00004150 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004151 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004152 mbedtls_record rec;
4153
4154 rec.buf = ssl->out_iv;
4155 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4156 ( ssl->out_iv - ssl->out_buf );
4157 rec.data_len = ssl->out_msglen;
4158 rec.data_offset = ssl->out_msg - rec.buf;
4159
4160 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4161 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4162 ssl->conf->transport, rec.ver );
4163 rec.type = ssl->out_msgtype;
4164
Hanno Beckera0e20d02019-05-15 14:03:01 +01004165#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004166 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004167 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004168#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004169
Hanno Beckera18d1322018-01-03 14:27:32 +00004170 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004171 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004174 return( ret );
4175 }
4176
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004177 if( rec.data_offset != 0 )
4178 {
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4181 }
4182
Hanno Becker6430faf2019-05-08 11:57:13 +01004183 /* Update the record content type and CID. */
4184 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004185#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004186 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004187#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004188 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004189 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4190 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004191 }
4192
Hanno Becker5903de42019-05-03 14:46:38 +01004193 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004194
4195#if defined(MBEDTLS_SSL_PROTO_DTLS)
4196 /* In case of DTLS, double-check that we don't exceed
4197 * the remaining space in the datagram. */
4198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4199 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004200 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004201 if( ret < 0 )
4202 return( ret );
4203
4204 if( protected_record_size > (size_t) ret )
4205 {
4206 /* Should never happen */
4207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4208 }
4209 }
4210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004211
Hanno Becker6430faf2019-05-08 11:57:13 +01004212 /* Now write the potentially updated record content type. */
4213 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004216 "version = [%d:%d], msglen = %d",
4217 ssl->out_hdr[0], ssl->out_hdr[1],
4218 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004221 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004222
4223 ssl->out_left += protected_record_size;
4224 ssl->out_hdr += protected_record_size;
4225 ssl_update_out_pointers( ssl, ssl->transform_out );
4226
Hanno Becker04484622018-08-06 09:49:38 +01004227 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4228 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4229 break;
4230
4231 /* The loop goes to its end iff the counter is wrapping */
4232 if( i == ssl_ep_len( ssl ) )
4233 {
4234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4235 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004237 }
4238
Hanno Becker67bc7c32018-08-06 11:33:50 +01004239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4241 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004243 size_t remaining;
4244 ret = ssl_get_remaining_payload_in_datagram( ssl );
4245 if( ret < 0 )
4246 {
4247 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4248 ret );
4249 return( ret );
4250 }
4251
4252 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004253 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004254 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004255 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004256 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004257 else
4258 {
Hanno Becker513815a2018-08-20 11:56:09 +01004259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004260 }
4261 }
4262#endif /* MBEDTLS_SSL_PROTO_DTLS */
4263
4264 if( ( flush == SSL_FORCE_FLUSH ) &&
4265 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004268 return( ret );
4269 }
4270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
4273 return( 0 );
4274}
4275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004277
4278static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4279{
4280 if( ssl->in_msglen < ssl->in_hslen ||
4281 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4282 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4283 {
4284 return( 1 );
4285 }
4286 return( 0 );
4287}
Hanno Becker44650b72018-08-16 12:51:11 +01004288
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004289static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004290{
4291 return( ( ssl->in_msg[9] << 16 ) |
4292 ( ssl->in_msg[10] << 8 ) |
4293 ssl->in_msg[11] );
4294}
4295
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004296static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004297{
4298 return( ( ssl->in_msg[6] << 16 ) |
4299 ( ssl->in_msg[7] << 8 ) |
4300 ssl->in_msg[8] );
4301}
4302
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004303static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004304{
4305 uint32_t msg_len, frag_off, frag_len;
4306
4307 msg_len = ssl_get_hs_total_len( ssl );
4308 frag_off = ssl_get_hs_frag_off( ssl );
4309 frag_len = ssl_get_hs_frag_len( ssl );
4310
4311 if( frag_off > msg_len )
4312 return( -1 );
4313
4314 if( frag_len > msg_len - frag_off )
4315 return( -1 );
4316
4317 if( frag_len + 12 > ssl->in_msglen )
4318 return( -1 );
4319
4320 return( 0 );
4321}
4322
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004323/*
4324 * Mark bits in bitmask (used for DTLS HS reassembly)
4325 */
4326static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4327{
4328 unsigned int start_bits, end_bits;
4329
4330 start_bits = 8 - ( offset % 8 );
4331 if( start_bits != 8 )
4332 {
4333 size_t first_byte_idx = offset / 8;
4334
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004335 /* Special case */
4336 if( len <= start_bits )
4337 {
4338 for( ; len != 0; len-- )
4339 mask[first_byte_idx] |= 1 << ( start_bits - len );
4340
4341 /* Avoid potential issues with offset or len becoming invalid */
4342 return;
4343 }
4344
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004345 offset += start_bits; /* Now offset % 8 == 0 */
4346 len -= start_bits;
4347
4348 for( ; start_bits != 0; start_bits-- )
4349 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4350 }
4351
4352 end_bits = len % 8;
4353 if( end_bits != 0 )
4354 {
4355 size_t last_byte_idx = ( offset + len ) / 8;
4356
4357 len -= end_bits; /* Now len % 8 == 0 */
4358
4359 for( ; end_bits != 0; end_bits-- )
4360 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4361 }
4362
4363 memset( mask + offset / 8, 0xFF, len / 8 );
4364}
4365
4366/*
4367 * Check that bitmask is full
4368 */
4369static int ssl_bitmask_check( unsigned char *mask, size_t len )
4370{
4371 size_t i;
4372
4373 for( i = 0; i < len / 8; i++ )
4374 if( mask[i] != 0xFF )
4375 return( -1 );
4376
4377 for( i = 0; i < len % 8; i++ )
4378 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4379 return( -1 );
4380
4381 return( 0 );
4382}
4383
Hanno Becker56e205e2018-08-16 09:06:12 +01004384/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004385static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004386 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004387{
Hanno Becker56e205e2018-08-16 09:06:12 +01004388 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004389
Hanno Becker56e205e2018-08-16 09:06:12 +01004390 alloc_len = 12; /* Handshake header */
4391 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004392
Hanno Beckerd07df862018-08-16 09:14:58 +01004393 if( add_bitmap )
4394 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004395
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004396 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004397}
Hanno Becker56e205e2018-08-16 09:06:12 +01004398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004400
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004401static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004402{
4403 return( ( ssl->in_msg[1] << 16 ) |
4404 ( ssl->in_msg[2] << 8 ) |
4405 ssl->in_msg[3] );
4406}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004407
Simon Butcher99000142016-10-13 17:21:01 +01004408int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004413 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004415 }
4416
Hanno Becker12555c62018-08-16 12:47:53 +01004417 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004420 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004421 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004425 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004426 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004427 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004428
Hanno Becker44650b72018-08-16 12:51:11 +01004429 if( ssl_check_hs_header( ssl ) != 0 )
4430 {
4431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4433 }
4434
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004435 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004436 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4437 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4438 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4439 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004440 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004441 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4442 {
4443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4444 recv_msg_seq,
4445 ssl->handshake->in_msg_seq ) );
4446 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4447 }
4448
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004449 /* Retransmit only on last message from previous flight, to avoid
4450 * too many retransmissions.
4451 * Besides, No sane server ever retransmits HelloVerifyRequest */
4452 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004456 "message_seq = %d, start_of_flight = %d",
4457 recv_msg_seq,
4458 ssl->handshake->in_flight_start_seq ) );
4459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004463 return( ret );
4464 }
4465 }
4466 else
4467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004469 "message_seq = %d, expected = %d",
4470 recv_msg_seq,
4471 ssl->handshake->in_msg_seq ) );
4472 }
4473
Hanno Becker90333da2017-10-10 11:27:13 +01004474 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004475 }
4476 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004477
Hanno Becker6d97ef52018-08-16 13:09:04 +01004478 /* Message reassembly is handled alongside buffering of future
4479 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004480 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004481 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004482 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004485 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004486 }
4487 }
4488 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004490 /* With TLS we don't handle fragmentation (for now) */
4491 if( ssl->in_msglen < ssl->in_hslen )
4492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4494 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004495 }
4496
Simon Butcher99000142016-10-13 17:21:01 +01004497 return( 0 );
4498}
4499
4500void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4501{
Hanno Becker0271f962018-08-16 13:23:47 +01004502 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004503
Hanno Becker0271f962018-08-16 13:23:47 +01004504 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004505 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004506 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004507 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004508
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004509 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004512 ssl->handshake != NULL )
4513 {
Hanno Becker0271f962018-08-16 13:23:47 +01004514 unsigned offset;
4515 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004516
Hanno Becker0271f962018-08-16 13:23:47 +01004517 /* Increment handshake sequence number */
4518 hs->in_msg_seq++;
4519
4520 /*
4521 * Clear up handshake buffering and reassembly structure.
4522 */
4523
4524 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004525 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004526
4527 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004528 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4529 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004530 offset++, hs_buf++ )
4531 {
4532 *hs_buf = *(hs_buf + 1);
4533 }
4534
4535 /* Create a fresh last entry */
4536 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004537 }
4538#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004539}
4540
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004541/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004542 * DTLS anti-replay: RFC 6347 4.1.2.6
4543 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004544 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4545 * Bit n is set iff record number in_window_top - n has been seen.
4546 *
4547 * Usually, in_window_top is the last record number seen and the lsb of
4548 * in_window is set. The only exception is the initial state (record number 0
4549 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4552static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004553{
4554 ssl->in_window_top = 0;
4555 ssl->in_window = 0;
4556}
4557
4558static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4559{
4560 return( ( (uint64_t) buf[0] << 40 ) |
4561 ( (uint64_t) buf[1] << 32 ) |
4562 ( (uint64_t) buf[2] << 24 ) |
4563 ( (uint64_t) buf[3] << 16 ) |
4564 ( (uint64_t) buf[4] << 8 ) |
4565 ( (uint64_t) buf[5] ) );
4566}
4567
4568/*
4569 * Return 0 if sequence number is acceptable, -1 otherwise
4570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004572{
4573 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4574 uint64_t bit;
4575
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004576 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004577 return( 0 );
4578
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004579 if( rec_seqnum > ssl->in_window_top )
4580 return( 0 );
4581
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004582 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004583
4584 if( bit >= 64 )
4585 return( -1 );
4586
4587 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4588 return( -1 );
4589
4590 return( 0 );
4591}
4592
4593/*
4594 * Update replay window on new validated record
4595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004597{
4598 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4599
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004600 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004601 return;
4602
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004603 if( rec_seqnum > ssl->in_window_top )
4604 {
4605 /* Update window_top and the contents of the window */
4606 uint64_t shift = rec_seqnum - ssl->in_window_top;
4607
4608 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004609 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004610 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004611 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004613 ssl->in_window |= 1;
4614 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004615
4616 ssl->in_window_top = rec_seqnum;
4617 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004618 else
4619 {
4620 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004621 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004622
4623 if( bit < 64 ) /* Always true, but be extra sure */
4624 ssl->in_window |= (uint64_t) 1 << bit;
4625 }
4626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004628
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004629#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004630/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004631static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4632
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004633/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004634 * Without any SSL context, check if a datagram looks like a ClientHello with
4635 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004636 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004637 *
4638 * - if cookie is valid, return 0
4639 * - if ClientHello looks superficially valid but cookie is not,
4640 * fill obuf and set olen, then
4641 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4642 * - otherwise return a specific error code
4643 */
4644static int ssl_check_dtls_clihlo_cookie(
4645 mbedtls_ssl_cookie_write_t *f_cookie_write,
4646 mbedtls_ssl_cookie_check_t *f_cookie_check,
4647 void *p_cookie,
4648 const unsigned char *cli_id, size_t cli_id_len,
4649 const unsigned char *in, size_t in_len,
4650 unsigned char *obuf, size_t buf_len, size_t *olen )
4651{
4652 size_t sid_len, cookie_len;
4653 unsigned char *p;
4654
4655 if( f_cookie_write == NULL || f_cookie_check == NULL )
4656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4657
4658 /*
4659 * Structure of ClientHello with record and handshake headers,
4660 * and expected values. We don't need to check a lot, more checks will be
4661 * done when actually parsing the ClientHello - skipping those checks
4662 * avoids code duplication and does not make cookie forging any easier.
4663 *
4664 * 0-0 ContentType type; copied, must be handshake
4665 * 1-2 ProtocolVersion version; copied
4666 * 3-4 uint16 epoch; copied, must be 0
4667 * 5-10 uint48 sequence_number; copied
4668 * 11-12 uint16 length; (ignored)
4669 *
4670 * 13-13 HandshakeType msg_type; (ignored)
4671 * 14-16 uint24 length; (ignored)
4672 * 17-18 uint16 message_seq; copied
4673 * 19-21 uint24 fragment_offset; copied, must be 0
4674 * 22-24 uint24 fragment_length; (ignored)
4675 *
4676 * 25-26 ProtocolVersion client_version; (ignored)
4677 * 27-58 Random random; (ignored)
4678 * 59-xx SessionID session_id; 1 byte len + sid_len content
4679 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4680 * ...
4681 *
4682 * Minimum length is 61 bytes.
4683 */
4684 if( in_len < 61 ||
4685 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4686 in[3] != 0 || in[4] != 0 ||
4687 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4688 {
4689 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4690 }
4691
4692 sid_len = in[59];
4693 if( sid_len > in_len - 61 )
4694 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4695
4696 cookie_len = in[60 + sid_len];
4697 if( cookie_len > in_len - 60 )
4698 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4699
4700 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4701 cli_id, cli_id_len ) == 0 )
4702 {
4703 /* Valid cookie */
4704 return( 0 );
4705 }
4706
4707 /*
4708 * If we get here, we've got an invalid cookie, let's prepare HVR.
4709 *
4710 * 0-0 ContentType type; copied
4711 * 1-2 ProtocolVersion version; copied
4712 * 3-4 uint16 epoch; copied
4713 * 5-10 uint48 sequence_number; copied
4714 * 11-12 uint16 length; olen - 13
4715 *
4716 * 13-13 HandshakeType msg_type; hello_verify_request
4717 * 14-16 uint24 length; olen - 25
4718 * 17-18 uint16 message_seq; copied
4719 * 19-21 uint24 fragment_offset; copied
4720 * 22-24 uint24 fragment_length; olen - 25
4721 *
4722 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4723 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4724 *
4725 * Minimum length is 28.
4726 */
4727 if( buf_len < 28 )
4728 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4729
4730 /* Copy most fields and adapt others */
4731 memcpy( obuf, in, 25 );
4732 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4733 obuf[25] = 0xfe;
4734 obuf[26] = 0xff;
4735
4736 /* Generate and write actual cookie */
4737 p = obuf + 28;
4738 if( f_cookie_write( p_cookie,
4739 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4740 {
4741 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4742 }
4743
4744 *olen = p - obuf;
4745
4746 /* Go back and fill length fields */
4747 obuf[27] = (unsigned char)( *olen - 28 );
4748
4749 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4750 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4751 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4752
4753 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4754 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4755
4756 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4757}
4758
4759/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004760 * Handle possible client reconnect with the same UDP quadruplet
4761 * (RFC 6347 Section 4.2.8).
4762 *
4763 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4764 * that looks like a ClientHello.
4765 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 * - if the input looks like a ClientHello without cookies,
4767 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004768 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769 * - if the input looks like a ClientHello with a valid cookie,
4770 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004771 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004772 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004773 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004774 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004775 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4776 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004777 */
4778static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4779{
4780 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004783 ret = ssl_check_dtls_clihlo_cookie(
4784 ssl->conf->f_cookie_write,
4785 ssl->conf->f_cookie_check,
4786 ssl->conf->p_cookie,
4787 ssl->cli_id, ssl->cli_id_len,
4788 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004789 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004791 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4792
4793 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004794 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004795 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004796 * If the error is permanent we'll catch it later,
4797 * if it's not, then hopefully it'll work next time. */
4798 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4799
4800 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004801 }
4802
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004803 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004804 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004805 /* Got a valid cookie, partially reset context */
4806 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4807 {
4808 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4809 return( ret );
4810 }
4811
4812 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004813 }
4814
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004815 return( ret );
4816}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004817#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004818
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004819static int ssl_check_record_type( uint8_t record_type )
4820{
4821 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4822 record_type != MBEDTLS_SSL_MSG_ALERT &&
4823 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4824 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4825 {
4826 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4827 }
4828
4829 return( 0 );
4830}
4831
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004832/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004833 * ContentType type;
4834 * ProtocolVersion version;
4835 * uint16 epoch; // DTLS only
4836 * uint48 sequence_number; // DTLS only
4837 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004838 *
4839 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004840 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004841 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4842 *
4843 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004844 * 1. proceed with the record if this function returns 0
4845 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4846 * 3. return CLIENT_RECONNECT if this function return that value
4847 * 4. drop the whole datagram if this function returns anything else.
4848 * Point 2 is needed when the peer is resending, and we have already received
4849 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004852{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004853 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004854 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004855
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004856 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004857
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004859 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004860
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004861 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004862#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4864 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4865 ssl->conf->cid_len != 0 )
4866 {
4867 /* Shift pointers to account for record header including CID
4868 * struct {
4869 * ContentType special_type = tls12_cid;
4870 * ProtocolVersion version;
4871 * uint16 epoch;
4872 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004873 * opaque cid[cid_length]; // Additional field compared to
4874 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004875 * uint16 length;
4876 * opaque enc_content[DTLSCiphertext.length];
4877 * } DTLSCiphertext;
4878 */
4879
4880 /* So far, we only support static CID lengths
4881 * fixed in the configuration. */
4882 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4883 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4884 }
4885 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004886#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004887 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004889 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004890
4891#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004892 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4893 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004894 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4895#endif /* MBEDTLS_SSL_PROTO_DTLS */
4896 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4897 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004900 }
4901
4902 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004903 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4906 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004907 }
4908
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004909 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004910 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004911 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4912 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004913 }
4914
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004915 /* Now that the total length of the record header is known, ensure
4916 * that the current datagram is large enough to hold it.
4917 * This would fail, for example, if we received a datagram of
4918 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4919 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4920 if( ret != 0 )
4921 {
4922 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4923 return( ret );
4924 }
4925 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4926
4927 /* Parse and validate record length
4928 * This must happen after the CID parsing because
4929 * its position in the record header depends on
4930 * the presence of a CID. */
4931
4932 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004933 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004934 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4937 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004938 }
4939
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004940 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004941 "version = [%d:%d], msglen = %d",
4942 ssl->in_msgtype,
4943 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004944
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004945 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004946 * DTLS-related tests.
4947 * Check epoch before checking length constraint because
4948 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4949 * message gets duplicated before the corresponding Finished message,
4950 * the second ChangeCipherSpec should be discarded because it belongs
4951 * to an old epoch, but not because its length is shorter than
4952 * the minimum record length for packets using the new record transform.
4953 * Note that these two kinds of failures are handled differently,
4954 * as an unexpected record is silently skipped but an invalid
4955 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004956 */
4957#if defined(MBEDTLS_SSL_PROTO_DTLS)
4958 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4959 {
4960 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4961
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004962 /* Check epoch (and sequence number) with DTLS */
4963 if( rec_epoch != ssl->in_epoch )
4964 {
4965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4966 "expected %d, received %d",
4967 ssl->in_epoch, rec_epoch ) );
4968
4969#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4970 /*
4971 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4972 * access the first byte of record content (handshake type), as we
4973 * have an active transform (possibly iv_len != 0), so use the
4974 * fact that the record header len is 13 instead.
4975 */
4976 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4977 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4978 rec_epoch == 0 &&
4979 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4980 ssl->in_left > 13 &&
4981 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4982 {
4983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4984 "from the same port" ) );
4985 return( ssl_handle_possible_reconnect( ssl ) );
4986 }
4987 else
4988#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004989 {
4990 /* Consider buffering the record. */
4991 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4992 {
4993 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4994 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4995 }
4996
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004997 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004998 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004999 }
5000
5001#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5002 /* Replay detection only works for the current epoch */
5003 if( rec_epoch == ssl->in_epoch &&
5004 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5005 {
5006 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5007 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5008 }
5009#endif
5010 }
5011#endif /* MBEDTLS_SSL_PROTO_DTLS */
5012
Hanno Becker52c6dc62017-05-26 16:07:36 +01005013
5014 /* Check length against bounds of the current transform and version */
5015 if( ssl->transform_in == NULL )
5016 {
5017 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005018 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005019 {
5020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5021 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5022 }
5023 }
5024 else
5025 {
5026 if( ssl->in_msglen < ssl->transform_in->minlen )
5027 {
5028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5029 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5030 }
5031
5032#if defined(MBEDTLS_SSL_PROTO_SSL3)
5033 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005034 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005035 {
5036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5037 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5038 }
5039#endif
5040#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5041 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5042 /*
5043 * TLS encrypted messages can have up to 256 bytes of padding
5044 */
5045 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5046 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005047 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005048 {
5049 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5050 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5051 }
5052#endif
5053 }
5054
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005055 return( 0 );
5056}
Paul Bakker5121ce52009-01-03 21:22:43 +00005057
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005058/*
5059 * If applicable, decrypt (and decompress) record content
5060 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005061static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005062{
5063 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005066 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5069 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005070 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005071 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 ret = mbedtls_ssl_hw_record_read( ssl );
5074 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005075 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005076 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5077 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005078 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005079
5080 if( ret == 0 )
5081 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005082 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005083#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005084 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005085 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005086 mbedtls_record rec;
5087
5088 rec.buf = ssl->in_iv;
5089 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5090 - ( ssl->in_iv - ssl->in_buf );
5091 rec.data_len = ssl->in_msglen;
5092 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005093#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005094 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005095 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005096#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005097
5098 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5099 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5100 ssl->conf->transport, rec.ver );
5101 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005102 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5103 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005105 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005106
Hanno Beckera0e20d02019-05-15 14:03:01 +01005107#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005108 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5109 ssl->conf->ignore_unexpected_cid
5110 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5111 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005112 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005113 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005114 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005115#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005116
Paul Bakker5121ce52009-01-03 21:22:43 +00005117 return( ret );
5118 }
5119
Hanno Becker6430faf2019-05-08 11:57:13 +01005120 if( ssl->in_msgtype != rec.type )
5121 {
5122 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5123 ssl->in_msgtype, rec.type ) );
5124 }
5125
5126 /* The record content type may change during decryption,
5127 * so re-read it. */
5128 ssl->in_msgtype = rec.type;
5129 /* Also update the input buffer, because unfortunately
5130 * the server-side ssl_parse_client_hello() reparses the
5131 * record header when receiving a ClientHello initiating
5132 * a renegotiation. */
5133 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005134 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005135 ssl->in_msglen = rec.data_len;
5136 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5137 ssl->in_len[1] = (unsigned char)( rec.data_len );
5138
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005139 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5140 ssl->in_msg, ssl->in_msglen );
5141
Hanno Beckera0e20d02019-05-15 14:03:01 +01005142#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005143 /* We have already checked the record content type
5144 * in ssl_parse_record_header(), failing or silently
5145 * dropping the record in the case of an unknown type.
5146 *
5147 * Since with the use of CIDs, the record content type
5148 * might change during decryption, re-check the record
5149 * content type, but treat a failure as fatal this time. */
5150 if( ssl_check_record_type( ssl->in_msgtype ) )
5151 {
5152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5153 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5154 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005155#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005156
Angus Grattond8213d02016-05-25 20:56:48 +10005157 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5160 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005161 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005162 else if( ssl->in_msglen == 0 )
5163 {
5164#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5165 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5166 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5167 {
5168 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5170 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5171 }
5172#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5173
5174 ssl->nb_zero++;
5175
5176 /*
5177 * Three or more empty messages may be a DoS attack
5178 * (excessive CPU consumption).
5179 */
5180 if( ssl->nb_zero > 3 )
5181 {
5182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005183 "messages, possible DoS attack" ) );
5184 /* Treat the records as if they were not properly authenticated,
5185 * thereby failing the connection if we see more than allowed
5186 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005187 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5188 }
5189 }
5190 else
5191 ssl->nb_zero = 0;
5192
5193#if defined(MBEDTLS_SSL_PROTO_DTLS)
5194 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5195 {
5196 ; /* in_ctr read from peer, not maintained internally */
5197 }
5198 else
5199#endif
5200 {
5201 unsigned i;
5202 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5203 if( ++ssl->in_ctr[i - 1] != 0 )
5204 break;
5205
5206 /* The loop goes to its end iff the counter is wrapping */
5207 if( i == ssl_ep_len( ssl ) )
5208 {
5209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5210 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5211 }
5212 }
5213
Paul Bakker5121ce52009-01-03 21:22:43 +00005214 }
5215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005217 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005218 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005219 {
5220 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5221 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005223 return( ret );
5224 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005225 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005229 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005231 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005232 }
5233#endif
5234
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005235 return( 0 );
5236}
5237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005238static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005239
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005240/*
5241 * Read a record.
5242 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005243 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5244 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5245 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005246 */
Hanno Becker1097b342018-08-15 14:09:41 +01005247
5248/* Helper functions for mbedtls_ssl_read_record(). */
5249static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005250static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5251static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005252
Hanno Becker327c93b2018-08-15 13:56:18 +01005253int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005254 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005255{
5256 int ret;
5257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005259
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005260 if( ssl->keep_current_message == 0 )
5261 {
5262 do {
Simon Butcher99000142016-10-13 17:21:01 +01005263
Hanno Becker26994592018-08-15 14:14:59 +01005264 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005265 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005266 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005267
Hanno Beckere74d5562018-08-15 14:26:08 +01005268 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005269 {
Hanno Becker40f50842018-08-15 14:48:01 +01005270#if defined(MBEDTLS_SSL_PROTO_DTLS)
5271 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005272
Hanno Becker40f50842018-08-15 14:48:01 +01005273 /* We only check for buffered messages if the
5274 * current datagram is fully consumed. */
5275 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005276 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005277 {
Hanno Becker40f50842018-08-15 14:48:01 +01005278 if( ssl_load_buffered_message( ssl ) == 0 )
5279 have_buffered = 1;
5280 }
5281
5282 if( have_buffered == 0 )
5283#endif /* MBEDTLS_SSL_PROTO_DTLS */
5284 {
5285 ret = ssl_get_next_record( ssl );
5286 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5287 continue;
5288
5289 if( ret != 0 )
5290 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005291 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005292 return( ret );
5293 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005294 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005295 }
5296
5297 ret = mbedtls_ssl_handle_message_type( ssl );
5298
Hanno Becker40f50842018-08-15 14:48:01 +01005299#if defined(MBEDTLS_SSL_PROTO_DTLS)
5300 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5301 {
5302 /* Buffer future message */
5303 ret = ssl_buffer_message( ssl );
5304 if( ret != 0 )
5305 return( ret );
5306
5307 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5308 }
5309#endif /* MBEDTLS_SSL_PROTO_DTLS */
5310
Hanno Becker90333da2017-10-10 11:27:13 +01005311 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5312 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005313
5314 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005315 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005316 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005317 return( ret );
5318 }
5319
Hanno Becker327c93b2018-08-15 13:56:18 +01005320 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005321 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005322 {
5323 mbedtls_ssl_update_handshake_status( ssl );
5324 }
Simon Butcher99000142016-10-13 17:21:01 +01005325 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005326 else
Simon Butcher99000142016-10-13 17:21:01 +01005327 {
Hanno Becker02f59072018-08-15 14:00:24 +01005328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005329 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005330 }
5331
5332 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5333
5334 return( 0 );
5335}
5336
Hanno Becker40f50842018-08-15 14:48:01 +01005337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005338static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005339{
Hanno Becker40f50842018-08-15 14:48:01 +01005340 if( ssl->in_left > ssl->next_record_offset )
5341 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005342
Hanno Becker40f50842018-08-15 14:48:01 +01005343 return( 0 );
5344}
5345
5346static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5347{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005348 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005349 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005350 int ret = 0;
5351
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005352 if( hs == NULL )
5353 return( -1 );
5354
Hanno Beckere00ae372018-08-20 09:39:42 +01005355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5356
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005357 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5358 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5359 {
5360 /* Check if we have seen a ChangeCipherSpec before.
5361 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005362 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005363 {
5364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5365 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005366 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005367 }
5368
Hanno Becker39b8bc92018-08-28 17:17:13 +01005369 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005370 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5371 ssl->in_msglen = 1;
5372 ssl->in_msg[0] = 1;
5373
5374 /* As long as they are equal, the exact value doesn't matter. */
5375 ssl->in_left = 0;
5376 ssl->next_record_offset = 0;
5377
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005378 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005379 goto exit;
5380 }
Hanno Becker37f95322018-08-16 13:55:32 +01005381
Hanno Beckerb8f50142018-08-28 10:01:34 +01005382#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005383 /* Debug only */
5384 {
5385 unsigned offset;
5386 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5387 {
5388 hs_buf = &hs->buffering.hs[offset];
5389 if( hs_buf->is_valid == 1 )
5390 {
5391 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5392 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005393 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005394 }
5395 }
5396 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005397#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005398
5399 /* Check if we have buffered and/or fully reassembled the
5400 * next handshake message. */
5401 hs_buf = &hs->buffering.hs[0];
5402 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5403 {
5404 /* Synthesize a record containing the buffered HS message. */
5405 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5406 ( hs_buf->data[2] << 8 ) |
5407 hs_buf->data[3];
5408
5409 /* Double-check that we haven't accidentally buffered
5410 * a message that doesn't fit into the input buffer. */
5411 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5412 {
5413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5414 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5415 }
5416
5417 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5418 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5419 hs_buf->data, msg_len + 12 );
5420
5421 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5422 ssl->in_hslen = msg_len + 12;
5423 ssl->in_msglen = msg_len + 12;
5424 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5425
5426 ret = 0;
5427 goto exit;
5428 }
5429 else
5430 {
5431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5432 hs->in_msg_seq ) );
5433 }
5434
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005435 ret = -1;
5436
5437exit:
5438
5439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5440 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005441}
5442
Hanno Beckera02b0b42018-08-21 17:20:27 +01005443static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5444 size_t desired )
5445{
5446 int offset;
5447 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005448 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5449 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005450
Hanno Becker01315ea2018-08-21 17:22:17 +01005451 /* Get rid of future records epoch first, if such exist. */
5452 ssl_free_buffered_record( ssl );
5453
5454 /* Check if we have enough space available now. */
5455 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5456 hs->buffering.total_bytes_buffered ) )
5457 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005458 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005459 return( 0 );
5460 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005461
Hanno Becker4f432ad2018-08-28 10:02:32 +01005462 /* We don't have enough space to buffer the next expected handshake
5463 * message. Remove buffers used for future messages to gain space,
5464 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005465 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5466 offset >= 0; offset-- )
5467 {
5468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5469 offset ) );
5470
Hanno Beckerb309b922018-08-23 13:18:05 +01005471 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005472
5473 /* Check if we have enough space available now. */
5474 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5475 hs->buffering.total_bytes_buffered ) )
5476 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005478 return( 0 );
5479 }
5480 }
5481
5482 return( -1 );
5483}
5484
Hanno Becker40f50842018-08-15 14:48:01 +01005485static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5486{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005487 int ret = 0;
5488 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5489
5490 if( hs == NULL )
5491 return( 0 );
5492
5493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5494
5495 switch( ssl->in_msgtype )
5496 {
5497 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5498 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005499
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005500 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005501 break;
5502
5503 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005504 {
5505 unsigned recv_msg_seq_offset;
5506 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5507 mbedtls_ssl_hs_buffer *hs_buf;
5508 size_t msg_len = ssl->in_hslen - 12;
5509
5510 /* We should never receive an old handshake
5511 * message - double-check nonetheless. */
5512 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5513 {
5514 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5515 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5516 }
5517
5518 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5519 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5520 {
5521 /* Silently ignore -- message too far in the future */
5522 MBEDTLS_SSL_DEBUG_MSG( 2,
5523 ( "Ignore future HS message with sequence number %u, "
5524 "buffering window %u - %u",
5525 recv_msg_seq, ssl->handshake->in_msg_seq,
5526 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5527
5528 goto exit;
5529 }
5530
5531 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5532 recv_msg_seq, recv_msg_seq_offset ) );
5533
5534 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5535
5536 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005537 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005538 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005539 size_t reassembly_buf_sz;
5540
Hanno Becker37f95322018-08-16 13:55:32 +01005541 hs_buf->is_fragmented =
5542 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5543
5544 /* We copy the message back into the input buffer
5545 * after reassembly, so check that it's not too large.
5546 * This is an implementation-specific limitation
5547 * and not one from the standard, hence it is not
5548 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005549 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005550 {
5551 /* Ignore message */
5552 goto exit;
5553 }
5554
Hanno Beckere0b150f2018-08-21 15:51:03 +01005555 /* Check if we have enough space to buffer the message. */
5556 if( hs->buffering.total_bytes_buffered >
5557 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5558 {
5559 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5560 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5561 }
5562
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005563 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5564 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005565
5566 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5567 hs->buffering.total_bytes_buffered ) )
5568 {
5569 if( recv_msg_seq_offset > 0 )
5570 {
5571 /* If we can't buffer a future message because
5572 * of space limitations -- ignore. */
5573 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",
5574 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5575 (unsigned) hs->buffering.total_bytes_buffered ) );
5576 goto exit;
5577 }
Hanno Beckere1801392018-08-21 16:51:05 +01005578 else
5579 {
5580 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",
5581 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5582 (unsigned) hs->buffering.total_bytes_buffered ) );
5583 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005584
Hanno Beckera02b0b42018-08-21 17:20:27 +01005585 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005586 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005587 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",
5588 (unsigned) msg_len,
5589 (unsigned) reassembly_buf_sz,
5590 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005591 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005592 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5593 goto exit;
5594 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005595 }
5596
5597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5598 msg_len ) );
5599
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005600 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5601 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005602 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005603 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005604 goto exit;
5605 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005606 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005607
5608 /* Prepare final header: copy msg_type, length and message_seq,
5609 * then add standardised fragment_offset and fragment_length */
5610 memcpy( hs_buf->data, ssl->in_msg, 6 );
5611 memset( hs_buf->data + 6, 0, 3 );
5612 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5613
5614 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005615
5616 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005617 }
5618 else
5619 {
5620 /* Make sure msg_type and length are consistent */
5621 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5622 {
5623 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5624 /* Ignore */
5625 goto exit;
5626 }
5627 }
5628
Hanno Becker4422bbb2018-08-20 09:40:19 +01005629 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005630 {
5631 size_t frag_len, frag_off;
5632 unsigned char * const msg = hs_buf->data + 12;
5633
5634 /*
5635 * Check and copy current fragment
5636 */
5637
5638 /* Validation of header fields already done in
5639 * mbedtls_ssl_prepare_handshake_record(). */
5640 frag_off = ssl_get_hs_frag_off( ssl );
5641 frag_len = ssl_get_hs_frag_len( ssl );
5642
5643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5644 frag_off, frag_len ) );
5645 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5646
5647 if( hs_buf->is_fragmented )
5648 {
5649 unsigned char * const bitmask = msg + msg_len;
5650 ssl_bitmask_set( bitmask, frag_off, frag_len );
5651 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5652 msg_len ) == 0 );
5653 }
5654 else
5655 {
5656 hs_buf->is_complete = 1;
5657 }
5658
5659 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5660 hs_buf->is_complete ? "" : "not yet " ) );
5661 }
5662
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005663 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005664 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005665
5666 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005667 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005668 break;
5669 }
5670
5671exit:
5672
5673 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5674 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005675}
5676#endif /* MBEDTLS_SSL_PROTO_DTLS */
5677
Hanno Becker1097b342018-08-15 14:09:41 +01005678static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005679{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005680 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005681 * Consume last content-layer message and potentially
5682 * update in_msglen which keeps track of the contents'
5683 * consumption state.
5684 *
5685 * (1) Handshake messages:
5686 * Remove last handshake message, move content
5687 * and adapt in_msglen.
5688 *
5689 * (2) Alert messages:
5690 * Consume whole record content, in_msglen = 0.
5691 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005692 * (3) Change cipher spec:
5693 * Consume whole record content, in_msglen = 0.
5694 *
5695 * (4) Application data:
5696 * Don't do anything - the record layer provides
5697 * the application data as a stream transport
5698 * and consumes through mbedtls_ssl_read only.
5699 *
5700 */
5701
5702 /* Case (1): Handshake messages */
5703 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005704 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005705 /* Hard assertion to be sure that no application data
5706 * is in flight, as corrupting ssl->in_msglen during
5707 * ssl->in_offt != NULL is fatal. */
5708 if( ssl->in_offt != NULL )
5709 {
5710 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5711 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5712 }
5713
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005714 /*
5715 * Get next Handshake message in the current record
5716 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005717
Hanno Becker4a810fb2017-05-24 16:27:30 +01005718 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005719 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005720 * current handshake content: If DTLS handshake
5721 * fragmentation is used, that's the fragment
5722 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005723 * size here is faulty and should be changed at
5724 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005725 * (2) While it doesn't seem to cause problems, one
5726 * has to be very careful not to assume that in_hslen
5727 * is always <= in_msglen in a sensible communication.
5728 * Again, it's wrong for DTLS handshake fragmentation.
5729 * The following check is therefore mandatory, and
5730 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005731 * Additionally, ssl->in_hslen might be arbitrarily out of
5732 * bounds after handling a DTLS message with an unexpected
5733 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005734 */
5735 if( ssl->in_hslen < ssl->in_msglen )
5736 {
5737 ssl->in_msglen -= ssl->in_hslen;
5738 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5739 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005740
Hanno Becker4a810fb2017-05-24 16:27:30 +01005741 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5742 ssl->in_msg, ssl->in_msglen );
5743 }
5744 else
5745 {
5746 ssl->in_msglen = 0;
5747 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005748
Hanno Becker4a810fb2017-05-24 16:27:30 +01005749 ssl->in_hslen = 0;
5750 }
5751 /* Case (4): Application data */
5752 else if( ssl->in_offt != NULL )
5753 {
5754 return( 0 );
5755 }
5756 /* Everything else (CCS & Alerts) */
5757 else
5758 {
5759 ssl->in_msglen = 0;
5760 }
5761
Hanno Becker1097b342018-08-15 14:09:41 +01005762 return( 0 );
5763}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005764
Hanno Beckere74d5562018-08-15 14:26:08 +01005765static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5766{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005767 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005768 return( 1 );
5769
5770 return( 0 );
5771}
5772
Hanno Becker5f066e72018-08-16 14:56:31 +01005773#if defined(MBEDTLS_SSL_PROTO_DTLS)
5774
5775static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5776{
5777 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5778 if( hs == NULL )
5779 return;
5780
Hanno Becker01315ea2018-08-21 17:22:17 +01005781 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005782 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005783 hs->buffering.total_bytes_buffered -=
5784 hs->buffering.future_record.len;
5785
5786 mbedtls_free( hs->buffering.future_record.data );
5787 hs->buffering.future_record.data = NULL;
5788 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005789}
5790
5791static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5792{
5793 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5794 unsigned char * rec;
5795 size_t rec_len;
5796 unsigned rec_epoch;
5797
5798 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5799 return( 0 );
5800
5801 if( hs == NULL )
5802 return( 0 );
5803
Hanno Becker5f066e72018-08-16 14:56:31 +01005804 rec = hs->buffering.future_record.data;
5805 rec_len = hs->buffering.future_record.len;
5806 rec_epoch = hs->buffering.future_record.epoch;
5807
5808 if( rec == NULL )
5809 return( 0 );
5810
Hanno Becker4cb782d2018-08-20 11:19:05 +01005811 /* Only consider loading future records if the
5812 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005813 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005814 return( 0 );
5815
Hanno Becker5f066e72018-08-16 14:56:31 +01005816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5817
5818 if( rec_epoch != ssl->in_epoch )
5819 {
5820 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5821 goto exit;
5822 }
5823
5824 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5825
5826 /* Double-check that the record is not too large */
5827 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5828 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5829 {
5830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5831 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5832 }
5833
5834 memcpy( ssl->in_hdr, rec, rec_len );
5835 ssl->in_left = rec_len;
5836 ssl->next_record_offset = 0;
5837
5838 ssl_free_buffered_record( ssl );
5839
5840exit:
5841 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5842 return( 0 );
5843}
5844
5845static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5846{
5847 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5848 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005849 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005850
5851 /* Don't buffer future records outside handshakes. */
5852 if( hs == NULL )
5853 return( 0 );
5854
5855 /* Only buffer handshake records (we are only interested
5856 * in Finished messages). */
5857 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5858 return( 0 );
5859
5860 /* Don't buffer more than one future epoch record. */
5861 if( hs->buffering.future_record.data != NULL )
5862 return( 0 );
5863
Hanno Becker01315ea2018-08-21 17:22:17 +01005864 /* Don't buffer record if there's not enough buffering space remaining. */
5865 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5866 hs->buffering.total_bytes_buffered ) )
5867 {
5868 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",
5869 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5870 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005871 return( 0 );
5872 }
5873
Hanno Becker5f066e72018-08-16 14:56:31 +01005874 /* Buffer record */
5875 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5876 ssl->in_epoch + 1 ) );
5877 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5878 rec_hdr_len + ssl->in_msglen );
5879
5880 /* ssl_parse_record_header() only considers records
5881 * of the next epoch as candidates for buffering. */
5882 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005883 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005884
5885 hs->buffering.future_record.data =
5886 mbedtls_calloc( 1, hs->buffering.future_record.len );
5887 if( hs->buffering.future_record.data == NULL )
5888 {
5889 /* If we run out of RAM trying to buffer a
5890 * record from the next epoch, just ignore. */
5891 return( 0 );
5892 }
5893
Hanno Becker01315ea2018-08-21 17:22:17 +01005894 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005895
Hanno Becker01315ea2018-08-21 17:22:17 +01005896 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005897 return( 0 );
5898}
5899
5900#endif /* MBEDTLS_SSL_PROTO_DTLS */
5901
Hanno Beckere74d5562018-08-15 14:26:08 +01005902static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005903{
5904 int ret;
5905
Hanno Becker5f066e72018-08-16 14:56:31 +01005906#if defined(MBEDTLS_SSL_PROTO_DTLS)
5907 /* We might have buffered a future record; if so,
5908 * and if the epoch matches now, load it.
5909 * On success, this call will set ssl->in_left to
5910 * the length of the buffered record, so that
5911 * the calls to ssl_fetch_input() below will
5912 * essentially be no-ops. */
5913 ret = ssl_load_buffered_record( ssl );
5914 if( ret != 0 )
5915 return( ret );
5916#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005917
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005918 /* Reset in pointers to default state for TLS/DTLS records,
5919 * assuming no CID and no offset between record content and
5920 * record plaintext. */
5921 ssl_update_in_pointers( ssl );
5922
5923 /* Ensure that we have enough space available for the default form
5924 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5925 * with no space for CIDs counted in). */
5926 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5927 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005930 return( ret );
5931 }
5932
5933 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005935#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005936 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5937 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005938 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005939 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5940 {
5941 ret = ssl_buffer_future_record( ssl );
5942 if( ret != 0 )
5943 return( ret );
5944
5945 /* Fall through to handling of unexpected records */
5946 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5947 }
5948
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005949 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5950 {
5951 /* Skip unexpected record (but not whole datagram) */
5952 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005953 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005954
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005955 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5956 "(header)" ) );
5957 }
5958 else
5959 {
5960 /* Skip invalid record and the rest of the datagram */
5961 ssl->next_record_offset = 0;
5962 ssl->in_left = 0;
5963
5964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5965 "(header)" ) );
5966 }
5967
5968 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005969 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005970 }
5971#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005972 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005973 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005974
5975 /*
5976 * Read and optionally decrypt the message contents
5977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005979 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005981 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005982 return( ret );
5983 }
5984
5985 /* Done reading this record, get ready for the next one */
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 )
Hanno Beckere65ce782017-05-22 14:47:48 +01005988 {
Hanno Becker5903de42019-05-03 14:46:38 +01005989 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005990 if( ssl->next_record_offset < ssl->in_left )
5991 {
5992 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5993 }
5994 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005995 else
5996#endif
5997 ssl->in_left = 0;
5998
5999 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006001#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006002 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006003 {
6004 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006005 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006006 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006007 /* Except when waiting for Finished as a bad mac here
6008 * probably means something went wrong in the handshake
6009 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6010 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6011 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6012 {
6013#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6014 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6015 {
6016 mbedtls_ssl_send_alert_message( ssl,
6017 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6018 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6019 }
6020#endif
6021 return( ret );
6022 }
6023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006024#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006025 if( ssl->conf->badmac_limit != 0 &&
6026 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006028 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6029 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006030 }
6031#endif
6032
Hanno Becker4a810fb2017-05-24 16:27:30 +01006033 /* As above, invalid records cause
6034 * dismissal of the whole datagram. */
6035
6036 ssl->next_record_offset = 0;
6037 ssl->in_left = 0;
6038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006040 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006041 }
6042
6043 return( ret );
6044 }
6045 else
6046#endif
6047 {
6048 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6050 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006052 mbedtls_ssl_send_alert_message( ssl,
6053 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6054 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006055 }
6056#endif
6057 return( ret );
6058 }
6059 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006060
Simon Butcher99000142016-10-13 17:21:01 +01006061 return( 0 );
6062}
6063
6064int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6065{
6066 int ret;
6067
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006068 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006069 * Handle particular types of records
6070 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006071 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006072 {
Simon Butcher99000142016-10-13 17:21:01 +01006073 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6074 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006075 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006076 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006077 }
6078
Hanno Beckere678eaa2018-08-21 14:57:46 +01006079 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006080 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006081 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006082 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006083 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6084 ssl->in_msglen ) );
6085 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006086 }
6087
Hanno Beckere678eaa2018-08-21 14:57:46 +01006088 if( ssl->in_msg[0] != 1 )
6089 {
6090 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6091 ssl->in_msg[0] ) );
6092 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6093 }
6094
6095#if defined(MBEDTLS_SSL_PROTO_DTLS)
6096 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6097 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6098 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6099 {
6100 if( ssl->handshake == NULL )
6101 {
6102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6103 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6104 }
6105
6106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6107 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6108 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006109#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006110 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006113 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006114 if( ssl->in_msglen != 2 )
6115 {
6116 /* Note: Standard allows for more than one 2 byte alert
6117 to be packed in a single message, but Mbed TLS doesn't
6118 currently support this. */
6119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6120 ssl->in_msglen ) );
6121 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6122 }
6123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006125 ssl->in_msg[0], ssl->in_msg[1] ) );
6126
6127 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006128 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006131 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006133 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006135 }
6136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6138 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6141 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006142 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006143
6144#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6145 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6146 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6147 {
Hanno Becker90333da2017-10-10 11:27:13 +01006148 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006149 /* Will be handled when trying to parse ServerHello */
6150 return( 0 );
6151 }
6152#endif
6153
6154#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6155 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6156 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6157 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6158 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6159 {
6160 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6161 /* Will be handled in mbedtls_ssl_parse_certificate() */
6162 return( 0 );
6163 }
6164#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6165
6166 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006167 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006168 }
6169
Hanno Beckerc76c6192017-06-06 10:03:17 +01006170#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006171 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006172 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006173 /* Drop unexpected ApplicationData records,
6174 * except at the beginning of renegotiations */
6175 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6176 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6177#if defined(MBEDTLS_SSL_RENEGOTIATION)
6178 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6179 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006180#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006181 )
6182 {
6183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6184 return( MBEDTLS_ERR_SSL_NON_FATAL );
6185 }
6186
6187 if( ssl->handshake != NULL &&
6188 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6189 {
6190 ssl_handshake_wrapup_free_hs_transform( ssl );
6191 }
6192 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006193#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006194
Paul Bakker5121ce52009-01-03 21:22:43 +00006195 return( 0 );
6196}
6197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006198int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006199{
6200 int ret;
6201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006202 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6203 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6204 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006205 {
6206 return( ret );
6207 }
6208
6209 return( 0 );
6210}
6211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006212int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006213 unsigned char level,
6214 unsigned char message )
6215{
6216 int ret;
6217
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006218 if( ssl == NULL || ssl->conf == NULL )
6219 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006222 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006224 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006225 ssl->out_msglen = 2;
6226 ssl->out_msg[0] = level;
6227 ssl->out_msg[1] = message;
6228
Hanno Becker67bc7c32018-08-06 11:33:50 +01006229 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006232 return( ret );
6233 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006234 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006235
6236 return( 0 );
6237}
6238
Hanno Beckerb9d44792019-02-08 07:19:04 +00006239#if defined(MBEDTLS_X509_CRT_PARSE_C)
6240static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6241{
6242#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6243 if( session->peer_cert != NULL )
6244 {
6245 mbedtls_x509_crt_free( session->peer_cert );
6246 mbedtls_free( session->peer_cert );
6247 session->peer_cert = NULL;
6248 }
6249#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6250 if( session->peer_cert_digest != NULL )
6251 {
6252 /* Zeroization is not necessary. */
6253 mbedtls_free( session->peer_cert_digest );
6254 session->peer_cert_digest = NULL;
6255 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6256 session->peer_cert_digest_len = 0;
6257 }
6258#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6259}
6260#endif /* MBEDTLS_X509_CRT_PARSE_C */
6261
Paul Bakker5121ce52009-01-03 21:22:43 +00006262/*
6263 * Handshake functions
6264 */
Hanno Becker21489932019-02-05 13:20:55 +00006265#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006266/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006267int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006268{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006269 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6270 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006273
Hanno Becker7177a882019-02-05 13:36:46 +00006274 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006275 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006276 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006277 ssl->state++;
6278 return( 0 );
6279 }
6280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6282 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006283}
6284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006285int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006286{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006287 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6288 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006291
Hanno Becker7177a882019-02-05 13:36:46 +00006292 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006294 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006295 ssl->state++;
6296 return( 0 );
6297 }
6298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6300 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006301}
Gilles Peskinef9828522017-05-03 12:28:43 +02006302
Hanno Becker21489932019-02-05 13:20:55 +00006303#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006304/* Some certificate support -> implement write and parse */
6305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006307{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006309 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006310 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006311 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6312 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006315
Hanno Becker7177a882019-02-05 13:36:46 +00006316 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006319 ssl->state++;
6320 return( 0 );
6321 }
6322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006323#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006324 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006325 {
6326 if( ssl->client_auth == 0 )
6327 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006328 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006329 ssl->state++;
6330 return( 0 );
6331 }
6332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006333#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 /*
6335 * If using SSLv3 and got no cert, send an Alert message
6336 * (otherwise an empty Certificate message will be sent).
6337 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006338 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6339 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006340 {
6341 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006342 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6343 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6344 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 goto write_msg;
6348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351#endif /* MBEDTLS_SSL_CLI_C */
6352#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006353 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6358 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006359 }
6360 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006361#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006364
6365 /*
6366 * 0 . 0 handshake type
6367 * 1 . 3 handshake length
6368 * 4 . 6 length of all certs
6369 * 7 . 9 length of cert. 1
6370 * 10 . n-1 peer certificate
6371 * n . n+2 length of cert. 2
6372 * n+3 . ... upper level cert, etc.
6373 */
6374 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006375 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006376
Paul Bakker29087132010-03-21 21:03:34 +00006377 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 {
6379 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006380 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006381 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006383 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006384 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006385 }
6386
6387 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6388 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6389 ssl->out_msg[i + 2] = (unsigned char)( n );
6390
6391 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6392 i += n; crt = crt->next;
6393 }
6394
6395 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6396 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6397 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6398
6399 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006400 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6401 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006402
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006403#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006404write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006405#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006406
6407 ssl->state++;
6408
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006409 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006410 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006411 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 return( ret );
6413 }
6414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416
Paul Bakkered27a042013-04-18 22:46:23 +02006417 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006418}
6419
Hanno Becker84879e32019-01-31 07:44:03 +00006420#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006421
6422#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006423static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6424 unsigned char *crt_buf,
6425 size_t crt_buf_len )
6426{
6427 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6428
6429 if( peer_crt == NULL )
6430 return( -1 );
6431
6432 if( peer_crt->raw.len != crt_buf_len )
6433 return( -1 );
6434
Hanno Becker46f34d02019-02-08 14:00:04 +00006435 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006436}
Hanno Becker177475a2019-02-05 17:02:46 +00006437#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6438static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6439 unsigned char *crt_buf,
6440 size_t crt_buf_len )
6441{
6442 int ret;
6443 unsigned char const * const peer_cert_digest =
6444 ssl->session->peer_cert_digest;
6445 mbedtls_md_type_t const peer_cert_digest_type =
6446 ssl->session->peer_cert_digest_type;
6447 mbedtls_md_info_t const * const digest_info =
6448 mbedtls_md_info_from_type( peer_cert_digest_type );
6449 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6450 size_t digest_len;
6451
6452 if( peer_cert_digest == NULL || digest_info == NULL )
6453 return( -1 );
6454
6455 digest_len = mbedtls_md_get_size( digest_info );
6456 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6457 return( -1 );
6458
6459 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6460 if( ret != 0 )
6461 return( -1 );
6462
6463 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6464}
6465#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006466#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006467
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006468/*
6469 * Once the certificate message is read, parse it into a cert chain and
6470 * perform basic checks, but leave actual verification to the caller
6471 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006472static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6473 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006474{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006475 int ret;
6476#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6477 int crt_cnt=0;
6478#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006479 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006480 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006485 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6486 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006488 }
6489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6491 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006494 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6495 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006497 }
6498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006500
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006502 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006503 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006504 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006505
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006506 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006508 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006510 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6511 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006512 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006513 }
6514
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006515 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6516 i += 3;
6517
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006518 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006519 while( i < ssl->in_hslen )
6520 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006521 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006522 if ( i + 3 > ssl->in_hslen ) {
6523 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006524 mbedtls_ssl_send_alert_message( ssl,
6525 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6526 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006527 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6528 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006529 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6530 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006531 if( ssl->in_msg[i] != 0 )
6532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006534 mbedtls_ssl_send_alert_message( ssl,
6535 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6536 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006537 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006538 }
6539
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006540 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006541 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6542 | (unsigned int) ssl->in_msg[i + 2];
6543 i += 3;
6544
6545 if( n < 128 || i + n > ssl->in_hslen )
6546 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006547 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006548 mbedtls_ssl_send_alert_message( ssl,
6549 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6550 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006551 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006552 }
6553
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006554 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006555#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6556 if( crt_cnt++ == 0 &&
6557 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6558 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006559 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006560 /* During client-side renegotiation, check that the server's
6561 * end-CRTs hasn't changed compared to the initial handshake,
6562 * mitigating the triple handshake attack. On success, reuse
6563 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006564 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6565 if( ssl_check_peer_crt_unchanged( ssl,
6566 &ssl->in_msg[i],
6567 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006568 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6570 mbedtls_ssl_send_alert_message( ssl,
6571 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6572 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6573 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006574 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006575
6576 /* Now we can safely free the original chain. */
6577 ssl_clear_peer_cert( ssl->session );
6578 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006579#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6580
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006581 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006582#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006583 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006584#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006585 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006586 * it in-place from the input buffer instead of making a copy. */
6587 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6588#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006589 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006590 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006591 case 0: /*ok*/
6592 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6593 /* Ignore certificate with an unknown algorithm: maybe a
6594 prior certificate was already trusted. */
6595 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006596
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006597 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6598 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6599 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006600
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006601 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6602 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6603 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006604
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006605 default:
6606 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6607 crt_parse_der_failed:
6608 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6609 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6610 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006611 }
6612
6613 i += n;
6614 }
6615
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006616 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006617 return( 0 );
6618}
6619
Hanno Becker4a55f632019-02-05 12:49:06 +00006620#if defined(MBEDTLS_SSL_SRV_C)
6621static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6622{
6623 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6624 return( -1 );
6625
6626#if defined(MBEDTLS_SSL_PROTO_SSL3)
6627 /*
6628 * Check if the client sent an empty certificate
6629 */
6630 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6631 {
6632 if( ssl->in_msglen == 2 &&
6633 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6634 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6635 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6636 {
6637 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6638 return( 0 );
6639 }
6640
6641 return( -1 );
6642 }
6643#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6644
6645#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6646 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6647 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6648 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6649 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6650 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6651 {
6652 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6653 return( 0 );
6654 }
6655
6656 return( -1 );
6657#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6658 MBEDTLS_SSL_PROTO_TLS1_2 */
6659}
6660#endif /* MBEDTLS_SSL_SRV_C */
6661
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006662/* Check if a certificate message is expected.
6663 * Return either
6664 * - SSL_CERTIFICATE_EXPECTED, or
6665 * - SSL_CERTIFICATE_SKIP
6666 * indicating whether a Certificate message is expected or not.
6667 */
6668#define SSL_CERTIFICATE_EXPECTED 0
6669#define SSL_CERTIFICATE_SKIP 1
6670static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6671 int authmode )
6672{
6673 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006674 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006675
6676 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6677 return( SSL_CERTIFICATE_SKIP );
6678
6679#if defined(MBEDTLS_SSL_SRV_C)
6680 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6681 {
6682 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6683 return( SSL_CERTIFICATE_SKIP );
6684
6685 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6686 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006687 ssl->session_negotiate->verify_result =
6688 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6689 return( SSL_CERTIFICATE_SKIP );
6690 }
6691 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006692#else
6693 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006694#endif /* MBEDTLS_SSL_SRV_C */
6695
6696 return( SSL_CERTIFICATE_EXPECTED );
6697}
6698
Hanno Becker68636192019-02-05 14:36:34 +00006699static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6700 int authmode,
6701 mbedtls_x509_crt *chain,
6702 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006703{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006704 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006705 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006706 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006707 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006708
Hanno Becker8927c832019-04-03 12:52:50 +01006709 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6710 void *p_vrfy;
6711
Hanno Becker68636192019-02-05 14:36:34 +00006712 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6713 return( 0 );
6714
Hanno Becker8927c832019-04-03 12:52:50 +01006715 if( ssl->f_vrfy != NULL )
6716 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006718 f_vrfy = ssl->f_vrfy;
6719 p_vrfy = ssl->p_vrfy;
6720 }
6721 else
6722 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006723 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006724 f_vrfy = ssl->conf->f_vrfy;
6725 p_vrfy = ssl->conf->p_vrfy;
6726 }
6727
Hanno Becker68636192019-02-05 14:36:34 +00006728 /*
6729 * Main check: verify certificate
6730 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006731#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6732 if( ssl->conf->f_ca_cb != NULL )
6733 {
6734 ((void) rs_ctx);
6735 have_ca_chain = 1;
6736
6737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006738 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006739 chain,
6740 ssl->conf->f_ca_cb,
6741 ssl->conf->p_ca_cb,
6742 ssl->conf->cert_profile,
6743 ssl->hostname,
6744 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006745 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006746 }
6747 else
6748#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6749 {
6750 mbedtls_x509_crt *ca_chain;
6751 mbedtls_x509_crl *ca_crl;
6752
6753#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6754 if( ssl->handshake->sni_ca_chain != NULL )
6755 {
6756 ca_chain = ssl->handshake->sni_ca_chain;
6757 ca_crl = ssl->handshake->sni_ca_crl;
6758 }
6759 else
6760#endif
6761 {
6762 ca_chain = ssl->conf->ca_chain;
6763 ca_crl = ssl->conf->ca_crl;
6764 }
6765
6766 if( ca_chain != NULL )
6767 have_ca_chain = 1;
6768
6769 ret = mbedtls_x509_crt_verify_restartable(
6770 chain,
6771 ca_chain, ca_crl,
6772 ssl->conf->cert_profile,
6773 ssl->hostname,
6774 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006775 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006776 }
Hanno Becker68636192019-02-05 14:36:34 +00006777
6778 if( ret != 0 )
6779 {
6780 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6781 }
6782
6783#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6784 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6785 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6786#endif
6787
6788 /*
6789 * Secondary checks: always done, but change 'ret' only if it was 0
6790 */
6791
6792#if defined(MBEDTLS_ECP_C)
6793 {
6794 const mbedtls_pk_context *pk = &chain->pk;
6795
6796 /* If certificate uses an EC key, make sure the curve is OK */
6797 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6798 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6799 {
6800 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6801
6802 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6803 if( ret == 0 )
6804 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6805 }
6806 }
6807#endif /* MBEDTLS_ECP_C */
6808
6809 if( mbedtls_ssl_check_cert_usage( chain,
6810 ciphersuite_info,
6811 ! ssl->conf->endpoint,
6812 &ssl->session_negotiate->verify_result ) != 0 )
6813 {
6814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6815 if( ret == 0 )
6816 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6817 }
6818
6819 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6820 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6821 * with details encoded in the verification flags. All other kinds
6822 * of error codes, including those from the user provided f_vrfy
6823 * functions, are treated as fatal and lead to a failure of
6824 * ssl_parse_certificate even if verification was optional. */
6825 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6826 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6827 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6828 {
6829 ret = 0;
6830 }
6831
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006832 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006833 {
6834 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6835 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6836 }
6837
6838 if( ret != 0 )
6839 {
6840 uint8_t alert;
6841
6842 /* The certificate may have been rejected for several reasons.
6843 Pick one and send the corresponding alert. Which alert to send
6844 may be a subject of debate in some cases. */
6845 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6846 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6847 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6848 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6849 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6850 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6851 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6852 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6853 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6854 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6855 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6856 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6857 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6858 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6859 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6860 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6861 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6862 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6863 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6864 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6865 else
6866 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6867 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6868 alert );
6869 }
6870
6871#if defined(MBEDTLS_DEBUG_C)
6872 if( ssl->session_negotiate->verify_result != 0 )
6873 {
6874 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6875 ssl->session_negotiate->verify_result ) );
6876 }
6877 else
6878 {
6879 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6880 }
6881#endif /* MBEDTLS_DEBUG_C */
6882
6883 return( ret );
6884}
6885
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006886#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6887static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6888 unsigned char *start, size_t len )
6889{
6890 int ret;
6891 /* Remember digest of the peer's end-CRT. */
6892 ssl->session_negotiate->peer_cert_digest =
6893 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6894 if( ssl->session_negotiate->peer_cert_digest == NULL )
6895 {
6896 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6897 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6898 mbedtls_ssl_send_alert_message( ssl,
6899 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6900 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6901
6902 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6903 }
6904
6905 ret = mbedtls_md( mbedtls_md_info_from_type(
6906 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6907 start, len,
6908 ssl->session_negotiate->peer_cert_digest );
6909
6910 ssl->session_negotiate->peer_cert_digest_type =
6911 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6912 ssl->session_negotiate->peer_cert_digest_len =
6913 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6914
6915 return( ret );
6916}
6917
6918static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6919 unsigned char *start, size_t len )
6920{
6921 unsigned char *end = start + len;
6922 int ret;
6923
6924 /* Make a copy of the peer's raw public key. */
6925 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6926 ret = mbedtls_pk_parse_subpubkey( &start, end,
6927 &ssl->handshake->peer_pubkey );
6928 if( ret != 0 )
6929 {
6930 /* We should have parsed the public key before. */
6931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6932 }
6933
6934 return( 0 );
6935}
6936#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6937
Hanno Becker68636192019-02-05 14:36:34 +00006938int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6939{
6940 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006941 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006942#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6943 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6944 ? ssl->handshake->sni_authmode
6945 : ssl->conf->authmode;
6946#else
6947 const int authmode = ssl->conf->authmode;
6948#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006949 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006950 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006951
6952 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6953
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006954 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6955 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006956 {
6957 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006958 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006959 }
6960
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006961#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6962 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006963 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006964 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006965 chain = ssl->handshake->ecrs_peer_cert;
6966 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006967 goto crt_verify;
6968 }
6969#endif
6970
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006971 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006972 {
6973 /* mbedtls_ssl_read_record may have sent an alert already. We
6974 let it decide whether to alert. */
6975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006976 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006977 }
6978
Hanno Becker4a55f632019-02-05 12:49:06 +00006979#if defined(MBEDTLS_SSL_SRV_C)
6980 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6981 {
6982 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006983
6984 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006985 ret = 0;
6986 else
6987 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006988
Hanno Becker6bdfab22019-02-05 13:11:17 +00006989 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006990 }
6991#endif /* MBEDTLS_SSL_SRV_C */
6992
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006993 /* Clear existing peer CRT structure in case we tried to
6994 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006995 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006996
6997 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6998 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006999 {
7000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7001 sizeof( mbedtls_x509_crt ) ) );
7002 mbedtls_ssl_send_alert_message( ssl,
7003 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7004 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007005
Hanno Becker3dad3112019-02-05 17:19:52 +00007006 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7007 goto exit;
7008 }
7009 mbedtls_x509_crt_init( chain );
7010
7011 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007012 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007013 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007014
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007015#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7016 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007017 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007018
7019crt_verify:
7020 if( ssl->handshake->ecrs_enabled)
7021 rs_ctx = &ssl->handshake->ecrs_ctx;
7022#endif
7023
Hanno Becker68636192019-02-05 14:36:34 +00007024 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007025 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007026 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007027 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007028
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007029#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007030 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007031 unsigned char *crt_start, *pk_start;
7032 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007033
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007034 /* We parse the CRT chain without copying, so
7035 * these pointers point into the input buffer,
7036 * and are hence still valid after freeing the
7037 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007038
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007039 crt_start = chain->raw.p;
7040 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007041
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007042 pk_start = chain->pk_raw.p;
7043 pk_len = chain->pk_raw.len;
7044
7045 /* Free the CRT structures before computing
7046 * digest and copying the peer's public key. */
7047 mbedtls_x509_crt_free( chain );
7048 mbedtls_free( chain );
7049 chain = NULL;
7050
7051 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007052 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007053 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007054
7055 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7056 if( ret != 0 )
7057 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007058 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007059#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7060 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007061 ssl->session_negotiate->peer_cert = chain;
7062 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007063#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007066
Hanno Becker6bdfab22019-02-05 13:11:17 +00007067exit:
7068
Hanno Becker3dad3112019-02-05 17:19:52 +00007069 if( ret == 0 )
7070 ssl->state++;
7071
7072#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7073 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7074 {
7075 ssl->handshake->ecrs_peer_cert = chain;
7076 chain = NULL;
7077 }
7078#endif
7079
7080 if( chain != NULL )
7081 {
7082 mbedtls_x509_crt_free( chain );
7083 mbedtls_free( chain );
7084 }
7085
Paul Bakker5121ce52009-01-03 21:22:43 +00007086 return( ret );
7087}
Hanno Becker21489932019-02-05 13:20:55 +00007088#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007091{
7092 int ret;
7093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 ssl->out_msglen = 1;
7098 ssl->out_msg[0] = 1;
7099
Paul Bakker5121ce52009-01-03 21:22:43 +00007100 ssl->state++;
7101
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007102 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007104 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007105 return( ret );
7106 }
7107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007108 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007109
7110 return( 0 );
7111}
7112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007113int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007114{
7115 int ret;
7116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007118
Hanno Becker327c93b2018-08-15 13:56:18 +01007119 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007122 return( ret );
7123 }
7124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007128 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7129 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007130 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007131 }
7132
Hanno Beckere678eaa2018-08-21 14:57:46 +01007133 /* CCS records are only accepted if they have length 1 and content '1',
7134 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007135
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007136 /*
7137 * Switch to our negotiated transform and session parameters for inbound
7138 * data.
7139 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007140 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007141 ssl->transform_in = ssl->transform_negotiate;
7142 ssl->session_in = ssl->session_negotiate;
7143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007145 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007147#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007148 ssl_dtls_replay_reset( ssl );
7149#endif
7150
7151 /* Increment epoch */
7152 if( ++ssl->in_epoch == 0 )
7153 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007155 /* This is highly unlikely to happen for legitimate reasons, so
7156 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007157 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007158 }
7159 }
7160 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007161#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007162 memset( ssl->in_ctr, 0, 8 );
7163
Hanno Becker79594fd2019-05-08 09:38:41 +01007164 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7167 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007170 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007172 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7173 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007174 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007175 }
7176 }
7177#endif
7178
Paul Bakker5121ce52009-01-03 21:22:43 +00007179 ssl->state++;
7180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007182
7183 return( 0 );
7184}
7185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007186void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7187 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007188{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007189 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7192 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7193 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007194 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007195 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7198#if defined(MBEDTLS_SHA512_C)
7199 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007200 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7201 else
7202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007203#if defined(MBEDTLS_SHA256_C)
7204 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007205 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007206 else
7207#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007211 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007212 }
Paul Bakker380da532012-04-18 16:10:25 +00007213}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007216{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007217#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7218 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007219 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7220 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007222#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7223#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007224#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007225 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007226 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7227#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007228 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007229#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007230#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007231#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007233 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007234 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007235#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007236 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007237#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007238#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007240}
7241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007243 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007244{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007245#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7246 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007247 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7248 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007249#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007250#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7251#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007252#if defined(MBEDTLS_USE_PSA_CRYPTO)
7253 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7254#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007255 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007256#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007257#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007258#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007259#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007260 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007261#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007262 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007263#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007264#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007266}
7267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7269 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7270static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007271 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007272{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007273 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7274 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007275}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007276#endif
Paul Bakker380da532012-04-18 16:10:25 +00007277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007278#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7279#if defined(MBEDTLS_SHA256_C)
7280static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007281 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007282{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007283#if defined(MBEDTLS_USE_PSA_CRYPTO)
7284 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7285#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007286 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007287#endif
Paul Bakker380da532012-04-18 16:10:25 +00007288}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007289#endif
Paul Bakker380da532012-04-18 16:10:25 +00007290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007291#if defined(MBEDTLS_SHA512_C)
7292static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007293 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007294{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007295#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007296 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007297#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007298 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007299#endif
Paul Bakker380da532012-04-18 16:10:25 +00007300}
Paul Bakker769075d2012-11-24 11:26:46 +01007301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007305static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007306 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007307{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007308 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007309 mbedtls_md5_context md5;
7310 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007311
Paul Bakker5121ce52009-01-03 21:22:43 +00007312 unsigned char padbuf[48];
7313 unsigned char md5sum[16];
7314 unsigned char sha1sum[20];
7315
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007316 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007317 if( !session )
7318 session = ssl->session;
7319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007320 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007321
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007322 mbedtls_md5_init( &md5 );
7323 mbedtls_sha1_init( &sha1 );
7324
7325 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7326 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007327
7328 /*
7329 * SSLv3:
7330 * hash =
7331 * MD5( master + pad2 +
7332 * MD5( handshake + sender + master + pad1 ) )
7333 * + SHA1( master + pad2 +
7334 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007335 */
7336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007337#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007338 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7339 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007340#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007342#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007343 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7344 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007345#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007347 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007348 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
Paul Bakker1ef83d62012-04-11 12:09:53 +00007350 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007351
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007352 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7353 mbedtls_md5_update_ret( &md5, session->master, 48 );
7354 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7355 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007356
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007357 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7358 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7359 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7360 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Paul Bakker1ef83d62012-04-11 12:09:53 +00007362 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007364 mbedtls_md5_starts_ret( &md5 );
7365 mbedtls_md5_update_ret( &md5, session->master, 48 );
7366 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7367 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7368 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007370 mbedtls_sha1_starts_ret( &sha1 );
7371 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7372 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7373 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7374 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007376 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007377
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007378 mbedtls_md5_free( &md5 );
7379 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007380
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007381 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7382 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7383 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007386}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007390static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007391 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007392{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007393 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007394 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007395 mbedtls_md5_context md5;
7396 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007397 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007399 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007400 if( !session )
7401 session = ssl->session;
7402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007404
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007405 mbedtls_md5_init( &md5 );
7406 mbedtls_sha1_init( &sha1 );
7407
7408 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7409 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Paul Bakker1ef83d62012-04-11 12:09:53 +00007411 /*
7412 * TLSv1:
7413 * hash = PRF( master, finished_label,
7414 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7415 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007417#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007418 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7419 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007420#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007422#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007423 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7424 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007425#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007427 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007428 ? "client finished"
7429 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007431 mbedtls_md5_finish_ret( &md5, padbuf );
7432 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007433
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007434 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007435 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007437 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007439 mbedtls_md5_free( &md5 );
7440 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007441
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007442 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007447
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7449#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007450static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007452{
7453 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007454 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007455 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007456#if defined(MBEDTLS_USE_PSA_CRYPTO)
7457 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007458 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007459 psa_status_t status;
7460#else
7461 mbedtls_sha256_context sha256;
7462#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007464 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007465 if( !session )
7466 session = ssl->session;
7467
Andrzej Kurekeb342242019-01-29 09:14:33 -05007468 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7469 ? "client finished"
7470 : "server finished";
7471
7472#if defined(MBEDTLS_USE_PSA_CRYPTO)
7473 sha256_psa = psa_hash_operation_init();
7474
7475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7476
7477 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7478 if( status != PSA_SUCCESS )
7479 {
7480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7481 return;
7482 }
7483
7484 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7485 if( status != PSA_SUCCESS )
7486 {
7487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7488 return;
7489 }
7490 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7491#else
7492
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007493 mbedtls_sha256_init( &sha256 );
7494
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007496
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007497 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007498
7499 /*
7500 * TLSv1.2:
7501 * hash = PRF( master, finished_label,
7502 * Hash( handshake ) )[0.11]
7503 */
7504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007505#if !defined(MBEDTLS_SHA256_ALT)
7506 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007507 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007508#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007509
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007510 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007511 mbedtls_sha256_free( &sha256 );
7512#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007513
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007514 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007515 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007518
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007519 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007520
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007522}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007526static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007527 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007528{
7529 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007530 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007531 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007532#if defined(MBEDTLS_USE_PSA_CRYPTO)
7533 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007534 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007535 psa_status_t status;
7536#else
7537 mbedtls_sha512_context sha512;
7538#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007540 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007541 if( !session )
7542 session = ssl->session;
7543
Andrzej Kurekeb342242019-01-29 09:14:33 -05007544 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7545 ? "client finished"
7546 : "server finished";
7547
7548#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007549 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007550
7551 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7552
Andrzej Kurek972fba52019-01-30 03:29:12 -05007553 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007554 if( status != PSA_SUCCESS )
7555 {
7556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7557 return;
7558 }
7559
Andrzej Kurek972fba52019-01-30 03:29:12 -05007560 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007561 if( status != PSA_SUCCESS )
7562 {
7563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7564 return;
7565 }
7566 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7567#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007568 mbedtls_sha512_init( &sha512 );
7569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007570 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007571
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007572 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007573
7574 /*
7575 * TLSv1.2:
7576 * hash = PRF( master, finished_label,
7577 * Hash( handshake ) )[0.11]
7578 */
7579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007580#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007581 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7582 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007583#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007584
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007585 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007586 mbedtls_sha512_free( &sha512 );
7587#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007588
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007589 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007590 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007593
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007594 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007597}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598#endif /* MBEDTLS_SHA512_C */
7599#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007600
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007602{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007604
7605 /*
7606 * Free our handshake params
7607 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007608 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007609 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007610 ssl->handshake = NULL;
7611
7612 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007613 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007614 */
7615 if( ssl->transform )
7616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 mbedtls_ssl_transform_free( ssl->transform );
7618 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007619 }
7620 ssl->transform = ssl->transform_negotiate;
7621 ssl->transform_negotiate = NULL;
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007624}
7625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007627{
7628 int resume = ssl->handshake->resume;
7629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632#if defined(MBEDTLS_SSL_RENEGOTIATION)
7633 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007635 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007636 ssl->renego_records_seen = 0;
7637 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007638#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007639
7640 /*
7641 * Free the previous session and switch in the current one
7642 */
Paul Bakker0a597072012-09-25 21:55:46 +00007643 if( ssl->session )
7644 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007646 /* RFC 7366 3.1: keep the EtM state */
7647 ssl->session_negotiate->encrypt_then_mac =
7648 ssl->session->encrypt_then_mac;
7649#endif
7650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651 mbedtls_ssl_session_free( ssl->session );
7652 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007653 }
7654 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007655 ssl->session_negotiate = NULL;
7656
Paul Bakker0a597072012-09-25 21:55:46 +00007657 /*
7658 * Add cache entry
7659 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007660 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007661 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007662 resume == 0 )
7663 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007664 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007666 }
Paul Bakker0a597072012-09-25 21:55:46 +00007667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007668#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007669 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007670 ssl->handshake->flight != NULL )
7671 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007672 /* Cancel handshake timer */
7673 ssl_set_timer( ssl, 0 );
7674
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007675 /* Keep last flight around in case we need to resend it:
7676 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007677 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007678 }
7679 else
7680#endif
7681 ssl_handshake_wrapup_free_hs_transform( ssl );
7682
Paul Bakker48916f92012-09-16 19:57:18 +00007683 ssl->state++;
7684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007686}
7687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007688int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007690 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007693
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007694 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007695
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007696 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007697
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007698 /*
7699 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7700 * may define some other value. Currently (early 2016), no defined
7701 * ciphersuite does this (and this is unlikely to change as activity has
7702 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7703 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007707 ssl->verify_data_len = hash_len;
7708 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007709#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007710
Paul Bakker5121ce52009-01-03 21:22:43 +00007711 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007712 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7713 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007714
7715 /*
7716 * In case of session resuming, invert the client and server
7717 * ChangeCipherSpec messages order.
7718 */
Paul Bakker0a597072012-09-25 21:55:46 +00007719 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007720 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007722 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007724#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007726 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007727 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007728#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007729 }
7730 else
7731 ssl->state++;
7732
Paul Bakker48916f92012-09-16 19:57:18 +00007733 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007734 * Switch to our negotiated transform and session parameters for outbound
7735 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007736 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007740 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007741 {
7742 unsigned char i;
7743
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007744 /* Remember current epoch settings for resending */
7745 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007746 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007747
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007748 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007749 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007750
7751 /* Increment epoch */
7752 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007753 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007754 break;
7755
7756 /* The loop goes to its end iff the counter is wrapping */
7757 if( i == 0 )
7758 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007759 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7760 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007761 }
7762 }
7763 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007765 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007766
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007767 ssl->transform_out = ssl->transform_negotiate;
7768 ssl->session_out = ssl->session_negotiate;
7769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7771 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7776 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007777 }
7778 }
7779#endif
7780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007782 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007784#endif
7785
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007786 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007787 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007788 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007789 return( ret );
7790 }
7791
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007792#if defined(MBEDTLS_SSL_PROTO_DTLS)
7793 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7794 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7795 {
7796 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7797 return( ret );
7798 }
7799#endif
7800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007801 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007802
7803 return( 0 );
7804}
7805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007807#define SSL_MAX_HASH_LEN 36
7808#else
7809#define SSL_MAX_HASH_LEN 12
7810#endif
7811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007813{
Paul Bakker23986e52011-04-24 08:57:21 +00007814 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007815 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007816 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007820 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007821
Hanno Becker327c93b2018-08-15 13:56:18 +01007822 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007825 return( ret );
7826 }
7827
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007829 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007831 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7832 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007833 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007834 }
7835
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007836 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007837#if defined(MBEDTLS_SSL_PROTO_SSL3)
7838 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007839 hash_len = 36;
7840 else
7841#endif
7842 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007843
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7845 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007846 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007848 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7849 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007851 }
7852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007854 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007857 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7858 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007860 }
7861
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007862#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007863 ssl->verify_data_len = hash_len;
7864 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007865#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007866
Paul Bakker0a597072012-09-25 21:55:46 +00007867 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007868 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007870 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007872#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007874 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007876#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007877 }
7878 else
7879 ssl->state++;
7880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007884#endif
7885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007886 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007887
7888 return( 0 );
7889}
7890
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007892{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007894
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007895#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7896 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7897 mbedtls_md5_init( &handshake->fin_md5 );
7898 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007899 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7900 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007901#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7903#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007904#if defined(MBEDTLS_USE_PSA_CRYPTO)
7905 handshake->fin_sha256_psa = psa_hash_operation_init();
7906 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7907#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007908 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007909 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007910#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007911#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007912#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007913#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007914 handshake->fin_sha384_psa = psa_hash_operation_init();
7915 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007916#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007917 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007918 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007919#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007920#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007921#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007922
7923 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007924
7925#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7926 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7927 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7928#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#if defined(MBEDTLS_DHM_C)
7931 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007932#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007933#if defined(MBEDTLS_ECDH_C)
7934 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007935#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007936#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007937 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007938#if defined(MBEDTLS_SSL_CLI_C)
7939 handshake->ecjpake_cache = NULL;
7940 handshake->ecjpake_cache_len = 0;
7941#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007942#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007943
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007944#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007945 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007946#endif
7947
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007948#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7949 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7950#endif
Hanno Becker75173122019-02-06 16:18:31 +00007951
7952#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7953 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7954 mbedtls_pk_init( &handshake->peer_pubkey );
7955#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956}
7957
Hanno Beckera18d1322018-01-03 14:27:32 +00007958void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007959{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007962 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7963 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007964
Hanno Beckerd56ed242018-01-03 15:32:51 +00007965#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007966 mbedtls_md_init( &transform->md_ctx_enc );
7967 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007968#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969}
7970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007974}
7975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007977{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007979 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007982 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007983 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007984 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007985
7986 /*
7987 * Either the pointers are now NULL or cleared properly and can be freed.
7988 * Now allocate missing structures.
7989 */
7990 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007991 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007992 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007993 }
Paul Bakker48916f92012-09-16 19:57:18 +00007994
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007995 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007996 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007997 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007998 }
Paul Bakker48916f92012-09-16 19:57:18 +00007999
Paul Bakker82788fb2014-10-20 13:59:19 +02008000 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008001 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008002 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008003 }
Paul Bakker48916f92012-09-16 19:57:18 +00008004
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008006 if( ssl->handshake == NULL ||
8007 ssl->transform_negotiate == NULL ||
8008 ssl->session_negotiate == NULL )
8009 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008010 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008012 mbedtls_free( ssl->handshake );
8013 mbedtls_free( ssl->transform_negotiate );
8014 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008015
8016 ssl->handshake = NULL;
8017 ssl->transform_negotiate = NULL;
8018 ssl->session_negotiate = NULL;
8019
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008020 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008021 }
8022
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008023 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008024 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008025 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008026 ssl_handshake_params_init( ssl->handshake );
8027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008028#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008029 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8030 {
8031 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008032
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008033 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8034 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8035 else
8036 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008037
8038 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008039 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008040#endif
8041
Paul Bakker48916f92012-09-16 19:57:18 +00008042 return( 0 );
8043}
8044
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008045#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008046/* Dummy cookie callbacks for defaults */
8047static int ssl_cookie_write_dummy( void *ctx,
8048 unsigned char **p, unsigned char *end,
8049 const unsigned char *cli_id, size_t cli_id_len )
8050{
8051 ((void) ctx);
8052 ((void) p);
8053 ((void) end);
8054 ((void) cli_id);
8055 ((void) cli_id_len);
8056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008057 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008058}
8059
8060static int ssl_cookie_check_dummy( void *ctx,
8061 const unsigned char *cookie, size_t cookie_len,
8062 const unsigned char *cli_id, size_t cli_id_len )
8063{
8064 ((void) ctx);
8065 ((void) cookie);
8066 ((void) cookie_len);
8067 ((void) cli_id);
8068 ((void) cli_id_len);
8069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008070 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008071}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008072#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008073
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008074/* Once ssl->out_hdr as the address of the beginning of the
8075 * next outgoing record is set, deduce the other pointers.
8076 *
8077 * Note: For TLS, we save the implicit record sequence number
8078 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8079 * and the caller has to make sure there's space for this.
8080 */
8081
8082static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8083 mbedtls_ssl_transform *transform )
8084{
8085#if defined(MBEDTLS_SSL_PROTO_DTLS)
8086 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8087 {
8088 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008089#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008090 ssl->out_cid = ssl->out_ctr + 8;
8091 ssl->out_len = ssl->out_cid;
8092 if( transform != NULL )
8093 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008094#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008095 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008096#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008097 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008098 }
8099 else
8100#endif
8101 {
8102 ssl->out_ctr = ssl->out_hdr - 8;
8103 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008104#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008105 ssl->out_cid = ssl->out_len;
8106#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008107 ssl->out_iv = ssl->out_hdr + 5;
8108 }
8109
8110 /* Adjust out_msg to make space for explicit IV, if used. */
8111 if( transform != NULL &&
8112 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8113 {
8114 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8115 }
8116 else
8117 ssl->out_msg = ssl->out_iv;
8118}
8119
8120/* Once ssl->in_hdr as the address of the beginning of the
8121 * next incoming record is set, deduce the other pointers.
8122 *
8123 * Note: For TLS, we save the implicit record sequence number
8124 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8125 * and the caller has to make sure there's space for this.
8126 */
8127
Hanno Becker79594fd2019-05-08 09:38:41 +01008128static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008129{
Hanno Becker79594fd2019-05-08 09:38:41 +01008130 /* This function sets the pointers to match the case
8131 * of unprotected TLS/DTLS records, with both ssl->in_iv
8132 * and ssl->in_msg pointing to the beginning of the record
8133 * content.
8134 *
8135 * When decrypting a protected record, ssl->in_msg
8136 * will be shifted to point to the beginning of the
8137 * record plaintext.
8138 */
8139
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008140#if defined(MBEDTLS_SSL_PROTO_DTLS)
8141 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8142 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008143 /* This sets the header pointers to match records
8144 * without CID. When we receive a record containing
8145 * a CID, the fields are shifted accordingly in
8146 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008147 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008148#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008149 ssl->in_cid = ssl->in_ctr + 8;
8150 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008151#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008152 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008154 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008155 }
8156 else
8157#endif
8158 {
8159 ssl->in_ctr = ssl->in_hdr - 8;
8160 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008161#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008162 ssl->in_cid = ssl->in_len;
8163#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008164 ssl->in_iv = ssl->in_hdr + 5;
8165 }
8166
Hanno Becker79594fd2019-05-08 09:38:41 +01008167 /* This will be adjusted at record decryption time. */
8168 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008169}
8170
Paul Bakker5121ce52009-01-03 21:22:43 +00008171/*
8172 * Initialize an SSL context
8173 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008174void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8175{
8176 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8177}
8178
8179/*
8180 * Setup an SSL context
8181 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008182
8183static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8184{
8185 /* Set the incoming and outgoing record pointers. */
8186#if defined(MBEDTLS_SSL_PROTO_DTLS)
8187 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8188 {
8189 ssl->out_hdr = ssl->out_buf;
8190 ssl->in_hdr = ssl->in_buf;
8191 }
8192 else
8193#endif /* MBEDTLS_SSL_PROTO_DTLS */
8194 {
8195 ssl->out_hdr = ssl->out_buf + 8;
8196 ssl->in_hdr = ssl->in_buf + 8;
8197 }
8198
8199 /* Derive other internal pointers. */
8200 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008201 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008202}
8203
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008204int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008205 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008206{
Paul Bakker48916f92012-09-16 19:57:18 +00008207 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008208
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008209 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008210
8211 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008212 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008213 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008214
8215 /* Set to NULL in case of an error condition */
8216 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008217
Angus Grattond8213d02016-05-25 20:56:48 +10008218 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8219 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008220 {
Angus Grattond8213d02016-05-25 20:56:48 +10008221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008222 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008223 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008224 }
8225
8226 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8227 if( ssl->out_buf == NULL )
8228 {
8229 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008230 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008231 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008232 }
8233
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008234 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008235
Paul Bakker48916f92012-09-16 19:57:18 +00008236 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008237 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008238
8239 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008240
8241error:
8242 mbedtls_free( ssl->in_buf );
8243 mbedtls_free( ssl->out_buf );
8244
8245 ssl->conf = NULL;
8246
8247 ssl->in_buf = NULL;
8248 ssl->out_buf = NULL;
8249
8250 ssl->in_hdr = NULL;
8251 ssl->in_ctr = NULL;
8252 ssl->in_len = NULL;
8253 ssl->in_iv = NULL;
8254 ssl->in_msg = NULL;
8255
8256 ssl->out_hdr = NULL;
8257 ssl->out_ctr = NULL;
8258 ssl->out_len = NULL;
8259 ssl->out_iv = NULL;
8260 ssl->out_msg = NULL;
8261
k-stachowiak9f7798e2018-07-31 16:52:32 +02008262 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008263}
8264
8265/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008266 * Reset an initialized and used SSL context for re-use while retaining
8267 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008268 *
8269 * If partial is non-zero, keep data in the input buffer and client ID.
8270 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008271 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008272static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008273{
Paul Bakker48916f92012-09-16 19:57:18 +00008274 int ret;
8275
Hanno Becker7e772132018-08-10 12:38:21 +01008276#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8277 !defined(MBEDTLS_SSL_SRV_C)
8278 ((void) partial);
8279#endif
8280
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008281 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008282
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008283 /* Cancel any possibly running timer */
8284 ssl_set_timer( ssl, 0 );
8285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008286#if defined(MBEDTLS_SSL_RENEGOTIATION)
8287 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008288 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008289
8290 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8292 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008293#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008294 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008295
Paul Bakker7eb013f2011-10-06 12:37:39 +00008296 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008297 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008298
8299 ssl->in_msgtype = 0;
8300 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008302 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008303 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008304#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008305#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008306 ssl_dtls_replay_reset( ssl );
8307#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008308
8309 ssl->in_hslen = 0;
8310 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008311
8312 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008313
8314 ssl->out_msgtype = 0;
8315 ssl->out_msglen = 0;
8316 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008317#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8318 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008319 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008320#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008321
Hanno Becker19859472018-08-06 09:40:20 +01008322 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8323
Paul Bakker48916f92012-09-16 19:57:18 +00008324 ssl->transform_in = NULL;
8325 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008326
Hanno Becker78640902018-08-13 16:35:15 +01008327 ssl->session_in = NULL;
8328 ssl->session_out = NULL;
8329
Angus Grattond8213d02016-05-25 20:56:48 +10008330 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008331
8332#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008333 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008334#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8335 {
8336 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008337 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008338 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8341 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8344 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008346 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8347 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008348 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008349 }
8350#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008351
Paul Bakker48916f92012-09-16 19:57:18 +00008352 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008354 mbedtls_ssl_transform_free( ssl->transform );
8355 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008356 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008357 }
Paul Bakker48916f92012-09-16 19:57:18 +00008358
Paul Bakkerc0463502013-02-14 11:19:38 +01008359 if( ssl->session )
8360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008361 mbedtls_ssl_session_free( ssl->session );
8362 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008363 ssl->session = NULL;
8364 }
8365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008366#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008367 ssl->alpn_chosen = NULL;
8368#endif
8369
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008370#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008371#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008372 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008373#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008374 {
8375 mbedtls_free( ssl->cli_id );
8376 ssl->cli_id = NULL;
8377 ssl->cli_id_len = 0;
8378 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008379#endif
8380
Paul Bakker48916f92012-09-16 19:57:18 +00008381 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8382 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008383
8384 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008385}
8386
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008387/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008388 * Reset an initialized and used SSL context for re-use while retaining
8389 * all application-set variables, function pointers and data.
8390 */
8391int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8392{
8393 return( ssl_session_reset_int( ssl, 0 ) );
8394}
8395
8396/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008397 * SSL set accessors
8398 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008399void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008400{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008401 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008402}
8403
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008404void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008405{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008406 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008407}
8408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008409#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008410void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008411{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008412 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008413}
8414#endif
8415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008416#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008417void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008418{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008419 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008420}
8421#endif
8422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008424
Hanno Becker1841b0a2018-08-24 11:13:57 +01008425void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8426 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008427{
8428 ssl->disable_datagram_packing = !allow_packing;
8429}
8430
8431void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8432 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008433{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008434 conf->hs_timeout_min = min;
8435 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008436}
8437#endif
8438
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008439void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008440{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008441 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008442}
8443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008444#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008445void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008446 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008447 void *p_vrfy )
8448{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008449 conf->f_vrfy = f_vrfy;
8450 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008451}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008452#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008453
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008454void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008455 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008456 void *p_rng )
8457{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008458 conf->f_rng = f_rng;
8459 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008460}
8461
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008462void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008463 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008464 void *p_dbg )
8465{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008466 conf->f_dbg = f_dbg;
8467 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008468}
8469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008470void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008471 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008472 mbedtls_ssl_send_t *f_send,
8473 mbedtls_ssl_recv_t *f_recv,
8474 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008475{
8476 ssl->p_bio = p_bio;
8477 ssl->f_send = f_send;
8478 ssl->f_recv = f_recv;
8479 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008480}
8481
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008482#if defined(MBEDTLS_SSL_PROTO_DTLS)
8483void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8484{
8485 ssl->mtu = mtu;
8486}
8487#endif
8488
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008489void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008490{
8491 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008492}
8493
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008494void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8495 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008496 mbedtls_ssl_set_timer_t *f_set_timer,
8497 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008498{
8499 ssl->p_timer = p_timer;
8500 ssl->f_set_timer = f_set_timer;
8501 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008502
8503 /* Make sure we start with no timer running */
8504 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008505}
8506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008507#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008508void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008509 void *p_cache,
8510 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8511 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008512{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008513 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008514 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008515 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008519#if defined(MBEDTLS_SSL_CLI_C)
8520int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008521{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008522 int ret;
8523
8524 if( ssl == NULL ||
8525 session == NULL ||
8526 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008527 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008528 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008530 }
8531
Hanno Becker52055ae2019-02-06 14:30:46 +00008532 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8533 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008534 return( ret );
8535
Paul Bakker0a597072012-09-25 21:55:46 +00008536 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008537
8538 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008539}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008540#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008541
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008542void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008543 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008545 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8546 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8547 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8548 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008549}
8550
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008551void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008552 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008553 int major, int minor )
8554{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008556 return;
8557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008559 return;
8560
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008561 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008562}
8563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008564#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008565void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008566 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008567{
8568 conf->cert_profile = profile;
8569}
8570
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008571/* Append a new keycert entry to a (possibly empty) list */
8572static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8573 mbedtls_x509_crt *cert,
8574 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008575{
niisato8ee24222018-06-25 19:05:48 +09008576 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008577
niisato8ee24222018-06-25 19:05:48 +09008578 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8579 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008580 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008581
niisato8ee24222018-06-25 19:05:48 +09008582 new_cert->cert = cert;
8583 new_cert->key = key;
8584 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008585
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008586 /* Update head is the list was null, else add to the end */
8587 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008588 {
niisato8ee24222018-06-25 19:05:48 +09008589 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008590 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008591 else
8592 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008593 mbedtls_ssl_key_cert *cur = *head;
8594 while( cur->next != NULL )
8595 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008596 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008597 }
8598
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008599 return( 0 );
8600}
8601
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008602int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008603 mbedtls_x509_crt *own_cert,
8604 mbedtls_pk_context *pk_key )
8605{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008606 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008607}
8608
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008609void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008610 mbedtls_x509_crt *ca_chain,
8611 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008612{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008613 conf->ca_chain = ca_chain;
8614 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008615
8616#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8617 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8618 * cannot be used together. */
8619 conf->f_ca_cb = NULL;
8620 conf->p_ca_cb = NULL;
8621#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008622}
Hanno Becker5adaad92019-03-27 16:54:37 +00008623
8624#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8625void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008626 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008627 void *p_ca_cb )
8628{
8629 conf->f_ca_cb = f_ca_cb;
8630 conf->p_ca_cb = p_ca_cb;
8631
8632 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8633 * cannot be used together. */
8634 conf->ca_chain = NULL;
8635 conf->ca_crl = NULL;
8636}
8637#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008638#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008639
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008640#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8641int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8642 mbedtls_x509_crt *own_cert,
8643 mbedtls_pk_context *pk_key )
8644{
8645 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8646 own_cert, pk_key ) );
8647}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008648
8649void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8650 mbedtls_x509_crt *ca_chain,
8651 mbedtls_x509_crl *ca_crl )
8652{
8653 ssl->handshake->sni_ca_chain = ca_chain;
8654 ssl->handshake->sni_ca_crl = ca_crl;
8655}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008656
8657void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8658 int authmode )
8659{
8660 ssl->handshake->sni_authmode = authmode;
8661}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008662#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8663
Hanno Becker8927c832019-04-03 12:52:50 +01008664#if defined(MBEDTLS_X509_CRT_PARSE_C)
8665void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8666 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8667 void *p_vrfy )
8668{
8669 ssl->f_vrfy = f_vrfy;
8670 ssl->p_vrfy = p_vrfy;
8671}
8672#endif
8673
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008674#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008675/*
8676 * Set EC J-PAKE password for current handshake
8677 */
8678int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8679 const unsigned char *pw,
8680 size_t pw_len )
8681{
8682 mbedtls_ecjpake_role role;
8683
Janos Follath8eb64132016-06-03 15:40:57 +01008684 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008685 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8686
8687 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8688 role = MBEDTLS_ECJPAKE_SERVER;
8689 else
8690 role = MBEDTLS_ECJPAKE_CLIENT;
8691
8692 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8693 role,
8694 MBEDTLS_MD_SHA256,
8695 MBEDTLS_ECP_DP_SECP256R1,
8696 pw, pw_len ) );
8697}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008698#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008700#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008701
8702static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8703{
8704 /* Remove reference to existing PSK, if any. */
8705#if defined(MBEDTLS_USE_PSA_CRYPTO)
8706 if( conf->psk_opaque != 0 )
8707 {
8708 /* The maintenance of the PSK key slot is the
8709 * user's responsibility. */
8710 conf->psk_opaque = 0;
8711 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008712 /* This and the following branch should never
8713 * be taken simultaenously as we maintain the
8714 * invariant that raw and opaque PSKs are never
8715 * configured simultaneously. As a safeguard,
8716 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008717#endif /* MBEDTLS_USE_PSA_CRYPTO */
8718 if( conf->psk != NULL )
8719 {
8720 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8721
8722 mbedtls_free( conf->psk );
8723 conf->psk = NULL;
8724 conf->psk_len = 0;
8725 }
8726
8727 /* Remove reference to PSK identity, if any. */
8728 if( conf->psk_identity != NULL )
8729 {
8730 mbedtls_free( conf->psk_identity );
8731 conf->psk_identity = NULL;
8732 conf->psk_identity_len = 0;
8733 }
8734}
8735
Hanno Becker7390c712018-11-15 13:33:04 +00008736/* This function assumes that PSK identity in the SSL config is unset.
8737 * It checks that the provided identity is well-formed and attempts
8738 * to make a copy of it in the SSL config.
8739 * On failure, the PSK identity in the config remains unset. */
8740static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8741 unsigned char const *psk_identity,
8742 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008743{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008744 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008745 if( psk_identity == NULL ||
8746 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008747 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008748 {
8749 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8750 }
8751
Hanno Becker7390c712018-11-15 13:33:04 +00008752 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8753 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008754 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008755
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008756 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008757 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008758
8759 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008760}
8761
Hanno Becker7390c712018-11-15 13:33:04 +00008762int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8763 const unsigned char *psk, size_t psk_len,
8764 const unsigned char *psk_identity, size_t psk_identity_len )
8765{
8766 int ret;
8767 /* Remove opaque/raw PSK + PSK Identity */
8768 ssl_conf_remove_psk( conf );
8769
8770 /* Check and set raw PSK */
8771 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8772 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8773 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8774 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8775 conf->psk_len = psk_len;
8776 memcpy( conf->psk, psk, conf->psk_len );
8777
8778 /* Check and set PSK Identity */
8779 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8780 if( ret != 0 )
8781 ssl_conf_remove_psk( conf );
8782
8783 return( ret );
8784}
8785
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008786static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8787{
8788#if defined(MBEDTLS_USE_PSA_CRYPTO)
8789 if( ssl->handshake->psk_opaque != 0 )
8790 {
8791 ssl->handshake->psk_opaque = 0;
8792 }
8793 else
8794#endif /* MBEDTLS_USE_PSA_CRYPTO */
8795 if( ssl->handshake->psk != NULL )
8796 {
8797 mbedtls_platform_zeroize( ssl->handshake->psk,
8798 ssl->handshake->psk_len );
8799 mbedtls_free( ssl->handshake->psk );
8800 ssl->handshake->psk_len = 0;
8801 }
8802}
8803
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008804int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8805 const unsigned char *psk, size_t psk_len )
8806{
8807 if( psk == NULL || ssl->handshake == NULL )
8808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8809
8810 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8811 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8812
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008813 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008814
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008815 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008816 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008817
8818 ssl->handshake->psk_len = psk_len;
8819 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8820
8821 return( 0 );
8822}
8823
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008824#if defined(MBEDTLS_USE_PSA_CRYPTO)
8825int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008826 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008827 const unsigned char *psk_identity,
8828 size_t psk_identity_len )
8829{
Hanno Becker7390c712018-11-15 13:33:04 +00008830 int ret;
8831 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008832 ssl_conf_remove_psk( conf );
8833
Hanno Becker7390c712018-11-15 13:33:04 +00008834 /* Check and set opaque PSK */
8835 if( psk_slot == 0 )
8836 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008837 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008838
8839 /* Check and set PSK Identity */
8840 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8841 psk_identity_len );
8842 if( ret != 0 )
8843 ssl_conf_remove_psk( conf );
8844
8845 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008846}
8847
8848int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008849 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008850{
8851 if( psk_slot == 0 || ssl->handshake == NULL )
8852 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8853
8854 ssl_remove_psk( ssl );
8855 ssl->handshake->psk_opaque = psk_slot;
8856 return( 0 );
8857}
8858#endif /* MBEDTLS_USE_PSA_CRYPTO */
8859
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008860void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008861 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008862 size_t),
8863 void *p_psk )
8864{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008865 conf->f_psk = f_psk;
8866 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008867}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008869
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008870#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008871
8872#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008873int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008874{
8875 int ret;
8876
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008877 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8878 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8879 {
8880 mbedtls_mpi_free( &conf->dhm_P );
8881 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008882 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008883 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008884
8885 return( 0 );
8886}
Hanno Becker470a8c42017-10-04 15:28:46 +01008887#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008888
Hanno Beckera90658f2017-10-04 15:29:08 +01008889int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8890 const unsigned char *dhm_P, size_t P_len,
8891 const unsigned char *dhm_G, size_t G_len )
8892{
8893 int ret;
8894
8895 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8896 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8897 {
8898 mbedtls_mpi_free( &conf->dhm_P );
8899 mbedtls_mpi_free( &conf->dhm_G );
8900 return( ret );
8901 }
8902
8903 return( 0 );
8904}
Paul Bakker5121ce52009-01-03 21:22:43 +00008905
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008906int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008907{
8908 int ret;
8909
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008910 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8911 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8912 {
8913 mbedtls_mpi_free( &conf->dhm_P );
8914 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008915 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008916 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008917
8918 return( 0 );
8919}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008920#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008921
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008922#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8923/*
8924 * Set the minimum length for Diffie-Hellman parameters
8925 */
8926void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8927 unsigned int bitlen )
8928{
8929 conf->dhm_min_bitlen = bitlen;
8930}
8931#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8932
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008933#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008934/*
8935 * Set allowed/preferred hashes for handshake signatures
8936 */
8937void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8938 const int *hashes )
8939{
8940 conf->sig_hashes = hashes;
8941}
Hanno Becker947194e2017-04-07 13:25:49 +01008942#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008943
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008944#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008945/*
8946 * Set the allowed elliptic curves
8947 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008948void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008949 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008950{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008951 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008952}
Hanno Becker947194e2017-04-07 13:25:49 +01008953#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008954
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008955#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008956int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008957{
Hanno Becker947194e2017-04-07 13:25:49 +01008958 /* Initialize to suppress unnecessary compiler warning */
8959 size_t hostname_len = 0;
8960
8961 /* Check if new hostname is valid before
8962 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008963 if( hostname != NULL )
8964 {
8965 hostname_len = strlen( hostname );
8966
8967 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8969 }
8970
8971 /* Now it's clear that we will overwrite the old hostname,
8972 * so we can free it safely */
8973
8974 if( ssl->hostname != NULL )
8975 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008976 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008977 mbedtls_free( ssl->hostname );
8978 }
8979
8980 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008981
Paul Bakker5121ce52009-01-03 21:22:43 +00008982 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008983 {
8984 ssl->hostname = NULL;
8985 }
8986 else
8987 {
8988 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008989 if( ssl->hostname == NULL )
8990 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008991
Hanno Becker947194e2017-04-07 13:25:49 +01008992 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008993
Hanno Becker947194e2017-04-07 13:25:49 +01008994 ssl->hostname[hostname_len] = '\0';
8995 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008996
8997 return( 0 );
8998}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008999#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009000
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009001#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009002void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009003 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009004 const unsigned char *, size_t),
9005 void *p_sni )
9006{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009007 conf->f_sni = f_sni;
9008 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009009}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009012#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009013int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009014{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009015 size_t cur_len, tot_len;
9016 const char **p;
9017
9018 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009019 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9020 * MUST NOT be truncated."
9021 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009022 */
9023 tot_len = 0;
9024 for( p = protos; *p != NULL; p++ )
9025 {
9026 cur_len = strlen( *p );
9027 tot_len += cur_len;
9028
9029 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009030 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009031 }
9032
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009033 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009034
9035 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009036}
9037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009038const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009039{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009040 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009041}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009042#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009043
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009044void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009045{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009046 conf->max_major_ver = major;
9047 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009048}
9049
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009050void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009051{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009052 conf->min_major_ver = major;
9053 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009054}
9055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009056#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009057void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009058{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009059 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009060}
9061#endif
9062
Janos Follath088ce432017-04-10 12:42:31 +01009063#if defined(MBEDTLS_SSL_SRV_C)
9064void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9065 char cert_req_ca_list )
9066{
9067 conf->cert_req_ca_list = cert_req_ca_list;
9068}
9069#endif
9070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009071#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009072void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009073{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009074 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009075}
9076#endif
9077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009078#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009079void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009080{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009081 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009082}
9083#endif
9084
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009085#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009086void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009087{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009088 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009089}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009090#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009093int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009094{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009096 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009098 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009099 }
9100
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009101 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009102
9103 return( 0 );
9104}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009107#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009108void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009109{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009110 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009111}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009114#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009116{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009117 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009118}
9119#endif
9120
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009123 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009124}
9125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009126#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009127void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009128{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009129 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009130}
9131
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009132void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009133{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009134 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009135}
9136
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009137void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009138 const unsigned char period[8] )
9139{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009140 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009144#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009145#if defined(MBEDTLS_SSL_CLI_C)
9146void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009147{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009148 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009149}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009150#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009151
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009152#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009153void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9154 mbedtls_ssl_ticket_write_t *f_ticket_write,
9155 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9156 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009157{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009158 conf->f_ticket_write = f_ticket_write;
9159 conf->f_ticket_parse = f_ticket_parse;
9160 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009161}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009162#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009163#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009164
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009165#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9166void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9167 mbedtls_ssl_export_keys_t *f_export_keys,
9168 void *p_export_keys )
9169{
9170 conf->f_export_keys = f_export_keys;
9171 conf->p_export_keys = p_export_keys;
9172}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009173
9174void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9175 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9176 void *p_export_keys )
9177{
9178 conf->f_export_keys_ext = f_export_keys_ext;
9179 conf->p_export_keys = p_export_keys;
9180}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009181#endif
9182
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009183#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009184void mbedtls_ssl_conf_async_private_cb(
9185 mbedtls_ssl_config *conf,
9186 mbedtls_ssl_async_sign_t *f_async_sign,
9187 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9188 mbedtls_ssl_async_resume_t *f_async_resume,
9189 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009190 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009191{
9192 conf->f_async_sign_start = f_async_sign;
9193 conf->f_async_decrypt_start = f_async_decrypt;
9194 conf->f_async_resume = f_async_resume;
9195 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009196 conf->p_async_config_data = async_config_data;
9197}
9198
Gilles Peskine8f97af72018-04-26 11:46:10 +02009199void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9200{
9201 return( conf->p_async_config_data );
9202}
9203
Gilles Peskine1febfef2018-04-30 11:54:39 +02009204void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009205{
9206 if( ssl->handshake == NULL )
9207 return( NULL );
9208 else
9209 return( ssl->handshake->user_async_ctx );
9210}
9211
Gilles Peskine1febfef2018-04-30 11:54:39 +02009212void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009213 void *ctx )
9214{
9215 if( ssl->handshake != NULL )
9216 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009217}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009218#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009219
Paul Bakker5121ce52009-01-03 21:22:43 +00009220/*
9221 * SSL get accessors
9222 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009223size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009224{
9225 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9226}
9227
Hanno Becker8b170a02017-10-10 11:51:19 +01009228int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9229{
9230 /*
9231 * Case A: We're currently holding back
9232 * a message for further processing.
9233 */
9234
9235 if( ssl->keep_current_message == 1 )
9236 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009237 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009238 return( 1 );
9239 }
9240
9241 /*
9242 * Case B: Further records are pending in the current datagram.
9243 */
9244
9245#if defined(MBEDTLS_SSL_PROTO_DTLS)
9246 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9247 ssl->in_left > ssl->next_record_offset )
9248 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009249 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009250 return( 1 );
9251 }
9252#endif /* MBEDTLS_SSL_PROTO_DTLS */
9253
9254 /*
9255 * Case C: A handshake message is being processed.
9256 */
9257
Hanno Becker8b170a02017-10-10 11:51:19 +01009258 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9259 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009260 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009261 return( 1 );
9262 }
9263
9264 /*
9265 * Case D: An application data message is being processed
9266 */
9267 if( ssl->in_offt != NULL )
9268 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009269 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009270 return( 1 );
9271 }
9272
9273 /*
9274 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009275 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009276 * we implement support for multiple alerts in single records.
9277 */
9278
9279 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9280 return( 0 );
9281}
9282
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009283uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009284{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009285 if( ssl->session != NULL )
9286 return( ssl->session->verify_result );
9287
9288 if( ssl->session_negotiate != NULL )
9289 return( ssl->session_negotiate->verify_result );
9290
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009291 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009292}
9293
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009294const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009295{
Paul Bakker926c8e42013-03-06 10:23:34 +01009296 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009297 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009300}
9301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009303{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009304#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009305 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009306 {
9307 switch( ssl->minor_ver )
9308 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009310 return( "DTLSv1.0" );
9311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009312 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009313 return( "DTLSv1.2" );
9314
9315 default:
9316 return( "unknown (DTLS)" );
9317 }
9318 }
9319#endif
9320
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321 switch( ssl->minor_ver )
9322 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009324 return( "SSLv3.0" );
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009327 return( "TLSv1.0" );
9328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009330 return( "TLSv1.1" );
9331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009332 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009333 return( "TLSv1.2" );
9334
Paul Bakker43ca69c2011-01-15 17:35:19 +00009335 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009336 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009337 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009338}
9339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009341{
Hanno Becker3136ede2018-08-17 15:28:19 +01009342 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009343 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009344 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009345
Hanno Becker5903de42019-05-03 14:46:38 +01009346 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9347
Hanno Becker78640902018-08-13 16:35:15 +01009348 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009349 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009351#if defined(MBEDTLS_ZLIB_SUPPORT)
9352 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9353 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009354#endif
9355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 case MBEDTLS_MODE_GCM:
9359 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009360 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009361 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009362 transform_expansion = transform->minlen;
9363 break;
9364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009366
9367 block_size = mbedtls_cipher_get_block_size(
9368 &transform->cipher_ctx_enc );
9369
Hanno Becker3136ede2018-08-17 15:28:19 +01009370 /* Expansion due to the addition of the MAC. */
9371 transform_expansion += transform->maclen;
9372
9373 /* Expansion due to the addition of CBC padding;
9374 * Theoretically up to 256 bytes, but we never use
9375 * more than the block size of the underlying cipher. */
9376 transform_expansion += block_size;
9377
9378 /* For TLS 1.1 or higher, an explicit IV is added
9379 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009380#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9381 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009382 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009383#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009384
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009385 break;
9386
9387 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009389 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009390 }
9391
Hanno Beckera0e20d02019-05-15 14:03:01 +01009392#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009393 if( transform->out_cid_len != 0 )
9394 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009395#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009396
Hanno Becker5903de42019-05-03 14:46:38 +01009397 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009398}
9399
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009400#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9401size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9402{
9403 size_t max_len;
9404
9405 /*
9406 * Assume mfl_code is correct since it was checked when set
9407 */
Angus Grattond8213d02016-05-25 20:56:48 +10009408 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009409
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009410 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009411 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009412 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009413 {
Angus Grattond8213d02016-05-25 20:56:48 +10009414 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009415 }
9416
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009417 /* During a handshake, use the value being negotiated */
9418 if( ssl->session_negotiate != NULL &&
9419 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9420 {
9421 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9422 }
9423
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009424 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009425}
9426#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9427
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009428#if defined(MBEDTLS_SSL_PROTO_DTLS)
9429static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9430{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009431 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9432 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9433 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9434 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9435 return ( 0 );
9436
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009437 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9438 return( ssl->mtu );
9439
9440 if( ssl->mtu == 0 )
9441 return( ssl->handshake->mtu );
9442
9443 return( ssl->mtu < ssl->handshake->mtu ?
9444 ssl->mtu : ssl->handshake->mtu );
9445}
9446#endif /* MBEDTLS_SSL_PROTO_DTLS */
9447
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009448int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9449{
9450 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9451
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009452#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9453 !defined(MBEDTLS_SSL_PROTO_DTLS)
9454 (void) ssl;
9455#endif
9456
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009457#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9458 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9459
9460 if( max_len > mfl )
9461 max_len = mfl;
9462#endif
9463
9464#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009465 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009466 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009467 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009468 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9469 const size_t overhead = (size_t) ret;
9470
9471 if( ret < 0 )
9472 return( ret );
9473
9474 if( mtu <= overhead )
9475 {
9476 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9477 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9478 }
9479
9480 if( max_len > mtu - overhead )
9481 max_len = mtu - overhead;
9482 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009483#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009484
Hanno Becker0defedb2018-08-10 12:35:02 +01009485#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9486 !defined(MBEDTLS_SSL_PROTO_DTLS)
9487 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009488#endif
9489
9490 return( (int) max_len );
9491}
9492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009493#if defined(MBEDTLS_X509_CRT_PARSE_C)
9494const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009495{
9496 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009497 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009498
Hanno Beckere6824572019-02-07 13:18:46 +00009499#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009500 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009501#else
9502 return( NULL );
9503#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009507#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009508int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9509 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009510{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009511 if( ssl == NULL ||
9512 dst == NULL ||
9513 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009514 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009516 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009517 }
9518
Hanno Becker52055ae2019-02-06 14:30:46 +00009519 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009521#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009522
Paul Bakker5121ce52009-01-03 21:22:43 +00009523/*
Paul Bakker1961b702013-01-25 14:49:24 +01009524 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009527{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009528 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009529
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009530 if( ssl == NULL || ssl->conf == NULL )
9531 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009534 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009536#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009538 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009540#endif
9541
Paul Bakker1961b702013-01-25 14:49:24 +01009542 return( ret );
9543}
9544
9545/*
9546 * Perform the SSL handshake
9547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009548int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009549{
9550 int ret = 0;
9551
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009552 if( ssl == NULL || ssl->conf == NULL )
9553 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009558 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009560
9561 if( ret != 0 )
9562 break;
9563 }
9564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009565 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009566
9567 return( ret );
9568}
9569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009570#if defined(MBEDTLS_SSL_RENEGOTIATION)
9571#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009572/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009573 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009576{
9577 int ret;
9578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009580
9581 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9583 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009584
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009585 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009586 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009588 return( ret );
9589 }
9590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009592
9593 return( 0 );
9594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009596
9597/*
9598 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 * - any side: calling mbedtls_ssl_renegotiate(),
9600 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9601 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009602 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009603 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009605 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009606static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009607{
9608 int ret;
9609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009610 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009611
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009612 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9613 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009614
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009615 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9616 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009618 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009620 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009621 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009622 ssl->handshake->out_msg_seq = 1;
9623 else
9624 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009625 }
9626#endif
9627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9629 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009633 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009634 return( ret );
9635 }
9636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009638
9639 return( 0 );
9640}
9641
9642/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009643 * Renegotiate current connection on client,
9644 * or request renegotiation on server
9645 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009647{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009648 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009649
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009650 if( ssl == NULL || ssl->conf == NULL )
9651 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009653#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009654 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009655 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009656 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9658 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009661
9662 /* Did we already try/start sending HelloRequest? */
9663 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009665
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009666 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009667 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009671 /*
9672 * On client, either start the renegotiation process or,
9673 * if already in progress, continue the handshake
9674 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9678 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009679
9680 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009683 return( ret );
9684 }
9685 }
9686 else
9687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009691 return( ret );
9692 }
9693 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009694#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009695
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009696 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009697}
9698
9699/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009700 * Check record counters and renegotiate if they're above the limit.
9701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009703{
Andres AG2196c7f2016-12-15 17:01:16 +00009704 size_t ep_len = ssl_ep_len( ssl );
9705 int in_ctr_cmp;
9706 int out_ctr_cmp;
9707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009708 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9709 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009710 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009711 {
9712 return( 0 );
9713 }
9714
Andres AG2196c7f2016-12-15 17:01:16 +00009715 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9716 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009717 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009718 ssl->conf->renego_period + ep_len, 8 - ep_len );
9719
9720 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009721 {
9722 return( 0 );
9723 }
9724
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009725 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009727}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009728#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009729
9730/*
9731 * Receive application data decrypted from the SSL layer
9732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009733int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009734{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009735 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009736 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009737
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009738 if( ssl == NULL || ssl->conf == NULL )
9739 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009744 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009746 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009747 return( ret );
9748
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009749 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009750 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009751 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009752 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009753 return( ret );
9754 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009755 }
9756#endif
9757
Hanno Becker4a810fb2017-05-24 16:27:30 +01009758 /*
9759 * Check if renegotiation is necessary and/or handshake is
9760 * in process. If yes, perform/continue, and fall through
9761 * if an unexpected packet is received while the client
9762 * is waiting for the ServerHello.
9763 *
9764 * (There is no equivalent to the last condition on
9765 * the server-side as it is not treated as within
9766 * a handshake while waiting for the ClientHello
9767 * after a renegotiation request.)
9768 */
9769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009770#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009771 ret = ssl_check_ctr_renegotiate( ssl );
9772 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9773 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009774 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009775 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009776 return( ret );
9777 }
9778#endif
9779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009783 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9784 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009785 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 return( ret );
9788 }
9789 }
9790
Hanno Beckere41158b2017-10-23 13:30:32 +01009791 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009792 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009793 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009794 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009795 if( ssl->f_get_timer != NULL &&
9796 ssl->f_get_timer( ssl->p_timer ) == -1 )
9797 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009798 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009799 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009800
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 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009803 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9804 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009805
Hanno Becker4a810fb2017-05-24 16:27:30 +01009806 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9807 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009808 }
9809
9810 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009811 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009812 {
9813 /*
9814 * OpenSSL sends empty messages to randomize the IV
9815 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009816 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009819 return( 0 );
9820
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009822 return( ret );
9823 }
9824 }
9825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009829
Hanno Becker4a810fb2017-05-24 16:27:30 +01009830 /*
9831 * - For client-side, expect SERVER_HELLO_REQUEST.
9832 * - For server-side, expect CLIENT_HELLO.
9833 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9834 */
9835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009837 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009839 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009842
9843 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009845 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009846 {
9847 continue;
9848 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009849#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009851 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009853
Hanno Becker4a810fb2017-05-24 16:27:30 +01009854#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009855 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009857 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009859
9860 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009862 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009863 {
9864 continue;
9865 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009867 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009868 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009869#endif /* MBEDTLS_SSL_SRV_C */
9870
Hanno Becker21df7f92017-10-17 11:03:26 +01009871#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009872 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009873 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9874 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9875 ssl->conf->allow_legacy_renegotiation ==
9876 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9877 {
9878 /*
9879 * Accept renegotiation request
9880 */
Paul Bakker48916f92012-09-16 19:57:18 +00009881
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009882 /* DTLS clients need to know renego is server-initiated */
9883#if defined(MBEDTLS_SSL_PROTO_DTLS)
9884 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9885 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9886 {
9887 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9888 }
9889#endif
9890 ret = ssl_start_renegotiation( ssl );
9891 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9892 ret != 0 )
9893 {
9894 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9895 return( ret );
9896 }
9897 }
9898 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009899#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009900 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009901 /*
9902 * Refuse renegotiation
9903 */
9904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009906
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009907#if defined(MBEDTLS_SSL_PROTO_SSL3)
9908 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009909 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009910 /* SSLv3 does not have a "no_renegotiation" warning, so
9911 we send a fatal alert and abort the connection. */
9912 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9913 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9914 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009915 }
9916 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009917#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9918#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9919 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9920 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009921 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009922 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9923 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9924 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009925 {
9926 return( ret );
9927 }
Paul Bakker48916f92012-09-16 19:57:18 +00009928 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009929 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9931 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9934 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009935 }
Paul Bakker48916f92012-09-16 19:57:18 +00009936 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009937
Hanno Becker90333da2017-10-10 11:27:13 +01009938 /* At this point, we don't know whether the renegotiation has been
9939 * completed or not. The cases to consider are the following:
9940 * 1) The renegotiation is complete. In this case, no new record
9941 * has been read yet.
9942 * 2) The renegotiation is incomplete because the client received
9943 * an application data record while awaiting the ServerHello.
9944 * 3) The renegotiation is incomplete because the client received
9945 * a non-handshake, non-application data message while awaiting
9946 * the ServerHello.
9947 * In each of these case, looping will be the proper action:
9948 * - For 1), the next iteration will read a new record and check
9949 * if it's application data.
9950 * - For 2), the loop condition isn't satisfied as application data
9951 * is present, hence continue is the same as break
9952 * - For 3), the loop condition is satisfied and read_record
9953 * will re-deliver the message that was held back by the client
9954 * when expecting the ServerHello.
9955 */
9956 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009957 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009958#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009960 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009961 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009962 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009963 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009966 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009968 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009969 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009970 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9974 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009977 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009978 }
9979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9983 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009984 }
9985
9986 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009987
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009988 /* We're going to return something now, cancel timer,
9989 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009990 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009991 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009992
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009993#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009994 /* If we requested renego but received AppData, resend HelloRequest.
9995 * Do it now, after setting in_offt, to avoid taking this branch
9996 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009998 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009999 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010000 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010001 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010003 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010004 return( ret );
10005 }
10006 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010007#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010008#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010009 }
10010
10011 n = ( len < ssl->in_msglen )
10012 ? len : ssl->in_msglen;
10013
10014 memcpy( buf, ssl->in_offt, n );
10015 ssl->in_msglen -= n;
10016
10017 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018 {
10019 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010020 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010021 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010022 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010023 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010024 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010025 /* more data available */
10026 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010027 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010029 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010030
Paul Bakker23986e52011-04-24 08:57:21 +000010031 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010032}
10033
10034/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010035 * Send application data to be encrypted by the SSL layer, taking care of max
10036 * fragment length and buffer size.
10037 *
10038 * According to RFC 5246 Section 6.2.1:
10039 *
10040 * Zero-length fragments of Application data MAY be sent as they are
10041 * potentially useful as a traffic analysis countermeasure.
10042 *
10043 * Therefore, it is possible that the input message length is 0 and the
10044 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010045 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010046static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010047 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010048{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010049 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10050 const size_t max_len = (size_t) ret;
10051
10052 if( ret < 0 )
10053 {
10054 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10055 return( ret );
10056 }
10057
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010058 if( len > max_len )
10059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010064 "maximum fragment length: %d > %d",
10065 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010067 }
10068 else
10069#endif
10070 len = max_len;
10071 }
Paul Bakker887bd502011-06-08 13:10:54 +000010072
Paul Bakker5121ce52009-01-03 21:22:43 +000010073 if( ssl->out_left != 0 )
10074 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010075 /*
10076 * The user has previously tried to send the data and
10077 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10078 * written. In this case, we expect the high-level write function
10079 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010082 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010083 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010084 return( ret );
10085 }
10086 }
Paul Bakker887bd502011-06-08 13:10:54 +000010087 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010088 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010089 /*
10090 * The user is trying to send a message the first time, so we need to
10091 * copy the data into the internal buffers and setup the data structure
10092 * to keep track of partial writes
10093 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010094 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010096 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010097
Hanno Becker67bc7c32018-08-06 11:33:50 +010010098 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010100 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010101 return( ret );
10102 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010103 }
10104
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010105 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010106}
10107
10108/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010109 * Write application data, doing 1/n-1 splitting if necessary.
10110 *
10111 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010112 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010113 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010115#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010116static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010117 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010118{
10119 int ret;
10120
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010121 if( ssl->conf->cbc_record_splitting ==
10122 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010123 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010124 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10125 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10126 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010127 {
10128 return( ssl_write_real( ssl, buf, len ) );
10129 }
10130
10131 if( ssl->split_done == 0 )
10132 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010133 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010134 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010135 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010136 }
10137
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010138 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10139 return( ret );
10140 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010141
10142 return( ret + 1 );
10143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010144#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010145
10146/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010147 * Write application data (public-facing wrapper)
10148 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010149int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010150{
10151 int ret;
10152
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010154
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010155 if( ssl == NULL || ssl->conf == NULL )
10156 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10157
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010158#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010159 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10160 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010161 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010162 return( ret );
10163 }
10164#endif
10165
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010166 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010167 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010168 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010169 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010171 return( ret );
10172 }
10173 }
10174
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010175#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010176 ret = ssl_write_split( ssl, buf, len );
10177#else
10178 ret = ssl_write_real( ssl, buf, len );
10179#endif
10180
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010181 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010182
10183 return( ret );
10184}
10185
10186/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010187 * Notify the peer that the connection is being closed
10188 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010189int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010190{
10191 int ret;
10192
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010193 if( ssl == NULL || ssl->conf == NULL )
10194 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010197
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010198 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010202 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10204 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10205 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010207 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010208 return( ret );
10209 }
10210 }
10211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010212 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010213
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010214 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010215}
10216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010217void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010218{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010219 if( transform == NULL )
10220 return;
10221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010222#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010223 deflateEnd( &transform->ctx_deflate );
10224 inflateEnd( &transform->ctx_inflate );
10225#endif
10226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010227 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10228 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010229
Hanno Beckerd56ed242018-01-03 15:32:51 +000010230#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010231 mbedtls_md_free( &transform->md_ctx_enc );
10232 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010233#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010234
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010235 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010236}
10237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238#if defined(MBEDTLS_X509_CRT_PARSE_C)
10239static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010240{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010241 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010242
10243 while( cur != NULL )
10244 {
10245 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010246 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010247 cur = next;
10248 }
10249}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010250#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010251
Hanno Becker0271f962018-08-16 13:23:47 +010010252#if defined(MBEDTLS_SSL_PROTO_DTLS)
10253
10254static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10255{
10256 unsigned offset;
10257 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10258
10259 if( hs == NULL )
10260 return;
10261
Hanno Becker283f5ef2018-08-24 09:34:47 +010010262 ssl_free_buffered_record( ssl );
10263
Hanno Becker0271f962018-08-16 13:23:47 +010010264 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010265 ssl_buffering_free_slot( ssl, offset );
10266}
10267
10268static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10269 uint8_t slot )
10270{
10271 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10272 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010273
10274 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10275 return;
10276
Hanno Beckere605b192018-08-21 15:59:07 +010010277 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010278 {
Hanno Beckere605b192018-08-21 15:59:07 +010010279 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010280 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010281 mbedtls_free( hs_buf->data );
10282 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010283 }
10284}
10285
10286#endif /* MBEDTLS_SSL_PROTO_DTLS */
10287
Gilles Peskine9b562d52018-04-25 20:32:43 +020010288void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010289{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010290 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10291
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010292 if( handshake == NULL )
10293 return;
10294
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010295#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10296 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10297 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010298 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010299 handshake->async_in_progress = 0;
10300 }
10301#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10302
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010303#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10304 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10305 mbedtls_md5_free( &handshake->fin_md5 );
10306 mbedtls_sha1_free( &handshake->fin_sha1 );
10307#endif
10308#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10309#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010310#if defined(MBEDTLS_USE_PSA_CRYPTO)
10311 psa_hash_abort( &handshake->fin_sha256_psa );
10312#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010313 mbedtls_sha256_free( &handshake->fin_sha256 );
10314#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010315#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010316#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010317#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010318 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010319#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010320 mbedtls_sha512_free( &handshake->fin_sha512 );
10321#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010322#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010323#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325#if defined(MBEDTLS_DHM_C)
10326 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010327#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328#if defined(MBEDTLS_ECDH_C)
10329 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010330#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010331#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010332 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010333#if defined(MBEDTLS_SSL_CLI_C)
10334 mbedtls_free( handshake->ecjpake_cache );
10335 handshake->ecjpake_cache = NULL;
10336 handshake->ecjpake_cache_len = 0;
10337#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010338#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010339
Janos Follath4ae5c292016-02-10 11:27:43 +000010340#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10341 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010342 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010343 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010344#endif
10345
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010346#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10347 if( handshake->psk != NULL )
10348 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010349 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010350 mbedtls_free( handshake->psk );
10351 }
10352#endif
10353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010354#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10355 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010356 /*
10357 * Free only the linked list wrapper, not the keys themselves
10358 * since the belong to the SNI callback
10359 */
10360 if( handshake->sni_key_cert != NULL )
10361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010362 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010363
10364 while( cur != NULL )
10365 {
10366 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010367 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010368 cur = next;
10369 }
10370 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010371#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010372
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010373#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010374 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010375 if( handshake->ecrs_peer_cert != NULL )
10376 {
10377 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10378 mbedtls_free( handshake->ecrs_peer_cert );
10379 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010380#endif
10381
Hanno Becker75173122019-02-06 16:18:31 +000010382#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10383 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10384 mbedtls_pk_free( &handshake->peer_pubkey );
10385#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010387#if defined(MBEDTLS_SSL_PROTO_DTLS)
10388 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010389 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010390 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010391#endif
10392
Hanno Becker4a63ed42019-01-08 11:39:35 +000010393#if defined(MBEDTLS_ECDH_C) && \
10394 defined(MBEDTLS_USE_PSA_CRYPTO)
10395 psa_destroy_key( handshake->ecdh_psa_privkey );
10396#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10397
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010398 mbedtls_platform_zeroize( handshake,
10399 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010400}
10401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010402void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010403{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010404 if( session == NULL )
10405 return;
10406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010407#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010408 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010409#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010410
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010411#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010412 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010413#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010414
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010415 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010416}
10417
Paul Bakker5121ce52009-01-03 21:22:43 +000010418/*
10419 * Free an SSL context
10420 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010421void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010422{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010423 if( ssl == NULL )
10424 return;
10425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010426 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010427
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010428 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010429 {
Angus Grattond8213d02016-05-25 20:56:48 +100010430 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010432 }
10433
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010434 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010435 {
Angus Grattond8213d02016-05-25 20:56:48 +100010436 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010438 }
10439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010440#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010441 if( ssl->compress_buf != NULL )
10442 {
Angus Grattond8213d02016-05-25 20:56:48 +100010443 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010445 }
10446#endif
10447
Paul Bakker48916f92012-09-16 19:57:18 +000010448 if( ssl->transform )
10449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010450 mbedtls_ssl_transform_free( ssl->transform );
10451 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010452 }
10453
10454 if( ssl->handshake )
10455 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010456 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10458 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010460 mbedtls_free( ssl->handshake );
10461 mbedtls_free( ssl->transform_negotiate );
10462 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010463 }
10464
Paul Bakkerc0463502013-02-14 11:19:38 +010010465 if( ssl->session )
10466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010467 mbedtls_ssl_session_free( ssl->session );
10468 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010469 }
10470
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010471#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010472 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010473 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010474 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010475 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010476 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010477#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10480 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10483 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010484 }
10485#endif
10486
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010487#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010489#endif
10490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010492
Paul Bakker86f04f42013-02-14 11:20:09 +010010493 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010494 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010495}
10496
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010497/*
10498 * Initialze mbedtls_ssl_config
10499 */
10500void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10501{
10502 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10503}
10504
Simon Butcherc97b6972015-12-27 23:48:17 +000010505#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010506static int ssl_preset_default_hashes[] = {
10507#if defined(MBEDTLS_SHA512_C)
10508 MBEDTLS_MD_SHA512,
10509 MBEDTLS_MD_SHA384,
10510#endif
10511#if defined(MBEDTLS_SHA256_C)
10512 MBEDTLS_MD_SHA256,
10513 MBEDTLS_MD_SHA224,
10514#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010515#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010516 MBEDTLS_MD_SHA1,
10517#endif
10518 MBEDTLS_MD_NONE
10519};
Simon Butcherc97b6972015-12-27 23:48:17 +000010520#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010521
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010522static int ssl_preset_suiteb_ciphersuites[] = {
10523 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10524 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10525 0
10526};
10527
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010528#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010529static int ssl_preset_suiteb_hashes[] = {
10530 MBEDTLS_MD_SHA256,
10531 MBEDTLS_MD_SHA384,
10532 MBEDTLS_MD_NONE
10533};
10534#endif
10535
10536#if defined(MBEDTLS_ECP_C)
10537static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10538 MBEDTLS_ECP_DP_SECP256R1,
10539 MBEDTLS_ECP_DP_SECP384R1,
10540 MBEDTLS_ECP_DP_NONE
10541};
10542#endif
10543
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010544/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010545 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010546 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010547int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010548 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010550#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010551 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010552#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010553
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010554 /* Use the functions here so that they are covered in tests,
10555 * but otherwise access member directly for efficiency */
10556 mbedtls_ssl_conf_endpoint( conf, endpoint );
10557 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010558
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010559 /*
10560 * Things that are common to all presets
10561 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010562#if defined(MBEDTLS_SSL_CLI_C)
10563 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10564 {
10565 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10567 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10568#endif
10569 }
10570#endif
10571
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010572#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010573 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010574#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010575
10576#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10577 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10578#endif
10579
10580#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10581 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10582#endif
10583
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010584#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10585 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10586#endif
10587
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010588#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010589 conf->f_cookie_write = ssl_cookie_write_dummy;
10590 conf->f_cookie_check = ssl_cookie_check_dummy;
10591#endif
10592
10593#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10594 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10595#endif
10596
Janos Follath088ce432017-04-10 12:42:31 +010010597#if defined(MBEDTLS_SSL_SRV_C)
10598 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10599#endif
10600
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010601#if defined(MBEDTLS_SSL_PROTO_DTLS)
10602 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10603 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10604#endif
10605
10606#if defined(MBEDTLS_SSL_RENEGOTIATION)
10607 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010608 memset( conf->renego_period, 0x00, 2 );
10609 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010610#endif
10611
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010612#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10613 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10614 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010615 const unsigned char dhm_p[] =
10616 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10617 const unsigned char dhm_g[] =
10618 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10619
Hanno Beckera90658f2017-10-04 15:29:08 +010010620 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10621 dhm_p, sizeof( dhm_p ),
10622 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010623 {
10624 return( ret );
10625 }
10626 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010627#endif
10628
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010629 /*
10630 * Preset-specific defaults
10631 */
10632 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010633 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010634 /*
10635 * NSA Suite B
10636 */
10637 case MBEDTLS_SSL_PRESET_SUITEB:
10638 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10639 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10640 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10641 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10642
10643 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10644 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10645 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10646 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10647 ssl_preset_suiteb_ciphersuites;
10648
10649#if defined(MBEDTLS_X509_CRT_PARSE_C)
10650 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010651#endif
10652
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010653#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010654 conf->sig_hashes = ssl_preset_suiteb_hashes;
10655#endif
10656
10657#if defined(MBEDTLS_ECP_C)
10658 conf->curve_list = ssl_preset_suiteb_curves;
10659#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010660 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010661
10662 /*
10663 * Default
10664 */
10665 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010666 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10667 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10668 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10669 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10670 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10671 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10672 MBEDTLS_SSL_MIN_MINOR_VERSION :
10673 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010674 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10675 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10676
10677#if defined(MBEDTLS_SSL_PROTO_DTLS)
10678 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10679 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10680#endif
10681
10682 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10684 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10685 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10686 mbedtls_ssl_list_ciphersuites();
10687
10688#if defined(MBEDTLS_X509_CRT_PARSE_C)
10689 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10690#endif
10691
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010692#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010693 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010694#endif
10695
10696#if defined(MBEDTLS_ECP_C)
10697 conf->curve_list = mbedtls_ecp_grp_id_list();
10698#endif
10699
10700#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10701 conf->dhm_min_bitlen = 1024;
10702#endif
10703 }
10704
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010705 return( 0 );
10706}
10707
10708/*
10709 * Free mbedtls_ssl_config
10710 */
10711void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10712{
10713#if defined(MBEDTLS_DHM_C)
10714 mbedtls_mpi_free( &conf->dhm_P );
10715 mbedtls_mpi_free( &conf->dhm_G );
10716#endif
10717
10718#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10719 if( conf->psk != NULL )
10720 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010721 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010723 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010724 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010725 }
10726
10727 if( conf->psk_identity != NULL )
10728 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010729 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010730 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010731 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010732 conf->psk_identity_len = 0;
10733 }
10734#endif
10735
10736#if defined(MBEDTLS_X509_CRT_PARSE_C)
10737 ssl_key_cert_free( conf->key_cert );
10738#endif
10739
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010740 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010741}
10742
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010743#if defined(MBEDTLS_PK_C) && \
10744 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010745/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010747 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010749{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010750#if defined(MBEDTLS_RSA_C)
10751 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10752 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010754#if defined(MBEDTLS_ECDSA_C)
10755 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10756 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010758 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010759}
10760
Hanno Becker7e5437a2017-04-28 17:15:26 +010010761unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10762{
10763 switch( type ) {
10764 case MBEDTLS_PK_RSA:
10765 return( MBEDTLS_SSL_SIG_RSA );
10766 case MBEDTLS_PK_ECDSA:
10767 case MBEDTLS_PK_ECKEY:
10768 return( MBEDTLS_SSL_SIG_ECDSA );
10769 default:
10770 return( MBEDTLS_SSL_SIG_ANON );
10771 }
10772}
10773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010774mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010775{
10776 switch( sig )
10777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010778#if defined(MBEDTLS_RSA_C)
10779 case MBEDTLS_SSL_SIG_RSA:
10780 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010781#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010782#if defined(MBEDTLS_ECDSA_C)
10783 case MBEDTLS_SSL_SIG_ECDSA:
10784 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010785#endif
10786 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010787 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010788 }
10789}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010790#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010791
Hanno Becker7e5437a2017-04-28 17:15:26 +010010792#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10793 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10794
10795/* Find an entry in a signature-hash set matching a given hash algorithm. */
10796mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10797 mbedtls_pk_type_t sig_alg )
10798{
10799 switch( sig_alg )
10800 {
10801 case MBEDTLS_PK_RSA:
10802 return( set->rsa );
10803 case MBEDTLS_PK_ECDSA:
10804 return( set->ecdsa );
10805 default:
10806 return( MBEDTLS_MD_NONE );
10807 }
10808}
10809
10810/* Add a signature-hash-pair to a signature-hash set */
10811void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10812 mbedtls_pk_type_t sig_alg,
10813 mbedtls_md_type_t md_alg )
10814{
10815 switch( sig_alg )
10816 {
10817 case MBEDTLS_PK_RSA:
10818 if( set->rsa == MBEDTLS_MD_NONE )
10819 set->rsa = md_alg;
10820 break;
10821
10822 case MBEDTLS_PK_ECDSA:
10823 if( set->ecdsa == MBEDTLS_MD_NONE )
10824 set->ecdsa = md_alg;
10825 break;
10826
10827 default:
10828 break;
10829 }
10830}
10831
10832/* Allow exactly one hash algorithm for each signature. */
10833void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10834 mbedtls_md_type_t md_alg )
10835{
10836 set->rsa = md_alg;
10837 set->ecdsa = md_alg;
10838}
10839
10840#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10841 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10842
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010843/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010844 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010845 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010846mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010847{
10848 switch( hash )
10849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010850#if defined(MBEDTLS_MD5_C)
10851 case MBEDTLS_SSL_HASH_MD5:
10852 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010853#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010854#if defined(MBEDTLS_SHA1_C)
10855 case MBEDTLS_SSL_HASH_SHA1:
10856 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010857#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010858#if defined(MBEDTLS_SHA256_C)
10859 case MBEDTLS_SSL_HASH_SHA224:
10860 return( MBEDTLS_MD_SHA224 );
10861 case MBEDTLS_SSL_HASH_SHA256:
10862 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010864#if defined(MBEDTLS_SHA512_C)
10865 case MBEDTLS_SSL_HASH_SHA384:
10866 return( MBEDTLS_MD_SHA384 );
10867 case MBEDTLS_SSL_HASH_SHA512:
10868 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010869#endif
10870 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010871 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010872 }
10873}
10874
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010875/*
10876 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10877 */
10878unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10879{
10880 switch( md )
10881 {
10882#if defined(MBEDTLS_MD5_C)
10883 case MBEDTLS_MD_MD5:
10884 return( MBEDTLS_SSL_HASH_MD5 );
10885#endif
10886#if defined(MBEDTLS_SHA1_C)
10887 case MBEDTLS_MD_SHA1:
10888 return( MBEDTLS_SSL_HASH_SHA1 );
10889#endif
10890#if defined(MBEDTLS_SHA256_C)
10891 case MBEDTLS_MD_SHA224:
10892 return( MBEDTLS_SSL_HASH_SHA224 );
10893 case MBEDTLS_MD_SHA256:
10894 return( MBEDTLS_SSL_HASH_SHA256 );
10895#endif
10896#if defined(MBEDTLS_SHA512_C)
10897 case MBEDTLS_MD_SHA384:
10898 return( MBEDTLS_SSL_HASH_SHA384 );
10899 case MBEDTLS_MD_SHA512:
10900 return( MBEDTLS_SSL_HASH_SHA512 );
10901#endif
10902 default:
10903 return( MBEDTLS_SSL_HASH_NONE );
10904 }
10905}
10906
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010907#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010908/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010909 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010910 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010912int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010913{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010914 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010915
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010916 if( ssl->conf->curve_list == NULL )
10917 return( -1 );
10918
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010919 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010920 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010921 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010922
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010923 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010924}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010925#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010926
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010927#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010928/*
10929 * Check if a hash proposed by the peer is in our list.
10930 * Return 0 if we're willing to use it, -1 otherwise.
10931 */
10932int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10933 mbedtls_md_type_t md )
10934{
10935 const int *cur;
10936
10937 if( ssl->conf->sig_hashes == NULL )
10938 return( -1 );
10939
10940 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10941 if( *cur == (int) md )
10942 return( 0 );
10943
10944 return( -1 );
10945}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010946#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010948#if defined(MBEDTLS_X509_CRT_PARSE_C)
10949int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10950 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010951 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010952 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010953{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010954 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010955#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010956 int usage = 0;
10957#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010959 const char *ext_oid;
10960 size_t ext_len;
10961#endif
10962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010963#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10964 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010965 ((void) cert);
10966 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010967 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010968#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010970#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10971 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010972 {
10973 /* Server part of the key exchange */
10974 switch( ciphersuite->key_exchange )
10975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010976 case MBEDTLS_KEY_EXCHANGE_RSA:
10977 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010978 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010979 break;
10980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010981 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10982 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10983 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10984 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010985 break;
10986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010987 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10988 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010989 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010990 break;
10991
10992 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010993 case MBEDTLS_KEY_EXCHANGE_NONE:
10994 case MBEDTLS_KEY_EXCHANGE_PSK:
10995 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10996 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010997 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010998 usage = 0;
10999 }
11000 }
11001 else
11002 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011003 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11004 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011005 }
11006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011007 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011008 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011009 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011010 ret = -1;
11011 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011012#else
11013 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011016#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11017 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011018 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11020 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011021 }
11022 else
11023 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011024 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11025 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011026 }
11027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011028 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011029 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011030 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011031 ret = -1;
11032 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011033#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011034
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011035 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011036}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011037#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011038
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011039/*
11040 * Convert version numbers to/from wire format
11041 * and, for DTLS, to/from TLS equivalent.
11042 *
11043 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011044 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011045 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11046 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11047 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011048void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011049 unsigned char ver[2] )
11050{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051#if defined(MBEDTLS_SSL_PROTO_DTLS)
11052 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011055 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11056
11057 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11058 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11059 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011060 else
11061#else
11062 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011063#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011064 {
11065 ver[0] = (unsigned char) major;
11066 ver[1] = (unsigned char) minor;
11067 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011068}
11069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011070void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011071 const unsigned char ver[2] )
11072{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011073#if defined(MBEDTLS_SSL_PROTO_DTLS)
11074 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011075 {
11076 *major = 255 - ver[0] + 2;
11077 *minor = 255 - ver[1] + 1;
11078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011079 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011080 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11081 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011082 else
11083#else
11084 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011085#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011086 {
11087 *major = ver[0];
11088 *minor = ver[1];
11089 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011090}
11091
Simon Butcher99000142016-10-13 17:21:01 +010011092int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11093{
11094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11095 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11096 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11097
11098 switch( md )
11099 {
11100#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11101#if defined(MBEDTLS_MD5_C)
11102 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011103 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011104#endif
11105#if defined(MBEDTLS_SHA1_C)
11106 case MBEDTLS_SSL_HASH_SHA1:
11107 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11108 break;
11109#endif
11110#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11111#if defined(MBEDTLS_SHA512_C)
11112 case MBEDTLS_SSL_HASH_SHA384:
11113 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11114 break;
11115#endif
11116#if defined(MBEDTLS_SHA256_C)
11117 case MBEDTLS_SSL_HASH_SHA256:
11118 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11119 break;
11120#endif
11121 default:
11122 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11123 }
11124
11125 return 0;
11126#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11127 (void) ssl;
11128 (void) md;
11129
11130 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11131#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11132}
11133
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011134#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11135 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11136int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11137 unsigned char *output,
11138 unsigned char *data, size_t data_len )
11139{
11140 int ret = 0;
11141 mbedtls_md5_context mbedtls_md5;
11142 mbedtls_sha1_context mbedtls_sha1;
11143
11144 mbedtls_md5_init( &mbedtls_md5 );
11145 mbedtls_sha1_init( &mbedtls_sha1 );
11146
11147 /*
11148 * digitally-signed struct {
11149 * opaque md5_hash[16];
11150 * opaque sha_hash[20];
11151 * };
11152 *
11153 * md5_hash
11154 * MD5(ClientHello.random + ServerHello.random
11155 * + ServerParams);
11156 * sha_hash
11157 * SHA(ClientHello.random + ServerHello.random
11158 * + ServerParams);
11159 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011160 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 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_starts_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_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011166 ssl->handshake->randbytes, 64 ) ) != 0 )
11167 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011169 goto exit;
11170 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_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_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011179 goto exit;
11180 }
11181
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011182 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011183 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011184 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011185 goto exit;
11186 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011187 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011188 ssl->handshake->randbytes, 64 ) ) != 0 )
11189 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011190 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011191 goto exit;
11192 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011193 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011194 data_len ) ) != 0 )
11195 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011196 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011197 goto exit;
11198 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011199 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011200 output + 16 ) ) != 0 )
11201 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011203 goto exit;
11204 }
11205
11206exit:
11207 mbedtls_md5_free( &mbedtls_md5 );
11208 mbedtls_sha1_free( &mbedtls_sha1 );
11209
11210 if( ret != 0 )
11211 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11212 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11213
11214 return( ret );
11215
11216}
11217#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11218 MBEDTLS_SSL_PROTO_TLS1_1 */
11219
11220#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11221 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011222
11223#if defined(MBEDTLS_USE_PSA_CRYPTO)
11224int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11225 unsigned char *hash, size_t *hashlen,
11226 unsigned char *data, size_t data_len,
11227 mbedtls_md_type_t md_alg )
11228{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011229 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011230 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011231 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11232
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011234
11235 if( ( status = psa_hash_setup( &hash_operation,
11236 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011237 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011238 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011239 goto exit;
11240 }
11241
Andrzej Kurek814feff2019-01-14 04:35:19 -050011242 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11243 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011244 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011245 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011246 goto exit;
11247 }
11248
Andrzej Kurek814feff2019-01-14 04:35:19 -050011249 if( ( status = psa_hash_update( &hash_operation,
11250 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011251 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011252 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011253 goto exit;
11254 }
11255
Andrzej Kurek814feff2019-01-14 04:35:19 -050011256 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11257 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011258 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011259 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011260 goto exit;
11261 }
11262
11263exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011264 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011265 {
11266 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11267 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011268 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011269 {
11270 case PSA_ERROR_NOT_SUPPORTED:
11271 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011272 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011273 case PSA_ERROR_BUFFER_TOO_SMALL:
11274 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11275 case PSA_ERROR_INSUFFICIENT_MEMORY:
11276 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11277 default:
11278 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11279 }
11280 }
11281 return( 0 );
11282}
11283
11284#else
11285
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011286int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011287 unsigned char *hash, size_t *hashlen,
11288 unsigned char *data, size_t data_len,
11289 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011290{
11291 int ret = 0;
11292 mbedtls_md_context_t ctx;
11293 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011294 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011295
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011296 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011297
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011298 mbedtls_md_init( &ctx );
11299
11300 /*
11301 * digitally-signed struct {
11302 * opaque client_random[32];
11303 * opaque server_random[32];
11304 * ServerDHParams params;
11305 * };
11306 */
11307 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11308 {
11309 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11310 goto exit;
11311 }
11312 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11313 {
11314 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11315 goto exit;
11316 }
11317 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11318 {
11319 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11320 goto exit;
11321 }
11322 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11323 {
11324 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11325 goto exit;
11326 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011327 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011328 {
11329 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11330 goto exit;
11331 }
11332
11333exit:
11334 mbedtls_md_free( &ctx );
11335
11336 if( ret != 0 )
11337 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11338 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11339
11340 return( ret );
11341}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011342#endif /* MBEDTLS_USE_PSA_CRYPTO */
11343
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011344#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11345 MBEDTLS_SSL_PROTO_TLS1_2 */
11346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011347#endif /* MBEDTLS_SSL_TLS_C */