blob: 96a9aab2b97070b88b0d07bba72a6636a5b75944 [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, "
4941 "version = [%d:%d], msglen = %d",
4942 ssl->in_msgtype,
4943 major_ver, minor_ver, ssl->in_msglen ) );
4944
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 Becker16ded982019-05-08 13:02:55 +01005112 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005113 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005114#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005115
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 return( ret );
5117 }
5118
Hanno Becker6430faf2019-05-08 11:57:13 +01005119 if( ssl->in_msgtype != rec.type )
5120 {
5121 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5122 ssl->in_msgtype, rec.type ) );
5123 }
5124
5125 /* The record content type may change during decryption,
5126 * so re-read it. */
5127 ssl->in_msgtype = rec.type;
5128 /* Also update the input buffer, because unfortunately
5129 * the server-side ssl_parse_client_hello() reparses the
5130 * record header when receiving a ClientHello initiating
5131 * a renegotiation. */
5132 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005133 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005134 ssl->in_msglen = rec.data_len;
5135 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5136 ssl->in_len[1] = (unsigned char)( rec.data_len );
5137
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005138 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5139 ssl->in_msg, ssl->in_msglen );
5140
Hanno Beckera0e20d02019-05-15 14:03:01 +01005141#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005142 /* We have already checked the record content type
5143 * in ssl_parse_record_header(), failing or silently
5144 * dropping the record in the case of an unknown type.
5145 *
5146 * Since with the use of CIDs, the record content type
5147 * might change during decryption, re-check the record
5148 * content type, but treat a failure as fatal this time. */
5149 if( ssl_check_record_type( ssl->in_msgtype ) )
5150 {
5151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5153 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005154#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005155
Angus Grattond8213d02016-05-25 20:56:48 +10005156 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005161 else if( ssl->in_msglen == 0 )
5162 {
5163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5164 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5165 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5166 {
5167 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5169 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5170 }
5171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5172
5173 ssl->nb_zero++;
5174
5175 /*
5176 * Three or more empty messages may be a DoS attack
5177 * (excessive CPU consumption).
5178 */
5179 if( ssl->nb_zero > 3 )
5180 {
5181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005182 "messages, possible DoS attack" ) );
5183 /* Treat the records as if they were not properly authenticated,
5184 * thereby failing the connection if we see more than allowed
5185 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005186 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5187 }
5188 }
5189 else
5190 ssl->nb_zero = 0;
5191
5192#if defined(MBEDTLS_SSL_PROTO_DTLS)
5193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5194 {
5195 ; /* in_ctr read from peer, not maintained internally */
5196 }
5197 else
5198#endif
5199 {
5200 unsigned i;
5201 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5202 if( ++ssl->in_ctr[i - 1] != 0 )
5203 break;
5204
5205 /* The loop goes to its end iff the counter is wrapping */
5206 if( i == ssl_ep_len( ssl ) )
5207 {
5208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5209 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5210 }
5211 }
5212
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 }
5214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005216 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005218 {
5219 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005222 return( ret );
5223 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005231 }
5232#endif
5233
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005234 return( 0 );
5235}
5236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005238
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005239/*
5240 * Read a record.
5241 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005242 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5243 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5244 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005245 */
Hanno Becker1097b342018-08-15 14:09:41 +01005246
5247/* Helper functions for mbedtls_ssl_read_record(). */
5248static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005249static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5250static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005251
Hanno Becker327c93b2018-08-15 13:56:18 +01005252int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005253 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005254{
5255 int ret;
5256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005258
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005259 if( ssl->keep_current_message == 0 )
5260 {
5261 do {
Simon Butcher99000142016-10-13 17:21:01 +01005262
Hanno Becker26994592018-08-15 14:14:59 +01005263 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005264 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005265 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005266
Hanno Beckere74d5562018-08-15 14:26:08 +01005267 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005268 {
Hanno Becker40f50842018-08-15 14:48:01 +01005269#if defined(MBEDTLS_SSL_PROTO_DTLS)
5270 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005271
Hanno Becker40f50842018-08-15 14:48:01 +01005272 /* We only check for buffered messages if the
5273 * current datagram is fully consumed. */
5274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005275 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005276 {
Hanno Becker40f50842018-08-15 14:48:01 +01005277 if( ssl_load_buffered_message( ssl ) == 0 )
5278 have_buffered = 1;
5279 }
5280
5281 if( have_buffered == 0 )
5282#endif /* MBEDTLS_SSL_PROTO_DTLS */
5283 {
5284 ret = ssl_get_next_record( ssl );
5285 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5286 continue;
5287
5288 if( ret != 0 )
5289 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005290 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005291 return( ret );
5292 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005293 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005294 }
5295
5296 ret = mbedtls_ssl_handle_message_type( ssl );
5297
Hanno Becker40f50842018-08-15 14:48:01 +01005298#if defined(MBEDTLS_SSL_PROTO_DTLS)
5299 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5300 {
5301 /* Buffer future message */
5302 ret = ssl_buffer_message( ssl );
5303 if( ret != 0 )
5304 return( ret );
5305
5306 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5307 }
5308#endif /* MBEDTLS_SSL_PROTO_DTLS */
5309
Hanno Becker90333da2017-10-10 11:27:13 +01005310 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5311 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005312
5313 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005314 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005315 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005316 return( ret );
5317 }
5318
Hanno Becker327c93b2018-08-15 13:56:18 +01005319 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005320 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005321 {
5322 mbedtls_ssl_update_handshake_status( ssl );
5323 }
Simon Butcher99000142016-10-13 17:21:01 +01005324 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005325 else
Simon Butcher99000142016-10-13 17:21:01 +01005326 {
Hanno Becker02f59072018-08-15 14:00:24 +01005327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005328 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005329 }
5330
5331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5332
5333 return( 0 );
5334}
5335
Hanno Becker40f50842018-08-15 14:48:01 +01005336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005337static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005338{
Hanno Becker40f50842018-08-15 14:48:01 +01005339 if( ssl->in_left > ssl->next_record_offset )
5340 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005341
Hanno Becker40f50842018-08-15 14:48:01 +01005342 return( 0 );
5343}
5344
5345static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5346{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005347 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005348 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005349 int ret = 0;
5350
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005351 if( hs == NULL )
5352 return( -1 );
5353
Hanno Beckere00ae372018-08-20 09:39:42 +01005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5355
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005356 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5357 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5358 {
5359 /* Check if we have seen a ChangeCipherSpec before.
5360 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005361 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005362 {
5363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5364 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005365 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005366 }
5367
Hanno Becker39b8bc92018-08-28 17:17:13 +01005368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005369 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5370 ssl->in_msglen = 1;
5371 ssl->in_msg[0] = 1;
5372
5373 /* As long as they are equal, the exact value doesn't matter. */
5374 ssl->in_left = 0;
5375 ssl->next_record_offset = 0;
5376
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005377 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005378 goto exit;
5379 }
Hanno Becker37f95322018-08-16 13:55:32 +01005380
Hanno Beckerb8f50142018-08-28 10:01:34 +01005381#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005382 /* Debug only */
5383 {
5384 unsigned offset;
5385 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5386 {
5387 hs_buf = &hs->buffering.hs[offset];
5388 if( hs_buf->is_valid == 1 )
5389 {
5390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5391 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005392 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005393 }
5394 }
5395 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005396#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005397
5398 /* Check if we have buffered and/or fully reassembled the
5399 * next handshake message. */
5400 hs_buf = &hs->buffering.hs[0];
5401 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5402 {
5403 /* Synthesize a record containing the buffered HS message. */
5404 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5405 ( hs_buf->data[2] << 8 ) |
5406 hs_buf->data[3];
5407
5408 /* Double-check that we haven't accidentally buffered
5409 * a message that doesn't fit into the input buffer. */
5410 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5411 {
5412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5414 }
5415
5416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5417 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5418 hs_buf->data, msg_len + 12 );
5419
5420 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5421 ssl->in_hslen = msg_len + 12;
5422 ssl->in_msglen = msg_len + 12;
5423 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5424
5425 ret = 0;
5426 goto exit;
5427 }
5428 else
5429 {
5430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5431 hs->in_msg_seq ) );
5432 }
5433
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005434 ret = -1;
5435
5436exit:
5437
5438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5439 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005440}
5441
Hanno Beckera02b0b42018-08-21 17:20:27 +01005442static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5443 size_t desired )
5444{
5445 int offset;
5446 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5448 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005449
Hanno Becker01315ea2018-08-21 17:22:17 +01005450 /* Get rid of future records epoch first, if such exist. */
5451 ssl_free_buffered_record( ssl );
5452
5453 /* Check if we have enough space available now. */
5454 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5455 hs->buffering.total_bytes_buffered ) )
5456 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005458 return( 0 );
5459 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005460
Hanno Becker4f432ad2018-08-28 10:02:32 +01005461 /* We don't have enough space to buffer the next expected handshake
5462 * message. Remove buffers used for future messages to gain space,
5463 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005464 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5465 offset >= 0; offset-- )
5466 {
5467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5468 offset ) );
5469
Hanno Beckerb309b922018-08-23 13:18:05 +01005470 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005471
5472 /* Check if we have enough space available now. */
5473 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5474 hs->buffering.total_bytes_buffered ) )
5475 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005477 return( 0 );
5478 }
5479 }
5480
5481 return( -1 );
5482}
5483
Hanno Becker40f50842018-08-15 14:48:01 +01005484static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5485{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005486 int ret = 0;
5487 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5488
5489 if( hs == NULL )
5490 return( 0 );
5491
5492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5493
5494 switch( ssl->in_msgtype )
5495 {
5496 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005498
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005499 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005500 break;
5501
5502 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005503 {
5504 unsigned recv_msg_seq_offset;
5505 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5506 mbedtls_ssl_hs_buffer *hs_buf;
5507 size_t msg_len = ssl->in_hslen - 12;
5508
5509 /* We should never receive an old handshake
5510 * message - double-check nonetheless. */
5511 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5512 {
5513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5515 }
5516
5517 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5518 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5519 {
5520 /* Silently ignore -- message too far in the future */
5521 MBEDTLS_SSL_DEBUG_MSG( 2,
5522 ( "Ignore future HS message with sequence number %u, "
5523 "buffering window %u - %u",
5524 recv_msg_seq, ssl->handshake->in_msg_seq,
5525 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5526
5527 goto exit;
5528 }
5529
5530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5531 recv_msg_seq, recv_msg_seq_offset ) );
5532
5533 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5534
5535 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005536 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005537 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005538 size_t reassembly_buf_sz;
5539
Hanno Becker37f95322018-08-16 13:55:32 +01005540 hs_buf->is_fragmented =
5541 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5542
5543 /* We copy the message back into the input buffer
5544 * after reassembly, so check that it's not too large.
5545 * This is an implementation-specific limitation
5546 * and not one from the standard, hence it is not
5547 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005548 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005549 {
5550 /* Ignore message */
5551 goto exit;
5552 }
5553
Hanno Beckere0b150f2018-08-21 15:51:03 +01005554 /* Check if we have enough space to buffer the message. */
5555 if( hs->buffering.total_bytes_buffered >
5556 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5557 {
5558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5560 }
5561
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005562 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5563 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005564
5565 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5566 hs->buffering.total_bytes_buffered ) )
5567 {
5568 if( recv_msg_seq_offset > 0 )
5569 {
5570 /* If we can't buffer a future message because
5571 * of space limitations -- ignore. */
5572 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",
5573 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5574 (unsigned) hs->buffering.total_bytes_buffered ) );
5575 goto exit;
5576 }
Hanno Beckere1801392018-08-21 16:51:05 +01005577 else
5578 {
5579 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",
5580 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5581 (unsigned) hs->buffering.total_bytes_buffered ) );
5582 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005583
Hanno Beckera02b0b42018-08-21 17:20:27 +01005584 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005585 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005586 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",
5587 (unsigned) msg_len,
5588 (unsigned) reassembly_buf_sz,
5589 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005590 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005591 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5592 goto exit;
5593 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005594 }
5595
5596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5597 msg_len ) );
5598
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005599 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5600 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005601 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005602 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005603 goto exit;
5604 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005605 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005606
5607 /* Prepare final header: copy msg_type, length and message_seq,
5608 * then add standardised fragment_offset and fragment_length */
5609 memcpy( hs_buf->data, ssl->in_msg, 6 );
5610 memset( hs_buf->data + 6, 0, 3 );
5611 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5612
5613 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005614
5615 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005616 }
5617 else
5618 {
5619 /* Make sure msg_type and length are consistent */
5620 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5621 {
5622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5623 /* Ignore */
5624 goto exit;
5625 }
5626 }
5627
Hanno Becker4422bbb2018-08-20 09:40:19 +01005628 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005629 {
5630 size_t frag_len, frag_off;
5631 unsigned char * const msg = hs_buf->data + 12;
5632
5633 /*
5634 * Check and copy current fragment
5635 */
5636
5637 /* Validation of header fields already done in
5638 * mbedtls_ssl_prepare_handshake_record(). */
5639 frag_off = ssl_get_hs_frag_off( ssl );
5640 frag_len = ssl_get_hs_frag_len( ssl );
5641
5642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5643 frag_off, frag_len ) );
5644 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5645
5646 if( hs_buf->is_fragmented )
5647 {
5648 unsigned char * const bitmask = msg + msg_len;
5649 ssl_bitmask_set( bitmask, frag_off, frag_len );
5650 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5651 msg_len ) == 0 );
5652 }
5653 else
5654 {
5655 hs_buf->is_complete = 1;
5656 }
5657
5658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5659 hs_buf->is_complete ? "" : "not yet " ) );
5660 }
5661
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005662 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005663 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005664
5665 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005666 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005667 break;
5668 }
5669
5670exit:
5671
5672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5673 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005674}
5675#endif /* MBEDTLS_SSL_PROTO_DTLS */
5676
Hanno Becker1097b342018-08-15 14:09:41 +01005677static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005679 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005680 * Consume last content-layer message and potentially
5681 * update in_msglen which keeps track of the contents'
5682 * consumption state.
5683 *
5684 * (1) Handshake messages:
5685 * Remove last handshake message, move content
5686 * and adapt in_msglen.
5687 *
5688 * (2) Alert messages:
5689 * Consume whole record content, in_msglen = 0.
5690 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005691 * (3) Change cipher spec:
5692 * Consume whole record content, in_msglen = 0.
5693 *
5694 * (4) Application data:
5695 * Don't do anything - the record layer provides
5696 * the application data as a stream transport
5697 * and consumes through mbedtls_ssl_read only.
5698 *
5699 */
5700
5701 /* Case (1): Handshake messages */
5702 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005703 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005704 /* Hard assertion to be sure that no application data
5705 * is in flight, as corrupting ssl->in_msglen during
5706 * ssl->in_offt != NULL is fatal. */
5707 if( ssl->in_offt != NULL )
5708 {
5709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5711 }
5712
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005713 /*
5714 * Get next Handshake message in the current record
5715 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005716
Hanno Becker4a810fb2017-05-24 16:27:30 +01005717 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005718 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005719 * current handshake content: If DTLS handshake
5720 * fragmentation is used, that's the fragment
5721 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005722 * size here is faulty and should be changed at
5723 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005724 * (2) While it doesn't seem to cause problems, one
5725 * has to be very careful not to assume that in_hslen
5726 * is always <= in_msglen in a sensible communication.
5727 * Again, it's wrong for DTLS handshake fragmentation.
5728 * The following check is therefore mandatory, and
5729 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005730 * Additionally, ssl->in_hslen might be arbitrarily out of
5731 * bounds after handling a DTLS message with an unexpected
5732 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005733 */
5734 if( ssl->in_hslen < ssl->in_msglen )
5735 {
5736 ssl->in_msglen -= ssl->in_hslen;
5737 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5738 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005739
Hanno Becker4a810fb2017-05-24 16:27:30 +01005740 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5741 ssl->in_msg, ssl->in_msglen );
5742 }
5743 else
5744 {
5745 ssl->in_msglen = 0;
5746 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005747
Hanno Becker4a810fb2017-05-24 16:27:30 +01005748 ssl->in_hslen = 0;
5749 }
5750 /* Case (4): Application data */
5751 else if( ssl->in_offt != NULL )
5752 {
5753 return( 0 );
5754 }
5755 /* Everything else (CCS & Alerts) */
5756 else
5757 {
5758 ssl->in_msglen = 0;
5759 }
5760
Hanno Becker1097b342018-08-15 14:09:41 +01005761 return( 0 );
5762}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005763
Hanno Beckere74d5562018-08-15 14:26:08 +01005764static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5765{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005766 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005767 return( 1 );
5768
5769 return( 0 );
5770}
5771
Hanno Becker5f066e72018-08-16 14:56:31 +01005772#if defined(MBEDTLS_SSL_PROTO_DTLS)
5773
5774static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5775{
5776 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5777 if( hs == NULL )
5778 return;
5779
Hanno Becker01315ea2018-08-21 17:22:17 +01005780 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005781 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005782 hs->buffering.total_bytes_buffered -=
5783 hs->buffering.future_record.len;
5784
5785 mbedtls_free( hs->buffering.future_record.data );
5786 hs->buffering.future_record.data = NULL;
5787 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005788}
5789
5790static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5791{
5792 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5793 unsigned char * rec;
5794 size_t rec_len;
5795 unsigned rec_epoch;
5796
5797 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5798 return( 0 );
5799
5800 if( hs == NULL )
5801 return( 0 );
5802
Hanno Becker5f066e72018-08-16 14:56:31 +01005803 rec = hs->buffering.future_record.data;
5804 rec_len = hs->buffering.future_record.len;
5805 rec_epoch = hs->buffering.future_record.epoch;
5806
5807 if( rec == NULL )
5808 return( 0 );
5809
Hanno Becker4cb782d2018-08-20 11:19:05 +01005810 /* Only consider loading future records if the
5811 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005812 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005813 return( 0 );
5814
Hanno Becker5f066e72018-08-16 14:56:31 +01005815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5816
5817 if( rec_epoch != ssl->in_epoch )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5820 goto exit;
5821 }
5822
5823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5824
5825 /* Double-check that the record is not too large */
5826 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5827 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5828 {
5829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5830 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5831 }
5832
5833 memcpy( ssl->in_hdr, rec, rec_len );
5834 ssl->in_left = rec_len;
5835 ssl->next_record_offset = 0;
5836
5837 ssl_free_buffered_record( ssl );
5838
5839exit:
5840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5841 return( 0 );
5842}
5843
5844static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5845{
5846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5847 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005848 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005849
5850 /* Don't buffer future records outside handshakes. */
5851 if( hs == NULL )
5852 return( 0 );
5853
5854 /* Only buffer handshake records (we are only interested
5855 * in Finished messages). */
5856 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5857 return( 0 );
5858
5859 /* Don't buffer more than one future epoch record. */
5860 if( hs->buffering.future_record.data != NULL )
5861 return( 0 );
5862
Hanno Becker01315ea2018-08-21 17:22:17 +01005863 /* Don't buffer record if there's not enough buffering space remaining. */
5864 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5865 hs->buffering.total_bytes_buffered ) )
5866 {
5867 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",
5868 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5869 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005870 return( 0 );
5871 }
5872
Hanno Becker5f066e72018-08-16 14:56:31 +01005873 /* Buffer record */
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5875 ssl->in_epoch + 1 ) );
5876 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5877 rec_hdr_len + ssl->in_msglen );
5878
5879 /* ssl_parse_record_header() only considers records
5880 * of the next epoch as candidates for buffering. */
5881 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005882 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005883
5884 hs->buffering.future_record.data =
5885 mbedtls_calloc( 1, hs->buffering.future_record.len );
5886 if( hs->buffering.future_record.data == NULL )
5887 {
5888 /* If we run out of RAM trying to buffer a
5889 * record from the next epoch, just ignore. */
5890 return( 0 );
5891 }
5892
Hanno Becker01315ea2018-08-21 17:22:17 +01005893 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005894
Hanno Becker01315ea2018-08-21 17:22:17 +01005895 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005896 return( 0 );
5897}
5898
5899#endif /* MBEDTLS_SSL_PROTO_DTLS */
5900
Hanno Beckere74d5562018-08-15 14:26:08 +01005901static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005902{
5903 int ret;
5904
Hanno Becker5f066e72018-08-16 14:56:31 +01005905#if defined(MBEDTLS_SSL_PROTO_DTLS)
5906 /* We might have buffered a future record; if so,
5907 * and if the epoch matches now, load it.
5908 * On success, this call will set ssl->in_left to
5909 * the length of the buffered record, so that
5910 * the calls to ssl_fetch_input() below will
5911 * essentially be no-ops. */
5912 ret = ssl_load_buffered_record( ssl );
5913 if( ret != 0 )
5914 return( ret );
5915#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005916
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005917 /* Reset in pointers to default state for TLS/DTLS records,
5918 * assuming no CID and no offset between record content and
5919 * record plaintext. */
5920 ssl_update_in_pointers( ssl );
5921
5922 /* Ensure that we have enough space available for the default form
5923 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5924 * with no space for CIDs counted in). */
5925 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5926 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005929 return( ret );
5930 }
5931
5932 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005935 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5936 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005937 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005938 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5939 {
5940 ret = ssl_buffer_future_record( ssl );
5941 if( ret != 0 )
5942 return( ret );
5943
5944 /* Fall through to handling of unexpected records */
5945 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5946 }
5947
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005948 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5949 {
5950 /* Skip unexpected record (but not whole datagram) */
5951 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005952 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005953
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5955 "(header)" ) );
5956 }
5957 else
5958 {
5959 /* Skip invalid record and the rest of the datagram */
5960 ssl->next_record_offset = 0;
5961 ssl->in_left = 0;
5962
5963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5964 "(header)" ) );
5965 }
5966
5967 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005968 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005969 }
5970#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005971 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005972 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005973
5974 /*
5975 * Read and optionally decrypt the message contents
5976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005978 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005981 return( ret );
5982 }
5983
5984 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005987 {
Hanno Becker5903de42019-05-03 14:46:38 +01005988 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005989 if( ssl->next_record_offset < ssl->in_left )
5990 {
5991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5992 }
5993 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005994 else
5995#endif
5996 ssl->in_left = 0;
5997
5998 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006002 {
6003 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006004 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006005 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006006 /* Except when waiting for Finished as a bad mac here
6007 * probably means something went wrong in the handshake
6008 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6009 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6010 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6011 {
6012#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6013 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6014 {
6015 mbedtls_ssl_send_alert_message( ssl,
6016 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6017 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6018 }
6019#endif
6020 return( ret );
6021 }
6022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006024 if( ssl->conf->badmac_limit != 0 &&
6025 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6028 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006029 }
6030#endif
6031
Hanno Becker4a810fb2017-05-24 16:27:30 +01006032 /* As above, invalid records cause
6033 * dismissal of the whole datagram. */
6034
6035 ssl->next_record_offset = 0;
6036 ssl->in_left = 0;
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006039 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006040 }
6041
6042 return( ret );
6043 }
6044 else
6045#endif
6046 {
6047 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6049 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 mbedtls_ssl_send_alert_message( ssl,
6052 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6053 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006054 }
6055#endif
6056 return( ret );
6057 }
6058 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006059
Simon Butcher99000142016-10-13 17:21:01 +01006060 return( 0 );
6061}
6062
6063int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6064{
6065 int ret;
6066
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006067 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006068 * Handle particular types of records
6069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 {
Simon Butcher99000142016-10-13 17:21:01 +01006072 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6073 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006074 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 }
6077
Hanno Beckere678eaa2018-08-21 14:57:46 +01006078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006079 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006080 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006081 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6083 ssl->in_msglen ) );
6084 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006085 }
6086
Hanno Beckere678eaa2018-08-21 14:57:46 +01006087 if( ssl->in_msg[0] != 1 )
6088 {
6089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6090 ssl->in_msg[0] ) );
6091 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6092 }
6093
6094#if defined(MBEDTLS_SSL_PROTO_DTLS)
6095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6096 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6097 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6098 {
6099 if( ssl->handshake == NULL )
6100 {
6101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6102 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6103 }
6104
6105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6106 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6107 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006108#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006109 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006113 if( ssl->in_msglen != 2 )
6114 {
6115 /* Note: Standard allows for more than one 2 byte alert
6116 to be packed in a single message, but Mbed TLS doesn't
6117 currently support this. */
6118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6119 ssl->in_msglen ) );
6120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6121 }
6122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 ssl->in_msg[0], ssl->in_msg[1] ) );
6125
6126 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006127 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006132 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6137 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6140 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006142
6143#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6144 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6145 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6146 {
Hanno Becker90333da2017-10-10 11:27:13 +01006147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006148 /* Will be handled when trying to parse ServerHello */
6149 return( 0 );
6150 }
6151#endif
6152
6153#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6154 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6155 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6156 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6157 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6158 {
6159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6160 /* Will be handled in mbedtls_ssl_parse_certificate() */
6161 return( 0 );
6162 }
6163#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6164
6165 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006166 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006167 }
6168
Hanno Beckerc76c6192017-06-06 10:03:17 +01006169#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006170 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006171 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006172 /* Drop unexpected ApplicationData records,
6173 * except at the beginning of renegotiations */
6174 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6175 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6176#if defined(MBEDTLS_SSL_RENEGOTIATION)
6177 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6178 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006179#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006180 )
6181 {
6182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6183 return( MBEDTLS_ERR_SSL_NON_FATAL );
6184 }
6185
6186 if( ssl->handshake != NULL &&
6187 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6188 {
6189 ssl_handshake_wrapup_free_hs_transform( ssl );
6190 }
6191 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006192#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006193
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 return( 0 );
6195}
6196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006198{
6199 int ret;
6200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6202 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6203 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006204 {
6205 return( ret );
6206 }
6207
6208 return( 0 );
6209}
6210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006212 unsigned char level,
6213 unsigned char message )
6214{
6215 int ret;
6216
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006217 if( ssl == NULL || ssl->conf == NULL )
6218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006224 ssl->out_msglen = 2;
6225 ssl->out_msg[0] = level;
6226 ssl->out_msg[1] = message;
6227
Hanno Becker67bc7c32018-08-06 11:33:50 +01006228 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006231 return( ret );
6232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006234
6235 return( 0 );
6236}
6237
Hanno Beckerb9d44792019-02-08 07:19:04 +00006238#if defined(MBEDTLS_X509_CRT_PARSE_C)
6239static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6240{
6241#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6242 if( session->peer_cert != NULL )
6243 {
6244 mbedtls_x509_crt_free( session->peer_cert );
6245 mbedtls_free( session->peer_cert );
6246 session->peer_cert = NULL;
6247 }
6248#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6249 if( session->peer_cert_digest != NULL )
6250 {
6251 /* Zeroization is not necessary. */
6252 mbedtls_free( session->peer_cert_digest );
6253 session->peer_cert_digest = NULL;
6254 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6255 session->peer_cert_digest_len = 0;
6256 }
6257#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6258}
6259#endif /* MBEDTLS_X509_CRT_PARSE_C */
6260
Paul Bakker5121ce52009-01-03 21:22:43 +00006261/*
6262 * Handshake functions
6263 */
Hanno Becker21489932019-02-05 13:20:55 +00006264#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006265/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006267{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006268 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6269 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006272
Hanno Becker7177a882019-02-05 13:36:46 +00006273 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006276 ssl->state++;
6277 return( 0 );
6278 }
6279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006282}
6283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006285{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006286 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6287 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290
Hanno Becker7177a882019-02-05 13:36:46 +00006291 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006294 ssl->state++;
6295 return( 0 );
6296 }
6297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006300}
Gilles Peskinef9828522017-05-03 12:28:43 +02006301
Hanno Becker21489932019-02-05 13:20:55 +00006302#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006303/* Some certificate support -> implement write and parse */
6304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006308 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006310 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6311 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006314
Hanno Becker7177a882019-02-05 13:36:46 +00006315 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006318 ssl->state++;
6319 return( 0 );
6320 }
6321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006323 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006324 {
6325 if( ssl->client_auth == 0 )
6326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 ssl->state++;
6329 return( 0 );
6330 }
6331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006333 /*
6334 * If using SSLv3 and got no cert, send an Alert message
6335 * (otherwise an empty Certificate message will be sent).
6336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6338 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 {
6340 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6342 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6343 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 goto write_msg;
6347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350#endif /* MBEDTLS_SSL_CLI_C */
6351#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6357 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006358 }
6359 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363
6364 /*
6365 * 0 . 0 handshake type
6366 * 1 . 3 handshake length
6367 * 4 . 6 length of all certs
6368 * 7 . 9 length of cert. 1
6369 * 10 . n-1 peer certificate
6370 * n . n+2 length of cert. 2
6371 * n+3 . ... upper level cert, etc.
6372 */
6373 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006375
Paul Bakker29087132010-03-21 21:03:34 +00006376 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 {
6378 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006379 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006382 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 }
6385
6386 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6387 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6388 ssl->out_msg[i + 2] = (unsigned char)( n );
6389
6390 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6391 i += n; crt = crt->next;
6392 }
6393
6394 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6395 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6396 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6397
6398 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6400 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006401
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006402#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006403write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006405
6406 ssl->state++;
6407
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006408 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 return( ret );
6412 }
6413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Paul Bakkered27a042013-04-18 22:46:23 +02006416 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006417}
6418
Hanno Becker84879e32019-01-31 07:44:03 +00006419#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006420
6421#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006422static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6423 unsigned char *crt_buf,
6424 size_t crt_buf_len )
6425{
6426 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6427
6428 if( peer_crt == NULL )
6429 return( -1 );
6430
6431 if( peer_crt->raw.len != crt_buf_len )
6432 return( -1 );
6433
Hanno Becker46f34d02019-02-08 14:00:04 +00006434 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006435}
Hanno Becker177475a2019-02-05 17:02:46 +00006436#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6437static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6438 unsigned char *crt_buf,
6439 size_t crt_buf_len )
6440{
6441 int ret;
6442 unsigned char const * const peer_cert_digest =
6443 ssl->session->peer_cert_digest;
6444 mbedtls_md_type_t const peer_cert_digest_type =
6445 ssl->session->peer_cert_digest_type;
6446 mbedtls_md_info_t const * const digest_info =
6447 mbedtls_md_info_from_type( peer_cert_digest_type );
6448 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6449 size_t digest_len;
6450
6451 if( peer_cert_digest == NULL || digest_info == NULL )
6452 return( -1 );
6453
6454 digest_len = mbedtls_md_get_size( digest_info );
6455 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6456 return( -1 );
6457
6458 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6459 if( ret != 0 )
6460 return( -1 );
6461
6462 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6463}
6464#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006465#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006466
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006467/*
6468 * Once the certificate message is read, parse it into a cert chain and
6469 * perform basic checks, but leave actual verification to the caller
6470 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006471static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6472 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006474 int ret;
6475#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6476 int crt_cnt=0;
6477#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006478 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006479 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6485 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 }
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6490 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6494 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 }
6497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006499
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006503 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006504
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006505 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 }
6513
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006514 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6515 i += 3;
6516
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006517 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 while( i < ssl->in_hslen )
6519 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006520 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006521 if ( i + 3 > ssl->in_hslen ) {
6522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006523 mbedtls_ssl_send_alert_message( ssl,
6524 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006526 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6527 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006528 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6529 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 if( ssl->in_msg[i] != 0 )
6531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006533 mbedtls_ssl_send_alert_message( ssl,
6534 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6535 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 }
6538
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006539 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006540 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6541 | (unsigned int) ssl->in_msg[i + 2];
6542 i += 3;
6543
6544 if( n < 128 || i + n > ssl->in_hslen )
6545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006547 mbedtls_ssl_send_alert_message( ssl,
6548 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6549 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 }
6552
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006553 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006554#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6555 if( crt_cnt++ == 0 &&
6556 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6557 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006558 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006559 /* During client-side renegotiation, check that the server's
6560 * end-CRTs hasn't changed compared to the initial handshake,
6561 * mitigating the triple handshake attack. On success, reuse
6562 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6564 if( ssl_check_peer_crt_unchanged( ssl,
6565 &ssl->in_msg[i],
6566 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006567 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6569 mbedtls_ssl_send_alert_message( ssl,
6570 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6571 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6572 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006573 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006574
6575 /* Now we can safely free the original chain. */
6576 ssl_clear_peer_cert( ssl->session );
6577 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006578#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6579
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006580 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006581#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006582 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006583#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006584 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006585 * it in-place from the input buffer instead of making a copy. */
6586 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6587#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006588 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006590 case 0: /*ok*/
6591 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6592 /* Ignore certificate with an unknown algorithm: maybe a
6593 prior certificate was already trusted. */
6594 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006595
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006596 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6597 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6598 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006599
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006600 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6601 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6602 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006603
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006604 default:
6605 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6606 crt_parse_der_failed:
6607 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6608 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6609 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 }
6611
6612 i += n;
6613 }
6614
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006615 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006616 return( 0 );
6617}
6618
Hanno Becker4a55f632019-02-05 12:49:06 +00006619#if defined(MBEDTLS_SSL_SRV_C)
6620static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6621{
6622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6623 return( -1 );
6624
6625#if defined(MBEDTLS_SSL_PROTO_SSL3)
6626 /*
6627 * Check if the client sent an empty certificate
6628 */
6629 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6630 {
6631 if( ssl->in_msglen == 2 &&
6632 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6633 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6634 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6635 {
6636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6637 return( 0 );
6638 }
6639
6640 return( -1 );
6641 }
6642#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6643
6644#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6645 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6646 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6647 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6648 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6649 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6650 {
6651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6652 return( 0 );
6653 }
6654
6655 return( -1 );
6656#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6657 MBEDTLS_SSL_PROTO_TLS1_2 */
6658}
6659#endif /* MBEDTLS_SSL_SRV_C */
6660
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006661/* Check if a certificate message is expected.
6662 * Return either
6663 * - SSL_CERTIFICATE_EXPECTED, or
6664 * - SSL_CERTIFICATE_SKIP
6665 * indicating whether a Certificate message is expected or not.
6666 */
6667#define SSL_CERTIFICATE_EXPECTED 0
6668#define SSL_CERTIFICATE_SKIP 1
6669static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6670 int authmode )
6671{
6672 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006673 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006674
6675 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6676 return( SSL_CERTIFICATE_SKIP );
6677
6678#if defined(MBEDTLS_SSL_SRV_C)
6679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6680 {
6681 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6682 return( SSL_CERTIFICATE_SKIP );
6683
6684 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6685 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006686 ssl->session_negotiate->verify_result =
6687 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6688 return( SSL_CERTIFICATE_SKIP );
6689 }
6690 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006691#else
6692 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006693#endif /* MBEDTLS_SSL_SRV_C */
6694
6695 return( SSL_CERTIFICATE_EXPECTED );
6696}
6697
Hanno Becker68636192019-02-05 14:36:34 +00006698static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6699 int authmode,
6700 mbedtls_x509_crt *chain,
6701 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006702{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006703 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006704 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006705 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006706 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006707
Hanno Becker8927c832019-04-03 12:52:50 +01006708 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6709 void *p_vrfy;
6710
Hanno Becker68636192019-02-05 14:36:34 +00006711 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6712 return( 0 );
6713
Hanno Becker8927c832019-04-03 12:52:50 +01006714 if( ssl->f_vrfy != NULL )
6715 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006717 f_vrfy = ssl->f_vrfy;
6718 p_vrfy = ssl->p_vrfy;
6719 }
6720 else
6721 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006723 f_vrfy = ssl->conf->f_vrfy;
6724 p_vrfy = ssl->conf->p_vrfy;
6725 }
6726
Hanno Becker68636192019-02-05 14:36:34 +00006727 /*
6728 * Main check: verify certificate
6729 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006730#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6731 if( ssl->conf->f_ca_cb != NULL )
6732 {
6733 ((void) rs_ctx);
6734 have_ca_chain = 1;
6735
6736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006737 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006738 chain,
6739 ssl->conf->f_ca_cb,
6740 ssl->conf->p_ca_cb,
6741 ssl->conf->cert_profile,
6742 ssl->hostname,
6743 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006744 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006745 }
6746 else
6747#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6748 {
6749 mbedtls_x509_crt *ca_chain;
6750 mbedtls_x509_crl *ca_crl;
6751
6752#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6753 if( ssl->handshake->sni_ca_chain != NULL )
6754 {
6755 ca_chain = ssl->handshake->sni_ca_chain;
6756 ca_crl = ssl->handshake->sni_ca_crl;
6757 }
6758 else
6759#endif
6760 {
6761 ca_chain = ssl->conf->ca_chain;
6762 ca_crl = ssl->conf->ca_crl;
6763 }
6764
6765 if( ca_chain != NULL )
6766 have_ca_chain = 1;
6767
6768 ret = mbedtls_x509_crt_verify_restartable(
6769 chain,
6770 ca_chain, ca_crl,
6771 ssl->conf->cert_profile,
6772 ssl->hostname,
6773 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006774 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006775 }
Hanno Becker68636192019-02-05 14:36:34 +00006776
6777 if( ret != 0 )
6778 {
6779 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6780 }
6781
6782#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6783 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6784 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6785#endif
6786
6787 /*
6788 * Secondary checks: always done, but change 'ret' only if it was 0
6789 */
6790
6791#if defined(MBEDTLS_ECP_C)
6792 {
6793 const mbedtls_pk_context *pk = &chain->pk;
6794
6795 /* If certificate uses an EC key, make sure the curve is OK */
6796 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6797 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6798 {
6799 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6800
6801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6802 if( ret == 0 )
6803 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6804 }
6805 }
6806#endif /* MBEDTLS_ECP_C */
6807
6808 if( mbedtls_ssl_check_cert_usage( chain,
6809 ciphersuite_info,
6810 ! ssl->conf->endpoint,
6811 &ssl->session_negotiate->verify_result ) != 0 )
6812 {
6813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6814 if( ret == 0 )
6815 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6816 }
6817
6818 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6819 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6820 * with details encoded in the verification flags. All other kinds
6821 * of error codes, including those from the user provided f_vrfy
6822 * functions, are treated as fatal and lead to a failure of
6823 * ssl_parse_certificate even if verification was optional. */
6824 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6825 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6826 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6827 {
6828 ret = 0;
6829 }
6830
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006831 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006832 {
6833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6834 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6835 }
6836
6837 if( ret != 0 )
6838 {
6839 uint8_t alert;
6840
6841 /* The certificate may have been rejected for several reasons.
6842 Pick one and send the corresponding alert. Which alert to send
6843 may be a subject of debate in some cases. */
6844 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6845 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6847 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6849 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6850 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6851 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6852 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6853 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6854 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6855 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6856 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6857 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6858 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6859 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6860 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6861 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6862 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6863 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6864 else
6865 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6866 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6867 alert );
6868 }
6869
6870#if defined(MBEDTLS_DEBUG_C)
6871 if( ssl->session_negotiate->verify_result != 0 )
6872 {
6873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6874 ssl->session_negotiate->verify_result ) );
6875 }
6876 else
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6879 }
6880#endif /* MBEDTLS_DEBUG_C */
6881
6882 return( ret );
6883}
6884
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006885#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6886static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6887 unsigned char *start, size_t len )
6888{
6889 int ret;
6890 /* Remember digest of the peer's end-CRT. */
6891 ssl->session_negotiate->peer_cert_digest =
6892 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6893 if( ssl->session_negotiate->peer_cert_digest == NULL )
6894 {
6895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6896 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6897 mbedtls_ssl_send_alert_message( ssl,
6898 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6899 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6900
6901 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6902 }
6903
6904 ret = mbedtls_md( mbedtls_md_info_from_type(
6905 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6906 start, len,
6907 ssl->session_negotiate->peer_cert_digest );
6908
6909 ssl->session_negotiate->peer_cert_digest_type =
6910 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6911 ssl->session_negotiate->peer_cert_digest_len =
6912 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6913
6914 return( ret );
6915}
6916
6917static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6918 unsigned char *start, size_t len )
6919{
6920 unsigned char *end = start + len;
6921 int ret;
6922
6923 /* Make a copy of the peer's raw public key. */
6924 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6925 ret = mbedtls_pk_parse_subpubkey( &start, end,
6926 &ssl->handshake->peer_pubkey );
6927 if( ret != 0 )
6928 {
6929 /* We should have parsed the public key before. */
6930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6931 }
6932
6933 return( 0 );
6934}
6935#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6936
Hanno Becker68636192019-02-05 14:36:34 +00006937int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6938{
6939 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006940 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006941#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6942 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6943 ? ssl->handshake->sni_authmode
6944 : ssl->conf->authmode;
6945#else
6946 const int authmode = ssl->conf->authmode;
6947#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006948 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006949 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006950
6951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6952
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006953 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6954 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006955 {
6956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006957 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006958 }
6959
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006960#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6961 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006962 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006963 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006964 chain = ssl->handshake->ecrs_peer_cert;
6965 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006966 goto crt_verify;
6967 }
6968#endif
6969
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006970 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006971 {
6972 /* mbedtls_ssl_read_record may have sent an alert already. We
6973 let it decide whether to alert. */
6974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006975 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006976 }
6977
Hanno Becker4a55f632019-02-05 12:49:06 +00006978#if defined(MBEDTLS_SSL_SRV_C)
6979 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6980 {
6981 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006982
6983 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006984 ret = 0;
6985 else
6986 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006987
Hanno Becker6bdfab22019-02-05 13:11:17 +00006988 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006989 }
6990#endif /* MBEDTLS_SSL_SRV_C */
6991
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006992 /* Clear existing peer CRT structure in case we tried to
6993 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006994 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006995
6996 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6997 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006998 {
6999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7000 sizeof( mbedtls_x509_crt ) ) );
7001 mbedtls_ssl_send_alert_message( ssl,
7002 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7003 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007004
Hanno Becker3dad3112019-02-05 17:19:52 +00007005 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7006 goto exit;
7007 }
7008 mbedtls_x509_crt_init( chain );
7009
7010 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007011 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007012 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007013
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007014#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7015 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007016 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007017
7018crt_verify:
7019 if( ssl->handshake->ecrs_enabled)
7020 rs_ctx = &ssl->handshake->ecrs_ctx;
7021#endif
7022
Hanno Becker68636192019-02-05 14:36:34 +00007023 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007024 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007025 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007026 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007028#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007029 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007030 unsigned char *crt_start, *pk_start;
7031 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007032
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007033 /* We parse the CRT chain without copying, so
7034 * these pointers point into the input buffer,
7035 * and are hence still valid after freeing the
7036 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007037
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007038 crt_start = chain->raw.p;
7039 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007040
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007041 pk_start = chain->pk_raw.p;
7042 pk_len = chain->pk_raw.len;
7043
7044 /* Free the CRT structures before computing
7045 * digest and copying the peer's public key. */
7046 mbedtls_x509_crt_free( chain );
7047 mbedtls_free( chain );
7048 chain = NULL;
7049
7050 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007051 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007052 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007053
7054 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7055 if( ret != 0 )
7056 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007057 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007058#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7059 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007060 ssl->session_negotiate->peer_cert = chain;
7061 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007062#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007065
Hanno Becker6bdfab22019-02-05 13:11:17 +00007066exit:
7067
Hanno Becker3dad3112019-02-05 17:19:52 +00007068 if( ret == 0 )
7069 ssl->state++;
7070
7071#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7072 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7073 {
7074 ssl->handshake->ecrs_peer_cert = chain;
7075 chain = NULL;
7076 }
7077#endif
7078
7079 if( chain != NULL )
7080 {
7081 mbedtls_x509_crt_free( chain );
7082 mbedtls_free( chain );
7083 }
7084
Paul Bakker5121ce52009-01-03 21:22:43 +00007085 return( ret );
7086}
Hanno Becker21489932019-02-05 13:20:55 +00007087#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007090{
7091 int ret;
7092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007096 ssl->out_msglen = 1;
7097 ssl->out_msg[0] = 1;
7098
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 ssl->state++;
7100
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007101 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 return( ret );
7105 }
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007108
7109 return( 0 );
7110}
7111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113{
7114 int ret;
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117
Hanno Becker327c93b2018-08-15 13:56:18 +01007118 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 return( ret );
7122 }
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007127 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7128 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 }
7131
Hanno Beckere678eaa2018-08-21 14:57:46 +01007132 /* CCS records are only accepted if they have length 1 and content '1',
7133 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007134
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007135 /*
7136 * Switch to our negotiated transform and session parameters for inbound
7137 * data.
7138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007140 ssl->transform_in = ssl->transform_negotiate;
7141 ssl->session_in = ssl->session_negotiate;
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007147 ssl_dtls_replay_reset( ssl );
7148#endif
7149
7150 /* Increment epoch */
7151 if( ++ssl->in_epoch == 0 )
7152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007154 /* This is highly unlikely to happen for legitimate reasons, so
7155 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007157 }
7158 }
7159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007161 memset( ssl->in_ctr, 0, 8 );
7162
Hanno Becker79594fd2019-05-08 09:38:41 +01007163 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7166 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007171 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7172 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007174 }
7175 }
7176#endif
7177
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 ssl->state++;
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007181
7182 return( 0 );
7183}
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7186 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007187{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007188 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7191 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7192 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007193 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007194 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7197#if defined(MBEDTLS_SHA512_C)
7198 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007199 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7200 else
7201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SHA256_C)
7203 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007204 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007205 else
7206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007210 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007211 }
Paul Bakker380da532012-04-18 16:10:25 +00007212}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7217 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007218 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7219 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7222#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007224 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007225 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7226#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007227 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007228#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007231#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007232 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007233 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007234#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007236#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007239}
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007242 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7245 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007246 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7247 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7250#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007251#if defined(MBEDTLS_USE_PSA_CRYPTO)
7252 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7253#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007254 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007255#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007258#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007259 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007260#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007261 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007262#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007265}
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7268 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7269static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007270 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007271{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007272 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7273 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007274}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007275#endif
Paul Bakker380da532012-04-18 16:10:25 +00007276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7278#if defined(MBEDTLS_SHA256_C)
7279static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007280 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007281{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007282#if defined(MBEDTLS_USE_PSA_CRYPTO)
7283 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7284#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007285 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007286#endif
Paul Bakker380da532012-04-18 16:10:25 +00007287}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007288#endif
Paul Bakker380da532012-04-18 16:10:25 +00007289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_SHA512_C)
7291static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007292 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007293{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007294#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007295 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007296#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007297 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007298#endif
Paul Bakker380da532012-04-18 16:10:25 +00007299}
Paul Bakker769075d2012-11-24 11:26:46 +01007300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007304static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007306{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007307 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007308 mbedtls_md5_context md5;
7309 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007310
Paul Bakker5121ce52009-01-03 21:22:43 +00007311 unsigned char padbuf[48];
7312 unsigned char md5sum[16];
7313 unsigned char sha1sum[20];
7314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007316 if( !session )
7317 session = ssl->session;
7318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007321 mbedtls_md5_init( &md5 );
7322 mbedtls_sha1_init( &sha1 );
7323
7324 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7325 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
7327 /*
7328 * SSLv3:
7329 * hash =
7330 * MD5( master + pad2 +
7331 * MD5( handshake + sender + master + pad1 ) )
7332 * + SHA1( master + pad2 +
7333 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 */
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007337 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7338 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007339#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007342 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7343 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007347 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
Paul Bakker1ef83d62012-04-11 12:09:53 +00007349 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007351 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7352 mbedtls_md5_update_ret( &md5, session->master, 48 );
7353 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7354 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007356 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7357 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7358 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7359 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Paul Bakker1ef83d62012-04-11 12:09:53 +00007361 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007363 mbedtls_md5_starts_ret( &md5 );
7364 mbedtls_md5_update_ret( &md5, session->master, 48 );
7365 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7366 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7367 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007369 mbedtls_sha1_starts_ret( &sha1 );
7370 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7371 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7372 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7373 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007376
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007377 mbedtls_md5_free( &md5 );
7378 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007380 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7381 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7382 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007391{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007392 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007393 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007394 mbedtls_md5_context md5;
7395 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007399 if( !session )
7400 session = ssl->session;
7401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007403
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007404 mbedtls_md5_init( &md5 );
7405 mbedtls_sha1_init( &sha1 );
7406
7407 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7408 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007409
Paul Bakker1ef83d62012-04-11 12:09:53 +00007410 /*
7411 * TLSv1:
7412 * hash = PRF( master, finished_label,
7413 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7414 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007417 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7418 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007419#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007422 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7423 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007424#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007427 ? "client finished"
7428 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007430 mbedtls_md5_finish_ret( &md5, padbuf );
7431 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007433 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007434 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007437
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007438 mbedtls_md5_free( &md5 );
7439 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007440
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007441 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7448#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007449static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007451{
7452 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007453 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007454 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007455#if defined(MBEDTLS_USE_PSA_CRYPTO)
7456 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007457 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007458 psa_status_t status;
7459#else
7460 mbedtls_sha256_context sha256;
7461#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007464 if( !session )
7465 session = ssl->session;
7466
Andrzej Kurekeb342242019-01-29 09:14:33 -05007467 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7468 ? "client finished"
7469 : "server finished";
7470
7471#if defined(MBEDTLS_USE_PSA_CRYPTO)
7472 sha256_psa = psa_hash_operation_init();
7473
7474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7475
7476 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7477 if( status != PSA_SUCCESS )
7478 {
7479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7480 return;
7481 }
7482
7483 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7484 if( status != PSA_SUCCESS )
7485 {
7486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7487 return;
7488 }
7489 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7490#else
7491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007492 mbedtls_sha256_init( &sha256 );
7493
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007495
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007496 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007497
7498 /*
7499 * TLSv1.2:
7500 * hash = PRF( master, finished_label,
7501 * Hash( handshake ) )[0.11]
7502 */
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if !defined(MBEDTLS_SHA256_ALT)
7505 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007506 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007507#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007508
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007509 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007510 mbedtls_sha256_free( &sha256 );
7511#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007512
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007513 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007514 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007518 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007525static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007527{
7528 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007529 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007530 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007531#if defined(MBEDTLS_USE_PSA_CRYPTO)
7532 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007533 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007534 psa_status_t status;
7535#else
7536 mbedtls_sha512_context sha512;
7537#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007540 if( !session )
7541 session = ssl->session;
7542
Andrzej Kurekeb342242019-01-29 09:14:33 -05007543 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7544 ? "client finished"
7545 : "server finished";
7546
7547#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007548 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549
7550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7551
Andrzej Kurek972fba52019-01-30 03:29:12 -05007552 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007553 if( status != PSA_SUCCESS )
7554 {
7555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7556 return;
7557 }
7558
Andrzej Kurek972fba52019-01-30 03:29:12 -05007559 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007560 if( status != PSA_SUCCESS )
7561 {
7562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7563 return;
7564 }
7565 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7566#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007567 mbedtls_sha512_init( &sha512 );
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007570
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007571 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007572
7573 /*
7574 * TLSv1.2:
7575 * hash = PRF( master, finished_label,
7576 * Hash( handshake ) )[0.11]
7577 */
7578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007580 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7581 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007582#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007583
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007584 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585 mbedtls_sha512_free( &sha512 );
7586#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007587
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007588 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007589 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007592
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007593 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007596}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#endif /* MBEDTLS_SHA512_C */
7598#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007603
7604 /*
7605 * Free our handshake params
7606 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007607 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007609 ssl->handshake = NULL;
7610
7611 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007612 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007613 */
7614 if( ssl->transform )
7615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 mbedtls_ssl_transform_free( ssl->transform );
7617 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007618 }
7619 ssl->transform = ssl->transform_negotiate;
7620 ssl->transform_negotiate = NULL;
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007623}
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626{
7627 int resume = ssl->handshake->resume;
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631#if defined(MBEDTLS_SSL_RENEGOTIATION)
7632 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007635 ssl->renego_records_seen = 0;
7636 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007637#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007638
7639 /*
7640 * Free the previous session and switch in the current one
7641 */
Paul Bakker0a597072012-09-25 21:55:46 +00007642 if( ssl->session )
7643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007645 /* RFC 7366 3.1: keep the EtM state */
7646 ssl->session_negotiate->encrypt_then_mac =
7647 ssl->session->encrypt_then_mac;
7648#endif
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 mbedtls_ssl_session_free( ssl->session );
7651 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007652 }
7653 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007654 ssl->session_negotiate = NULL;
7655
Paul Bakker0a597072012-09-25 21:55:46 +00007656 /*
7657 * Add cache entry
7658 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007659 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007660 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007661 resume == 0 )
7662 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007663 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007665 }
Paul Bakker0a597072012-09-25 21:55:46 +00007666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007669 ssl->handshake->flight != NULL )
7670 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007671 /* Cancel handshake timer */
7672 ssl_set_timer( ssl, 0 );
7673
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007674 /* Keep last flight around in case we need to resend it:
7675 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007677 }
7678 else
7679#endif
7680 ssl_handshake_wrapup_free_hs_transform( ssl );
7681
Paul Bakker48916f92012-09-16 19:57:18 +00007682 ssl->state++;
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007685}
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007689 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007693 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007694
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007695 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007696
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007697 /*
7698 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7699 * may define some other value. Currently (early 2016), no defined
7700 * ciphersuite does this (and this is unlikely to change as activity has
7701 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007706 ssl->verify_data_len = hash_len;
7707 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007708#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007709
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7712 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007713
7714 /*
7715 * In case of session resuming, invert the client and server
7716 * ChangeCipherSpec messages order.
7717 */
Paul Bakker0a597072012-09-25 21:55:46 +00007718 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007728 }
7729 else
7730 ssl->state++;
7731
Paul Bakker48916f92012-09-16 19:57:18 +00007732 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007733 * Switch to our negotiated transform and session parameters for outbound
7734 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007740 {
7741 unsigned char i;
7742
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007743 /* Remember current epoch settings for resending */
7744 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007745 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007746
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007747 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007748 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007749
7750 /* Increment epoch */
7751 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007752 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007753 break;
7754
7755 /* The loop goes to its end iff the counter is wrapping */
7756 if( i == 0 )
7757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7759 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007760 }
7761 }
7762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007764 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007766 ssl->transform_out = ssl->transform_negotiate;
7767 ssl->session_out = ssl->session_negotiate;
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7770 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7775 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007776 }
7777 }
7778#endif
7779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007783#endif
7784
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007785 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007786 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 return( ret );
7789 }
7790
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007791#if defined(MBEDTLS_SSL_PROTO_DTLS)
7792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7793 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7794 {
7795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7796 return( ret );
7797 }
7798#endif
7799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007801
7802 return( 0 );
7803}
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007806#define SSL_MAX_HASH_LEN 36
7807#else
7808#define SSL_MAX_HASH_LEN 12
7809#endif
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812{
Paul Bakker23986e52011-04-24 08:57:21 +00007813 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007814 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007815 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007818
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007819 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
Hanno Becker327c93b2018-08-15 13:56:18 +01007821 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 return( ret );
7825 }
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007830 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7831 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833 }
7834
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007835 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#if defined(MBEDTLS_SSL_PROTO_SSL3)
7837 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007838 hash_len = 36;
7839 else
7840#endif
7841 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7844 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007847 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7848 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007850 }
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007853 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7857 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 }
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007862 ssl->verify_data_len = hash_len;
7863 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007864#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007865
Paul Bakker0a597072012-09-25 21:55:46 +00007866 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007873 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007876 }
7877 else
7878 ssl->state++;
7879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007883#endif
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007886
7887 return( 0 );
7888}
7889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7895 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7896 mbedtls_md5_init( &handshake->fin_md5 );
7897 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007898 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7899 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7902#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007903#if defined(MBEDTLS_USE_PSA_CRYPTO)
7904 handshake->fin_sha256_psa = psa_hash_operation_init();
7905 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7906#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007908 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007909#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007912#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007913 handshake->fin_sha384_psa = psa_hash_operation_init();
7914 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007915#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007917 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007918#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007921
7922 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007923
7924#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7925 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7926 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7927#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_DHM_C)
7930 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_ECDH_C)
7933 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007934#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007935#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007936 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007937#if defined(MBEDTLS_SSL_CLI_C)
7938 handshake->ecjpake_cache = NULL;
7939 handshake->ecjpake_cache_len = 0;
7940#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007941#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007942
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007943#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007944 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007945#endif
7946
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007947#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7948 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7949#endif
Hanno Becker75173122019-02-06 16:18:31 +00007950
7951#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7952 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7953 mbedtls_pk_init( &handshake->peer_pubkey );
7954#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955}
7956
Hanno Beckera18d1322018-01-03 14:27:32 +00007957void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7962 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007963
Hanno Beckerd56ed242018-01-03 15:32:51 +00007964#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_md_init( &transform->md_ctx_enc );
7966 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007967#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968}
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007973}
7974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007976{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007977 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007978 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007983 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007984
7985 /*
7986 * Either the pointers are now NULL or cleared properly and can be freed.
7987 * Now allocate missing structures.
7988 */
7989 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007991 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007992 }
Paul Bakker48916f92012-09-16 19:57:18 +00007993
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007995 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007996 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007997 }
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Paul Bakker82788fb2014-10-20 13:59:19 +02007999 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008000 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008001 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008002 }
Paul Bakker48916f92012-09-16 19:57:18 +00008003
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008004 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008005 if( ssl->handshake == NULL ||
8006 ssl->transform_negotiate == NULL ||
8007 ssl->session_negotiate == NULL )
8008 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 mbedtls_free( ssl->handshake );
8012 mbedtls_free( ssl->transform_negotiate );
8013 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008014
8015 ssl->handshake = NULL;
8016 ssl->transform_negotiate = NULL;
8017 ssl->session_negotiate = NULL;
8018
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008019 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008020 }
8021
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008024 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008025 ssl_handshake_params_init( ssl->handshake );
8026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8029 {
8030 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008031
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8034 else
8035 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008036
8037 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008038 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008039#endif
8040
Paul Bakker48916f92012-09-16 19:57:18 +00008041 return( 0 );
8042}
8043
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008044#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008045/* Dummy cookie callbacks for defaults */
8046static int ssl_cookie_write_dummy( void *ctx,
8047 unsigned char **p, unsigned char *end,
8048 const unsigned char *cli_id, size_t cli_id_len )
8049{
8050 ((void) ctx);
8051 ((void) p);
8052 ((void) end);
8053 ((void) cli_id);
8054 ((void) cli_id_len);
8055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008057}
8058
8059static int ssl_cookie_check_dummy( void *ctx,
8060 const unsigned char *cookie, size_t cookie_len,
8061 const unsigned char *cli_id, size_t cli_id_len )
8062{
8063 ((void) ctx);
8064 ((void) cookie);
8065 ((void) cookie_len);
8066 ((void) cli_id);
8067 ((void) cli_id_len);
8068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008070}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008071#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008072
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008073/* Once ssl->out_hdr as the address of the beginning of the
8074 * next outgoing record is set, deduce the other pointers.
8075 *
8076 * Note: For TLS, we save the implicit record sequence number
8077 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8078 * and the caller has to make sure there's space for this.
8079 */
8080
8081static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8082 mbedtls_ssl_transform *transform )
8083{
8084#if defined(MBEDTLS_SSL_PROTO_DTLS)
8085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8086 {
8087 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008088#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008089 ssl->out_cid = ssl->out_ctr + 8;
8090 ssl->out_len = ssl->out_cid;
8091 if( transform != NULL )
8092 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008093#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008094 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008096 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008097 }
8098 else
8099#endif
8100 {
8101 ssl->out_ctr = ssl->out_hdr - 8;
8102 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008103#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008104 ssl->out_cid = ssl->out_len;
8105#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008106 ssl->out_iv = ssl->out_hdr + 5;
8107 }
8108
8109 /* Adjust out_msg to make space for explicit IV, if used. */
8110 if( transform != NULL &&
8111 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8112 {
8113 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8114 }
8115 else
8116 ssl->out_msg = ssl->out_iv;
8117}
8118
8119/* Once ssl->in_hdr as the address of the beginning of the
8120 * next incoming record is set, deduce the other pointers.
8121 *
8122 * Note: For TLS, we save the implicit record sequence number
8123 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8124 * and the caller has to make sure there's space for this.
8125 */
8126
Hanno Becker79594fd2019-05-08 09:38:41 +01008127static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008128{
Hanno Becker79594fd2019-05-08 09:38:41 +01008129 /* This function sets the pointers to match the case
8130 * of unprotected TLS/DTLS records, with both ssl->in_iv
8131 * and ssl->in_msg pointing to the beginning of the record
8132 * content.
8133 *
8134 * When decrypting a protected record, ssl->in_msg
8135 * will be shifted to point to the beginning of the
8136 * record plaintext.
8137 */
8138
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008139#if defined(MBEDTLS_SSL_PROTO_DTLS)
8140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8141 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008142 /* This sets the header pointers to match records
8143 * without CID. When we receive a record containing
8144 * a CID, the fields are shifted accordingly in
8145 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008147#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008148 ssl->in_cid = ssl->in_ctr + 8;
8149 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008150#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008151 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008152#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008153 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008154 }
8155 else
8156#endif
8157 {
8158 ssl->in_ctr = ssl->in_hdr - 8;
8159 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008161 ssl->in_cid = ssl->in_len;
8162#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008163 ssl->in_iv = ssl->in_hdr + 5;
8164 }
8165
Hanno Becker79594fd2019-05-08 09:38:41 +01008166 /* This will be adjusted at record decryption time. */
8167 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168}
8169
Paul Bakker5121ce52009-01-03 21:22:43 +00008170/*
8171 * Initialize an SSL context
8172 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008173void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8174{
8175 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8176}
8177
8178/*
8179 * Setup an SSL context
8180 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008181
8182static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8183{
8184 /* Set the incoming and outgoing record pointers. */
8185#if defined(MBEDTLS_SSL_PROTO_DTLS)
8186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8187 {
8188 ssl->out_hdr = ssl->out_buf;
8189 ssl->in_hdr = ssl->in_buf;
8190 }
8191 else
8192#endif /* MBEDTLS_SSL_PROTO_DTLS */
8193 {
8194 ssl->out_hdr = ssl->out_buf + 8;
8195 ssl->in_hdr = ssl->in_buf + 8;
8196 }
8197
8198 /* Derive other internal pointers. */
8199 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008200 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008201}
8202
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008203int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008204 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008205{
Paul Bakker48916f92012-09-16 19:57:18 +00008206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008207
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008208 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008209
8210 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008211 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008212 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008213
8214 /* Set to NULL in case of an error condition */
8215 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008216
Angus Grattond8213d02016-05-25 20:56:48 +10008217 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8218 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008219 {
Angus Grattond8213d02016-05-25 20:56:48 +10008220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008221 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008222 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008223 }
8224
8225 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8226 if( ssl->out_buf == NULL )
8227 {
8228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008229 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008230 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008231 }
8232
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008233 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008234
Paul Bakker48916f92012-09-16 19:57:18 +00008235 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008236 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008237
8238 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008239
8240error:
8241 mbedtls_free( ssl->in_buf );
8242 mbedtls_free( ssl->out_buf );
8243
8244 ssl->conf = NULL;
8245
8246 ssl->in_buf = NULL;
8247 ssl->out_buf = NULL;
8248
8249 ssl->in_hdr = NULL;
8250 ssl->in_ctr = NULL;
8251 ssl->in_len = NULL;
8252 ssl->in_iv = NULL;
8253 ssl->in_msg = NULL;
8254
8255 ssl->out_hdr = NULL;
8256 ssl->out_ctr = NULL;
8257 ssl->out_len = NULL;
8258 ssl->out_iv = NULL;
8259 ssl->out_msg = NULL;
8260
k-stachowiak9f7798e2018-07-31 16:52:32 +02008261 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008262}
8263
8264/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008265 * Reset an initialized and used SSL context for re-use while retaining
8266 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008267 *
8268 * If partial is non-zero, keep data in the input buffer and client ID.
8269 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008270 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008271static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008272{
Paul Bakker48916f92012-09-16 19:57:18 +00008273 int ret;
8274
Hanno Becker7e772132018-08-10 12:38:21 +01008275#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8276 !defined(MBEDTLS_SSL_SRV_C)
8277 ((void) partial);
8278#endif
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008281
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008282 /* Cancel any possibly running timer */
8283 ssl_set_timer( ssl, 0 );
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285#if defined(MBEDTLS_SSL_RENEGOTIATION)
8286 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008287 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008288
8289 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8291 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008294
Paul Bakker7eb013f2011-10-06 12:37:39 +00008295 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008296 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008297
8298 ssl->in_msgtype = 0;
8299 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008301 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008302 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008305 ssl_dtls_replay_reset( ssl );
8306#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008307
8308 ssl->in_hslen = 0;
8309 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008310
8311 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008312
8313 ssl->out_msgtype = 0;
8314 ssl->out_msglen = 0;
8315 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8317 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008318 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008319#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008320
Hanno Becker19859472018-08-06 09:40:20 +01008321 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8322
Paul Bakker48916f92012-09-16 19:57:18 +00008323 ssl->transform_in = NULL;
8324 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325
Hanno Becker78640902018-08-13 16:35:15 +01008326 ssl->session_in = NULL;
8327 ssl->session_out = NULL;
8328
Angus Grattond8213d02016-05-25 20:56:48 +10008329 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008330
8331#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008332 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008333#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8334 {
8335 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008336 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008337 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8340 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8343 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8346 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008347 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008348 }
8349#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008350
Paul Bakker48916f92012-09-16 19:57:18 +00008351 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 mbedtls_ssl_transform_free( ssl->transform );
8354 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008355 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008356 }
Paul Bakker48916f92012-09-16 19:57:18 +00008357
Paul Bakkerc0463502013-02-14 11:19:38 +01008358 if( ssl->session )
8359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 mbedtls_ssl_session_free( ssl->session );
8361 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008362 ssl->session = NULL;
8363 }
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008366 ssl->alpn_chosen = NULL;
8367#endif
8368
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008369#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008370#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008371 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008372#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008373 {
8374 mbedtls_free( ssl->cli_id );
8375 ssl->cli_id = NULL;
8376 ssl->cli_id_len = 0;
8377 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008378#endif
8379
Paul Bakker48916f92012-09-16 19:57:18 +00008380 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8381 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008382
8383 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008384}
8385
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008386/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008387 * Reset an initialized and used SSL context for re-use while retaining
8388 * all application-set variables, function pointers and data.
8389 */
8390int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8391{
8392 return( ssl_session_reset_int( ssl, 0 ) );
8393}
8394
8395/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008396 * SSL set accessors
8397 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008401}
8402
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008403void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008406}
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008409void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008411 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008412}
8413#endif
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008416void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008417{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008418 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008419}
8420#endif
8421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008423
Hanno Becker1841b0a2018-08-24 11:13:57 +01008424void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8425 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008426{
8427 ssl->disable_datagram_packing = !allow_packing;
8428}
8429
8430void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8431 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 conf->hs_timeout_min = min;
8434 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008435}
8436#endif
8437
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008438void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008441}
8442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008444void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008445 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008446 void *p_vrfy )
8447{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008448 conf->f_vrfy = f_vrfy;
8449 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008453void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008454 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008455 void *p_rng )
8456{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008457 conf->f_rng = f_rng;
8458 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008459}
8460
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008461void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008462 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008463 void *p_dbg )
8464{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008465 conf->f_dbg = f_dbg;
8466 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008467}
8468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008470 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008471 mbedtls_ssl_send_t *f_send,
8472 mbedtls_ssl_recv_t *f_recv,
8473 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008474{
8475 ssl->p_bio = p_bio;
8476 ssl->f_send = f_send;
8477 ssl->f_recv = f_recv;
8478 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008479}
8480
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008481#if defined(MBEDTLS_SSL_PROTO_DTLS)
8482void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8483{
8484 ssl->mtu = mtu;
8485}
8486#endif
8487
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008488void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008489{
8490 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008491}
8492
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008493void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8494 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008495 mbedtls_ssl_set_timer_t *f_set_timer,
8496 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008497{
8498 ssl->p_timer = p_timer;
8499 ssl->f_set_timer = f_set_timer;
8500 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008501
8502 /* Make sure we start with no timer running */
8503 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008504}
8505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008507void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008508 void *p_cache,
8509 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8510 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008511{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008512 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008513 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008514 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#if defined(MBEDTLS_SSL_CLI_C)
8519int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008520{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008521 int ret;
8522
8523 if( ssl == NULL ||
8524 session == NULL ||
8525 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008526 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008529 }
8530
Hanno Becker52055ae2019-02-06 14:30:46 +00008531 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8532 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008533 return( ret );
8534
Paul Bakker0a597072012-09-25 21:55:46 +00008535 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008536
8537 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008540
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008541void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008542 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008544 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8545 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8546 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8547 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008548}
8549
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008551 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008552 int major, int minor )
8553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008555 return;
8556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008558 return;
8559
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008560 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008561}
8562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008564void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008565 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008566{
8567 conf->cert_profile = profile;
8568}
8569
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008570/* Append a new keycert entry to a (possibly empty) list */
8571static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8572 mbedtls_x509_crt *cert,
8573 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008574{
niisato8ee24222018-06-25 19:05:48 +09008575 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008576
niisato8ee24222018-06-25 19:05:48 +09008577 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8578 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008579 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008580
niisato8ee24222018-06-25 19:05:48 +09008581 new_cert->cert = cert;
8582 new_cert->key = key;
8583 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008584
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008585 /* Update head is the list was null, else add to the end */
8586 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008587 {
niisato8ee24222018-06-25 19:05:48 +09008588 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008589 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008590 else
8591 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008592 mbedtls_ssl_key_cert *cur = *head;
8593 while( cur->next != NULL )
8594 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008595 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008596 }
8597
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008598 return( 0 );
8599}
8600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008601int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008602 mbedtls_x509_crt *own_cert,
8603 mbedtls_pk_context *pk_key )
8604{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008605 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008606}
8607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008608void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008609 mbedtls_x509_crt *ca_chain,
8610 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008611{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008612 conf->ca_chain = ca_chain;
8613 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008614
8615#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8616 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8617 * cannot be used together. */
8618 conf->f_ca_cb = NULL;
8619 conf->p_ca_cb = NULL;
8620#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008621}
Hanno Becker5adaad92019-03-27 16:54:37 +00008622
8623#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8624void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008625 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008626 void *p_ca_cb )
8627{
8628 conf->f_ca_cb = f_ca_cb;
8629 conf->p_ca_cb = p_ca_cb;
8630
8631 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8632 * cannot be used together. */
8633 conf->ca_chain = NULL;
8634 conf->ca_crl = NULL;
8635}
8636#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008638
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008639#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8640int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8641 mbedtls_x509_crt *own_cert,
8642 mbedtls_pk_context *pk_key )
8643{
8644 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8645 own_cert, pk_key ) );
8646}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008647
8648void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8649 mbedtls_x509_crt *ca_chain,
8650 mbedtls_x509_crl *ca_crl )
8651{
8652 ssl->handshake->sni_ca_chain = ca_chain;
8653 ssl->handshake->sni_ca_crl = ca_crl;
8654}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008655
8656void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8657 int authmode )
8658{
8659 ssl->handshake->sni_authmode = authmode;
8660}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008661#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8662
Hanno Becker8927c832019-04-03 12:52:50 +01008663#if defined(MBEDTLS_X509_CRT_PARSE_C)
8664void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8665 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8666 void *p_vrfy )
8667{
8668 ssl->f_vrfy = f_vrfy;
8669 ssl->p_vrfy = p_vrfy;
8670}
8671#endif
8672
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008673#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008674/*
8675 * Set EC J-PAKE password for current handshake
8676 */
8677int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8678 const unsigned char *pw,
8679 size_t pw_len )
8680{
8681 mbedtls_ecjpake_role role;
8682
Janos Follath8eb64132016-06-03 15:40:57 +01008683 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8685
8686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8687 role = MBEDTLS_ECJPAKE_SERVER;
8688 else
8689 role = MBEDTLS_ECJPAKE_CLIENT;
8690
8691 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8692 role,
8693 MBEDTLS_MD_SHA256,
8694 MBEDTLS_ECP_DP_SECP256R1,
8695 pw, pw_len ) );
8696}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008697#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008700
8701static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8702{
8703 /* Remove reference to existing PSK, if any. */
8704#if defined(MBEDTLS_USE_PSA_CRYPTO)
8705 if( conf->psk_opaque != 0 )
8706 {
8707 /* The maintenance of the PSK key slot is the
8708 * user's responsibility. */
8709 conf->psk_opaque = 0;
8710 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008711 /* This and the following branch should never
8712 * be taken simultaenously as we maintain the
8713 * invariant that raw and opaque PSKs are never
8714 * configured simultaneously. As a safeguard,
8715 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008716#endif /* MBEDTLS_USE_PSA_CRYPTO */
8717 if( conf->psk != NULL )
8718 {
8719 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8720
8721 mbedtls_free( conf->psk );
8722 conf->psk = NULL;
8723 conf->psk_len = 0;
8724 }
8725
8726 /* Remove reference to PSK identity, if any. */
8727 if( conf->psk_identity != NULL )
8728 {
8729 mbedtls_free( conf->psk_identity );
8730 conf->psk_identity = NULL;
8731 conf->psk_identity_len = 0;
8732 }
8733}
8734
Hanno Becker7390c712018-11-15 13:33:04 +00008735/* This function assumes that PSK identity in the SSL config is unset.
8736 * It checks that the provided identity is well-formed and attempts
8737 * to make a copy of it in the SSL config.
8738 * On failure, the PSK identity in the config remains unset. */
8739static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8740 unsigned char const *psk_identity,
8741 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008742{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008743 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008744 if( psk_identity == NULL ||
8745 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008746 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008747 {
8748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8749 }
8750
Hanno Becker7390c712018-11-15 13:33:04 +00008751 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8752 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008753 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008754
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008755 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008756 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008757
8758 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008759}
8760
Hanno Becker7390c712018-11-15 13:33:04 +00008761int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8762 const unsigned char *psk, size_t psk_len,
8763 const unsigned char *psk_identity, size_t psk_identity_len )
8764{
8765 int ret;
8766 /* Remove opaque/raw PSK + PSK Identity */
8767 ssl_conf_remove_psk( conf );
8768
8769 /* Check and set raw PSK */
8770 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8772 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8773 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8774 conf->psk_len = psk_len;
8775 memcpy( conf->psk, psk, conf->psk_len );
8776
8777 /* Check and set PSK Identity */
8778 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8779 if( ret != 0 )
8780 ssl_conf_remove_psk( conf );
8781
8782 return( ret );
8783}
8784
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008785static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8786{
8787#if defined(MBEDTLS_USE_PSA_CRYPTO)
8788 if( ssl->handshake->psk_opaque != 0 )
8789 {
8790 ssl->handshake->psk_opaque = 0;
8791 }
8792 else
8793#endif /* MBEDTLS_USE_PSA_CRYPTO */
8794 if( ssl->handshake->psk != NULL )
8795 {
8796 mbedtls_platform_zeroize( ssl->handshake->psk,
8797 ssl->handshake->psk_len );
8798 mbedtls_free( ssl->handshake->psk );
8799 ssl->handshake->psk_len = 0;
8800 }
8801}
8802
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008803int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8804 const unsigned char *psk, size_t psk_len )
8805{
8806 if( psk == NULL || ssl->handshake == NULL )
8807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8808
8809 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8811
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008812 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008813
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008814 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008815 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008816
8817 ssl->handshake->psk_len = psk_len;
8818 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8819
8820 return( 0 );
8821}
8822
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008823#if defined(MBEDTLS_USE_PSA_CRYPTO)
8824int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008825 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008826 const unsigned char *psk_identity,
8827 size_t psk_identity_len )
8828{
Hanno Becker7390c712018-11-15 13:33:04 +00008829 int ret;
8830 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008831 ssl_conf_remove_psk( conf );
8832
Hanno Becker7390c712018-11-15 13:33:04 +00008833 /* Check and set opaque PSK */
8834 if( psk_slot == 0 )
8835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008836 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008837
8838 /* Check and set PSK Identity */
8839 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8840 psk_identity_len );
8841 if( ret != 0 )
8842 ssl_conf_remove_psk( conf );
8843
8844 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008845}
8846
8847int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008848 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008849{
8850 if( psk_slot == 0 || ssl->handshake == NULL )
8851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8852
8853 ssl_remove_psk( ssl );
8854 ssl->handshake->psk_opaque = psk_slot;
8855 return( 0 );
8856}
8857#endif /* MBEDTLS_USE_PSA_CRYPTO */
8858
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008859void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008861 size_t),
8862 void *p_psk )
8863{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008864 conf->f_psk = f_psk;
8865 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008866}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008868
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008869#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008870
8871#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008872int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008873{
8874 int ret;
8875
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008876 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8877 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8878 {
8879 mbedtls_mpi_free( &conf->dhm_P );
8880 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008881 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008883
8884 return( 0 );
8885}
Hanno Becker470a8c42017-10-04 15:28:46 +01008886#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008887
Hanno Beckera90658f2017-10-04 15:29:08 +01008888int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8889 const unsigned char *dhm_P, size_t P_len,
8890 const unsigned char *dhm_G, size_t G_len )
8891{
8892 int ret;
8893
8894 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8895 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8896 {
8897 mbedtls_mpi_free( &conf->dhm_P );
8898 mbedtls_mpi_free( &conf->dhm_G );
8899 return( ret );
8900 }
8901
8902 return( 0 );
8903}
Paul Bakker5121ce52009-01-03 21:22:43 +00008904
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008905int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008906{
8907 int ret;
8908
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008909 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8910 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8911 {
8912 mbedtls_mpi_free( &conf->dhm_P );
8913 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008914 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008915 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008916
8917 return( 0 );
8918}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008919#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008920
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008921#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8922/*
8923 * Set the minimum length for Diffie-Hellman parameters
8924 */
8925void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8926 unsigned int bitlen )
8927{
8928 conf->dhm_min_bitlen = bitlen;
8929}
8930#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8931
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008932#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008933/*
8934 * Set allowed/preferred hashes for handshake signatures
8935 */
8936void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8937 const int *hashes )
8938{
8939 conf->sig_hashes = hashes;
8940}
Hanno Becker947194e2017-04-07 13:25:49 +01008941#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008942
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008943#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008944/*
8945 * Set the allowed elliptic curves
8946 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008947void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008948 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008949{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008950 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008951}
Hanno Becker947194e2017-04-07 13:25:49 +01008952#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008953
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008954#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008956{
Hanno Becker947194e2017-04-07 13:25:49 +01008957 /* Initialize to suppress unnecessary compiler warning */
8958 size_t hostname_len = 0;
8959
8960 /* Check if new hostname is valid before
8961 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008962 if( hostname != NULL )
8963 {
8964 hostname_len = strlen( hostname );
8965
8966 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8968 }
8969
8970 /* Now it's clear that we will overwrite the old hostname,
8971 * so we can free it safely */
8972
8973 if( ssl->hostname != NULL )
8974 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008975 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008976 mbedtls_free( ssl->hostname );
8977 }
8978
8979 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008980
Paul Bakker5121ce52009-01-03 21:22:43 +00008981 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008982 {
8983 ssl->hostname = NULL;
8984 }
8985 else
8986 {
8987 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008988 if( ssl->hostname == NULL )
8989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008990
Hanno Becker947194e2017-04-07 13:25:49 +01008991 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008992
Hanno Becker947194e2017-04-07 13:25:49 +01008993 ssl->hostname[hostname_len] = '\0';
8994 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008995
8996 return( 0 );
8997}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008998#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008999
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009000#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009001void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009003 const unsigned char *, size_t),
9004 void *p_sni )
9005{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009006 conf->f_sni = f_sni;
9007 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009008}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009012int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009014 size_t cur_len, tot_len;
9015 const char **p;
9016
9017 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009018 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9019 * MUST NOT be truncated."
9020 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009021 */
9022 tot_len = 0;
9023 for( p = protos; *p != NULL; p++ )
9024 {
9025 cur_len = strlen( *p );
9026 tot_len += cur_len;
9027
9028 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009030 }
9031
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009032 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009033
9034 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009035}
9036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009038{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009039 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009042
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009043void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009044{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009045 conf->max_major_ver = major;
9046 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009047}
9048
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009049void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009051 conf->min_major_ver = major;
9052 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009053}
9054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009056void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009057{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009058 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009059}
9060#endif
9061
Janos Follath088ce432017-04-10 12:42:31 +01009062#if defined(MBEDTLS_SSL_SRV_C)
9063void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9064 char cert_req_ca_list )
9065{
9066 conf->cert_req_ca_list = cert_req_ca_list;
9067}
9068#endif
9069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009071void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009072{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009073 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009074}
9075#endif
9076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009078void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009080 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009081}
9082#endif
9083
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009084#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009087 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009088}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009089#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009092int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009095 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009098 }
9099
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009100 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009101
9102 return( 0 );
9103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009107void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009109 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009114void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009115{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009116 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009117}
9118#endif
9119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009120void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009122 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009123}
9124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009126void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009128 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009129}
9130
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009131void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009132{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009133 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009134}
9135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009136void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009137 const unsigned char period[8] )
9138{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009139 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009144#if defined(MBEDTLS_SSL_CLI_C)
9145void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009146{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009147 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009148}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009149#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009150
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009151#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009152void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9153 mbedtls_ssl_ticket_write_t *f_ticket_write,
9154 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9155 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009156{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009157 conf->f_ticket_write = f_ticket_write;
9158 conf->f_ticket_parse = f_ticket_parse;
9159 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009160}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009163
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009164#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9165void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9166 mbedtls_ssl_export_keys_t *f_export_keys,
9167 void *p_export_keys )
9168{
9169 conf->f_export_keys = f_export_keys;
9170 conf->p_export_keys = p_export_keys;
9171}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009172
9173void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9174 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9175 void *p_export_keys )
9176{
9177 conf->f_export_keys_ext = f_export_keys_ext;
9178 conf->p_export_keys = p_export_keys;
9179}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009180#endif
9181
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009182#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009183void mbedtls_ssl_conf_async_private_cb(
9184 mbedtls_ssl_config *conf,
9185 mbedtls_ssl_async_sign_t *f_async_sign,
9186 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9187 mbedtls_ssl_async_resume_t *f_async_resume,
9188 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009189 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009190{
9191 conf->f_async_sign_start = f_async_sign;
9192 conf->f_async_decrypt_start = f_async_decrypt;
9193 conf->f_async_resume = f_async_resume;
9194 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009195 conf->p_async_config_data = async_config_data;
9196}
9197
Gilles Peskine8f97af72018-04-26 11:46:10 +02009198void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9199{
9200 return( conf->p_async_config_data );
9201}
9202
Gilles Peskine1febfef2018-04-30 11:54:39 +02009203void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009204{
9205 if( ssl->handshake == NULL )
9206 return( NULL );
9207 else
9208 return( ssl->handshake->user_async_ctx );
9209}
9210
Gilles Peskine1febfef2018-04-30 11:54:39 +02009211void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009212 void *ctx )
9213{
9214 if( ssl->handshake != NULL )
9215 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009216}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009217#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009218
Paul Bakker5121ce52009-01-03 21:22:43 +00009219/*
9220 * SSL get accessors
9221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009223{
9224 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9225}
9226
Hanno Becker8b170a02017-10-10 11:51:19 +01009227int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9228{
9229 /*
9230 * Case A: We're currently holding back
9231 * a message for further processing.
9232 */
9233
9234 if( ssl->keep_current_message == 1 )
9235 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 return( 1 );
9238 }
9239
9240 /*
9241 * Case B: Further records are pending in the current datagram.
9242 */
9243
9244#if defined(MBEDTLS_SSL_PROTO_DTLS)
9245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9246 ssl->in_left > ssl->next_record_offset )
9247 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009249 return( 1 );
9250 }
9251#endif /* MBEDTLS_SSL_PROTO_DTLS */
9252
9253 /*
9254 * Case C: A handshake message is being processed.
9255 */
9256
Hanno Becker8b170a02017-10-10 11:51:19 +01009257 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9258 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009260 return( 1 );
9261 }
9262
9263 /*
9264 * Case D: An application data message is being processed
9265 */
9266 if( ssl->in_offt != NULL )
9267 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009269 return( 1 );
9270 }
9271
9272 /*
9273 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009274 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009275 * we implement support for multiple alerts in single records.
9276 */
9277
9278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9279 return( 0 );
9280}
9281
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009282uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009283{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009284 if( ssl->session != NULL )
9285 return( ssl->session->verify_result );
9286
9287 if( ssl->session_negotiate != NULL )
9288 return( ssl->session_negotiate->verify_result );
9289
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009290 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009291}
9292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009294{
Paul Bakker926c8e42013-03-06 10:23:34 +01009295 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009296 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009299}
9300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009305 {
9306 switch( ssl->minor_ver )
9307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009309 return( "DTLSv1.0" );
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009312 return( "DTLSv1.2" );
9313
9314 default:
9315 return( "unknown (DTLS)" );
9316 }
9317 }
9318#endif
9319
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320 switch( ssl->minor_ver )
9321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009323 return( "SSLv3.0" );
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326 return( "TLSv1.0" );
9327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329 return( "TLSv1.1" );
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009332 return( "TLSv1.2" );
9333
Paul Bakker43ca69c2011-01-15 17:35:19 +00009334 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009335 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009336 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009337}
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009340{
Hanno Becker3136ede2018-08-17 15:28:19 +01009341 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009343 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009344
Hanno Becker5903de42019-05-03 14:46:38 +01009345 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9346
Hanno Becker78640902018-08-13 16:35:15 +01009347 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009348 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_ZLIB_SUPPORT)
9351 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9352 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009353#endif
9354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 case MBEDTLS_MODE_GCM:
9358 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009359 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009361 transform_expansion = transform->minlen;
9362 break;
9363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009365
9366 block_size = mbedtls_cipher_get_block_size(
9367 &transform->cipher_ctx_enc );
9368
Hanno Becker3136ede2018-08-17 15:28:19 +01009369 /* Expansion due to the addition of the MAC. */
9370 transform_expansion += transform->maclen;
9371
9372 /* Expansion due to the addition of CBC padding;
9373 * Theoretically up to 256 bytes, but we never use
9374 * more than the block size of the underlying cipher. */
9375 transform_expansion += block_size;
9376
9377 /* For TLS 1.1 or higher, an explicit IV is added
9378 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009379#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9380 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009381 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009382#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009383
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009384 break;
9385
9386 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009389 }
9390
Hanno Beckera0e20d02019-05-15 14:03:01 +01009391#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009392 if( transform->out_cid_len != 0 )
9393 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009394#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009395
Hanno Becker5903de42019-05-03 14:46:38 +01009396 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009397}
9398
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009399#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9400size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9401{
9402 size_t max_len;
9403
9404 /*
9405 * Assume mfl_code is correct since it was checked when set
9406 */
Angus Grattond8213d02016-05-25 20:56:48 +10009407 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009408
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009409 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009410 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009411 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009412 {
Angus Grattond8213d02016-05-25 20:56:48 +10009413 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009414 }
9415
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009416 /* During a handshake, use the value being negotiated */
9417 if( ssl->session_negotiate != NULL &&
9418 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9419 {
9420 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9421 }
9422
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009423 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009424}
9425#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9426
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009427#if defined(MBEDTLS_SSL_PROTO_DTLS)
9428static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9429{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009430 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9432 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9433 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9434 return ( 0 );
9435
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009436 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9437 return( ssl->mtu );
9438
9439 if( ssl->mtu == 0 )
9440 return( ssl->handshake->mtu );
9441
9442 return( ssl->mtu < ssl->handshake->mtu ?
9443 ssl->mtu : ssl->handshake->mtu );
9444}
9445#endif /* MBEDTLS_SSL_PROTO_DTLS */
9446
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009447int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9448{
9449 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9450
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009451#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9452 !defined(MBEDTLS_SSL_PROTO_DTLS)
9453 (void) ssl;
9454#endif
9455
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009456#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9457 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9458
9459 if( max_len > mfl )
9460 max_len = mfl;
9461#endif
9462
9463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009464 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009465 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009466 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009467 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9468 const size_t overhead = (size_t) ret;
9469
9470 if( ret < 0 )
9471 return( ret );
9472
9473 if( mtu <= overhead )
9474 {
9475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9476 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9477 }
9478
9479 if( max_len > mtu - overhead )
9480 max_len = mtu - overhead;
9481 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009483
Hanno Becker0defedb2018-08-10 12:35:02 +01009484#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9485 !defined(MBEDTLS_SSL_PROTO_DTLS)
9486 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009487#endif
9488
9489 return( (int) max_len );
9490}
9491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_X509_CRT_PARSE_C)
9493const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009494{
9495 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009496 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009497
Hanno Beckere6824572019-02-07 13:18:46 +00009498#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009499 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009500#else
9501 return( NULL );
9502#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009507int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9508 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009509{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009510 if( ssl == NULL ||
9511 dst == NULL ||
9512 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009513 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009516 }
9517
Hanno Becker52055ae2019-02-06 14:30:46 +00009518 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009521
Paul Bakker5121ce52009-01-03 21:22:43 +00009522/*
Paul Bakker1961b702013-01-25 14:49:24 +01009523 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009528
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009529 if( ssl == NULL || ssl->conf == NULL )
9530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009533 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009539#endif
9540
Paul Bakker1961b702013-01-25 14:49:24 +01009541 return( ret );
9542}
9543
9544/*
9545 * Perform the SSL handshake
9546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009548{
9549 int ret = 0;
9550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009551 if( ssl == NULL || ssl->conf == NULL )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009559
9560 if( ret != 0 )
9561 break;
9562 }
9563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009565
9566 return( ret );
9567}
9568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569#if defined(MBEDTLS_SSL_RENEGOTIATION)
9570#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009571/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009572 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009575{
9576 int ret;
9577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009579
9580 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9582 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009583
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009584 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009585 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009587 return( ret );
9588 }
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009591
9592 return( 0 );
9593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009595
9596/*
9597 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 * - any side: calling mbedtls_ssl_renegotiate(),
9599 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9600 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009601 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009602 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009606{
9607 int ret;
9608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009610
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009611 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9612 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009613
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009614 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9615 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009619 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009620 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009621 ssl->handshake->out_msg_seq = 1;
9622 else
9623 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009624 }
9625#endif
9626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9628 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009633 return( ret );
9634 }
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009637
9638 return( 0 );
9639}
9640
9641/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009642 * Renegotiate current connection on client,
9643 * or request renegotiation on server
9644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009648
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009649 if( ssl == NULL || ssl->conf == NULL )
9650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009653 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009654 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009660
9661 /* Did we already try/start sending HelloRequest? */
9662 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009664
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009665 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009666 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670 /*
9671 * On client, either start the renegotiation process or,
9672 * if already in progress, continue the handshake
9673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009678
9679 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682 return( ret );
9683 }
9684 }
9685 else
9686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009690 return( ret );
9691 }
9692 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009694
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009695 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009696}
9697
9698/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009699 * Check record counters and renegotiate if they're above the limit.
9700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009702{
Andres AG2196c7f2016-12-15 17:01:16 +00009703 size_t ep_len = ssl_ep_len( ssl );
9704 int in_ctr_cmp;
9705 int out_ctr_cmp;
9706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9708 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009709 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009710 {
9711 return( 0 );
9712 }
9713
Andres AG2196c7f2016-12-15 17:01:16 +00009714 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9715 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009716 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009717 ssl->conf->renego_period + ep_len, 8 - ep_len );
9718
9719 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009720 {
9721 return( 0 );
9722 }
9723
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009728
9729/*
9730 * Receive application data decrypted from the SSL layer
9731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009733{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009734 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009735 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009736
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009737 if( ssl == NULL || ssl->conf == NULL )
9738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009746 return( ret );
9747
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009748 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009750 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009751 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009752 return( ret );
9753 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009754 }
9755#endif
9756
Hanno Becker4a810fb2017-05-24 16:27:30 +01009757 /*
9758 * Check if renegotiation is necessary and/or handshake is
9759 * in process. If yes, perform/continue, and fall through
9760 * if an unexpected packet is received while the client
9761 * is waiting for the ServerHello.
9762 *
9763 * (There is no equivalent to the last condition on
9764 * the server-side as it is not treated as within
9765 * a handshake while waiting for the ClientHello
9766 * after a renegotiation request.)
9767 */
9768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009770 ret = ssl_check_ctr_renegotiate( ssl );
9771 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9772 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009775 return( ret );
9776 }
9777#endif
9778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009782 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9783 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009786 return( ret );
9787 }
9788 }
9789
Hanno Beckere41158b2017-10-23 13:30:32 +01009790 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009791 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009792 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009793 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009794 if( ssl->f_get_timer != NULL &&
9795 ssl->f_get_timer( ssl->p_timer ) == -1 )
9796 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009797 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009798 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009799
Hanno Becker327c93b2018-08-15 13:56:18 +01009800 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009801 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009802 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9803 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009804
Hanno Becker4a810fb2017-05-24 16:27:30 +01009805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9806 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009807 }
9808
9809 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009811 {
9812 /*
9813 * OpenSSL sends empty messages to randomize the IV
9814 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009815 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009818 return( 0 );
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 return( ret );
9822 }
9823 }
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009828
Hanno Becker4a810fb2017-05-24 16:27:30 +01009829 /*
9830 * - For client-side, expect SERVER_HELLO_REQUEST.
9831 * - For server-side, expect CLIENT_HELLO.
9832 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9833 */
9834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009838 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009841
9842 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009844 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009845 {
9846 continue;
9847 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009850 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009851#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009852
Hanno Becker4a810fb2017-05-24 16:27:30 +01009853#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009854 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009858
9859 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009862 {
9863 continue;
9864 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009867 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009868#endif /* MBEDTLS_SSL_SRV_C */
9869
Hanno Becker21df7f92017-10-17 11:03:26 +01009870#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009872 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9873 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9874 ssl->conf->allow_legacy_renegotiation ==
9875 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9876 {
9877 /*
9878 * Accept renegotiation request
9879 */
Paul Bakker48916f92012-09-16 19:57:18 +00009880
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009881 /* DTLS clients need to know renego is server-initiated */
9882#if defined(MBEDTLS_SSL_PROTO_DTLS)
9883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9884 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9885 {
9886 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9887 }
9888#endif
9889 ret = ssl_start_renegotiation( ssl );
9890 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9891 ret != 0 )
9892 {
9893 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9894 return( ret );
9895 }
9896 }
9897 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009898#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009899 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009900 /*
9901 * Refuse renegotiation
9902 */
9903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906#if defined(MBEDTLS_SSL_PROTO_SSL3)
9907 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009908 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009909 /* SSLv3 does not have a "no_renegotiation" warning, so
9910 we send a fatal alert and abort the connection. */
9911 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9913 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009914 }
9915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9917#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9918 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9919 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9922 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9923 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009924 {
9925 return( ret );
9926 }
Paul Bakker48916f92012-09-16 19:57:18 +00009927 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009928 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9930 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009934 }
Paul Bakker48916f92012-09-16 19:57:18 +00009935 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009936
Hanno Becker90333da2017-10-10 11:27:13 +01009937 /* At this point, we don't know whether the renegotiation has been
9938 * completed or not. The cases to consider are the following:
9939 * 1) The renegotiation is complete. In this case, no new record
9940 * has been read yet.
9941 * 2) The renegotiation is incomplete because the client received
9942 * an application data record while awaiting the ServerHello.
9943 * 3) The renegotiation is incomplete because the client received
9944 * a non-handshake, non-application data message while awaiting
9945 * the ServerHello.
9946 * In each of these case, looping will be the proper action:
9947 * - For 1), the next iteration will read a new record and check
9948 * if it's application data.
9949 * - For 2), the loop condition isn't satisfied as application data
9950 * is present, hence continue is the same as break
9951 * - For 3), the loop condition is satisfied and read_record
9952 * will re-deliver the message that was held back by the client
9953 * when expecting the ServerHello.
9954 */
9955 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009956 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009957#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009959 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009960 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009961 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009962 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009965 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009967 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009968 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009969 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9973 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009976 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009977 }
9978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9982 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009983 }
9984
9985 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009986
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009987 /* We're going to return something now, cancel timer,
9988 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009990 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009991
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009993 /* If we requested renego but received AppData, resend HelloRequest.
9994 * Do it now, after setting in_offt, to avoid taking this branch
9995 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009997 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009999 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010000 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010003 return( ret );
10004 }
10005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010007#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010008 }
10009
10010 n = ( len < ssl->in_msglen )
10011 ? len : ssl->in_msglen;
10012
10013 memcpy( buf, ssl->in_offt, n );
10014 ssl->in_msglen -= n;
10015
10016 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010017 {
10018 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010019 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010020 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010022 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010023 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 /* more data available */
10025 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010026 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010029
Paul Bakker23986e52011-04-24 08:57:21 +000010030 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010031}
10032
10033/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010034 * Send application data to be encrypted by the SSL layer, taking care of max
10035 * fragment length and buffer size.
10036 *
10037 * According to RFC 5246 Section 6.2.1:
10038 *
10039 * Zero-length fragments of Application data MAY be sent as they are
10040 * potentially useful as a traffic analysis countermeasure.
10041 *
10042 * Therefore, it is possible that the input message length is 0 and the
10043 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010044 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010045static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010046 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010047{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010048 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10049 const size_t max_len = (size_t) ret;
10050
10051 if( ret < 0 )
10052 {
10053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10054 return( ret );
10055 }
10056
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010057 if( len > max_len )
10058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010063 "maximum fragment length: %d > %d",
10064 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010066 }
10067 else
10068#endif
10069 len = max_len;
10070 }
Paul Bakker887bd502011-06-08 13:10:54 +000010071
Paul Bakker5121ce52009-01-03 21:22:43 +000010072 if( ssl->out_left != 0 )
10073 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010074 /*
10075 * The user has previously tried to send the data and
10076 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10077 * written. In this case, we expect the high-level write function
10078 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010083 return( ret );
10084 }
10085 }
Paul Bakker887bd502011-06-08 13:10:54 +000010086 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010087 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010088 /*
10089 * The user is trying to send a message the first time, so we need to
10090 * copy the data into the internal buffers and setup the data structure
10091 * to keep track of partial writes
10092 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010093 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010095 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010096
Hanno Becker67bc7c32018-08-06 11:33:50 +010010097 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010100 return( ret );
10101 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010102 }
10103
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010104 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010105}
10106
10107/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010108 * Write application data, doing 1/n-1 splitting if necessary.
10109 *
10110 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010111 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010112 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010115static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010116 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010117{
10118 int ret;
10119
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010120 if( ssl->conf->cbc_record_splitting ==
10121 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010122 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10124 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10125 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010126 {
10127 return( ssl_write_real( ssl, buf, len ) );
10128 }
10129
10130 if( ssl->split_done == 0 )
10131 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010132 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010133 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010134 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010135 }
10136
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010137 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10138 return( ret );
10139 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010140
10141 return( ret + 1 );
10142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010144
10145/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010146 * Write application data (public-facing wrapper)
10147 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010148int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010149{
10150 int ret;
10151
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010153
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010154 if( ssl == NULL || ssl->conf == NULL )
10155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10156
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010157#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010158 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10159 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010160 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010161 return( ret );
10162 }
10163#endif
10164
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010165 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010166 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010167 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010168 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010170 return( ret );
10171 }
10172 }
10173
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010174#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010175 ret = ssl_write_split( ssl, buf, len );
10176#else
10177 ret = ssl_write_real( ssl, buf, len );
10178#endif
10179
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010181
10182 return( ret );
10183}
10184
10185/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010186 * Notify the peer that the connection is being closed
10187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010189{
10190 int ret;
10191
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010192 if( ssl == NULL || ssl->conf == NULL )
10193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010197 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10203 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10204 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 return( ret );
10208 }
10209 }
10210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010212
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010213 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010214}
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010217{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010218 if( transform == NULL )
10219 return;
10220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010222 deflateEnd( &transform->ctx_deflate );
10223 inflateEnd( &transform->ctx_inflate );
10224#endif
10225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10227 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010228
Hanno Beckerd56ed242018-01-03 15:32:51 +000010229#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 mbedtls_md_free( &transform->md_ctx_enc );
10231 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010232#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010233
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010234 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010235}
10236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#if defined(MBEDTLS_X509_CRT_PARSE_C)
10238static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010241
10242 while( cur != NULL )
10243 {
10244 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010246 cur = next;
10247 }
10248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010250
Hanno Becker0271f962018-08-16 13:23:47 +010010251#if defined(MBEDTLS_SSL_PROTO_DTLS)
10252
10253static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10254{
10255 unsigned offset;
10256 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10257
10258 if( hs == NULL )
10259 return;
10260
Hanno Becker283f5ef2018-08-24 09:34:47 +010010261 ssl_free_buffered_record( ssl );
10262
Hanno Becker0271f962018-08-16 13:23:47 +010010263 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010264 ssl_buffering_free_slot( ssl, offset );
10265}
10266
10267static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10268 uint8_t slot )
10269{
10270 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10271 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010272
10273 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10274 return;
10275
Hanno Beckere605b192018-08-21 15:59:07 +010010276 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010277 {
Hanno Beckere605b192018-08-21 15:59:07 +010010278 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010279 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010280 mbedtls_free( hs_buf->data );
10281 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010282 }
10283}
10284
10285#endif /* MBEDTLS_SSL_PROTO_DTLS */
10286
Gilles Peskine9b562d52018-04-25 20:32:43 +020010287void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010288{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010289 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10290
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010291 if( handshake == NULL )
10292 return;
10293
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010294#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10295 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10296 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010297 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010298 handshake->async_in_progress = 0;
10299 }
10300#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10301
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010302#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10303 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10304 mbedtls_md5_free( &handshake->fin_md5 );
10305 mbedtls_sha1_free( &handshake->fin_sha1 );
10306#endif
10307#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10308#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010309#if defined(MBEDTLS_USE_PSA_CRYPTO)
10310 psa_hash_abort( &handshake->fin_sha256_psa );
10311#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010312 mbedtls_sha256_free( &handshake->fin_sha256 );
10313#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010314#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010315#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010316#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010317 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010318#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010319 mbedtls_sha512_free( &handshake->fin_sha512 );
10320#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010321#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010322#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324#if defined(MBEDTLS_DHM_C)
10325 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327#if defined(MBEDTLS_ECDH_C)
10328 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010329#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010330#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010331 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010332#if defined(MBEDTLS_SSL_CLI_C)
10333 mbedtls_free( handshake->ecjpake_cache );
10334 handshake->ecjpake_cache = NULL;
10335 handshake->ecjpake_cache_len = 0;
10336#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010337#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010338
Janos Follath4ae5c292016-02-10 11:27:43 +000010339#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10340 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010341 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010343#endif
10344
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010345#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10346 if( handshake->psk != NULL )
10347 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010348 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010349 mbedtls_free( handshake->psk );
10350 }
10351#endif
10352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10354 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010355 /*
10356 * Free only the linked list wrapper, not the keys themselves
10357 * since the belong to the SNI callback
10358 */
10359 if( handshake->sni_key_cert != NULL )
10360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010362
10363 while( cur != NULL )
10364 {
10365 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010367 cur = next;
10368 }
10369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010371
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010372#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010373 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010374 if( handshake->ecrs_peer_cert != NULL )
10375 {
10376 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10377 mbedtls_free( handshake->ecrs_peer_cert );
10378 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010379#endif
10380
Hanno Becker75173122019-02-06 16:18:31 +000010381#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10382 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10383 mbedtls_pk_free( &handshake->peer_pubkey );
10384#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386#if defined(MBEDTLS_SSL_PROTO_DTLS)
10387 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010388 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010389 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010390#endif
10391
Hanno Becker4a63ed42019-01-08 11:39:35 +000010392#if defined(MBEDTLS_ECDH_C) && \
10393 defined(MBEDTLS_USE_PSA_CRYPTO)
10394 psa_destroy_key( handshake->ecdh_psa_privkey );
10395#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10396
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010397 mbedtls_platform_zeroize( handshake,
10398 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010399}
10400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010402{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010403 if( session == NULL )
10404 return;
10405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010407 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010408#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010409
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010410#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010412#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010413
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010414 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010415}
10416
Paul Bakker5121ce52009-01-03 21:22:43 +000010417/*
10418 * Free an SSL context
10419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010421{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010422 if( ssl == NULL )
10423 return;
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010426
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010427 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010428 {
Angus Grattond8213d02016-05-25 20:56:48 +100010429 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010431 }
10432
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010433 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010434 {
Angus Grattond8213d02016-05-25 20:56:48 +100010435 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010437 }
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010440 if( ssl->compress_buf != NULL )
10441 {
Angus Grattond8213d02016-05-25 20:56:48 +100010442 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010444 }
10445#endif
10446
Paul Bakker48916f92012-09-16 19:57:18 +000010447 if( ssl->transform )
10448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010449 mbedtls_ssl_transform_free( ssl->transform );
10450 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010451 }
10452
10453 if( ssl->handshake )
10454 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010455 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10457 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 mbedtls_free( ssl->handshake );
10460 mbedtls_free( ssl->transform_negotiate );
10461 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010462 }
10463
Paul Bakkerc0463502013-02-14 11:19:38 +010010464 if( ssl->session )
10465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 mbedtls_ssl_session_free( ssl->session );
10467 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010468 }
10469
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010470#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010471 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010472 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010473 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10479 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10482 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010483 }
10484#endif
10485
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010486#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010488#endif
10489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010491
Paul Bakker86f04f42013-02-14 11:20:09 +010010492 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010493 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010494}
10495
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010496/*
10497 * Initialze mbedtls_ssl_config
10498 */
10499void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10500{
10501 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10502}
10503
Simon Butcherc97b6972015-12-27 23:48:17 +000010504#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010505static int ssl_preset_default_hashes[] = {
10506#if defined(MBEDTLS_SHA512_C)
10507 MBEDTLS_MD_SHA512,
10508 MBEDTLS_MD_SHA384,
10509#endif
10510#if defined(MBEDTLS_SHA256_C)
10511 MBEDTLS_MD_SHA256,
10512 MBEDTLS_MD_SHA224,
10513#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010514#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010515 MBEDTLS_MD_SHA1,
10516#endif
10517 MBEDTLS_MD_NONE
10518};
Simon Butcherc97b6972015-12-27 23:48:17 +000010519#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010520
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010521static int ssl_preset_suiteb_ciphersuites[] = {
10522 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10523 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10524 0
10525};
10526
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010527#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010528static int ssl_preset_suiteb_hashes[] = {
10529 MBEDTLS_MD_SHA256,
10530 MBEDTLS_MD_SHA384,
10531 MBEDTLS_MD_NONE
10532};
10533#endif
10534
10535#if defined(MBEDTLS_ECP_C)
10536static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10537 MBEDTLS_ECP_DP_SECP256R1,
10538 MBEDTLS_ECP_DP_SECP384R1,
10539 MBEDTLS_ECP_DP_NONE
10540};
10541#endif
10542
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010543/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010544 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010545 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010546int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010547 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010548{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010549#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010550 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010551#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010552
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010553 /* Use the functions here so that they are covered in tests,
10554 * but otherwise access member directly for efficiency */
10555 mbedtls_ssl_conf_endpoint( conf, endpoint );
10556 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010557
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010558 /*
10559 * Things that are common to all presets
10560 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010561#if defined(MBEDTLS_SSL_CLI_C)
10562 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10563 {
10564 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10565#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10566 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10567#endif
10568 }
10569#endif
10570
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010571#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010572 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010573#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010574
10575#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10576 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10577#endif
10578
10579#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10580 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10581#endif
10582
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010583#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10584 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10585#endif
10586
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010587#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010588 conf->f_cookie_write = ssl_cookie_write_dummy;
10589 conf->f_cookie_check = ssl_cookie_check_dummy;
10590#endif
10591
10592#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10593 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10594#endif
10595
Janos Follath088ce432017-04-10 12:42:31 +010010596#if defined(MBEDTLS_SSL_SRV_C)
10597 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10598#endif
10599
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010600#if defined(MBEDTLS_SSL_PROTO_DTLS)
10601 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10602 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10603#endif
10604
10605#if defined(MBEDTLS_SSL_RENEGOTIATION)
10606 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010607 memset( conf->renego_period, 0x00, 2 );
10608 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010609#endif
10610
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010611#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10612 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10613 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010614 const unsigned char dhm_p[] =
10615 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10616 const unsigned char dhm_g[] =
10617 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10618
Hanno Beckera90658f2017-10-04 15:29:08 +010010619 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10620 dhm_p, sizeof( dhm_p ),
10621 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010622 {
10623 return( ret );
10624 }
10625 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010626#endif
10627
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010628 /*
10629 * Preset-specific defaults
10630 */
10631 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010632 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010633 /*
10634 * NSA Suite B
10635 */
10636 case MBEDTLS_SSL_PRESET_SUITEB:
10637 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10638 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10639 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10640 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10641
10642 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10643 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10644 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10645 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10646 ssl_preset_suiteb_ciphersuites;
10647
10648#if defined(MBEDTLS_X509_CRT_PARSE_C)
10649 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010650#endif
10651
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010652#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010653 conf->sig_hashes = ssl_preset_suiteb_hashes;
10654#endif
10655
10656#if defined(MBEDTLS_ECP_C)
10657 conf->curve_list = ssl_preset_suiteb_curves;
10658#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010659 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010660
10661 /*
10662 * Default
10663 */
10664 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010665 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10666 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10667 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10668 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10669 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10670 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10671 MBEDTLS_SSL_MIN_MINOR_VERSION :
10672 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010673 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10674 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10675
10676#if defined(MBEDTLS_SSL_PROTO_DTLS)
10677 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10678 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10679#endif
10680
10681 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10682 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10684 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10685 mbedtls_ssl_list_ciphersuites();
10686
10687#if defined(MBEDTLS_X509_CRT_PARSE_C)
10688 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10689#endif
10690
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010691#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010692 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010693#endif
10694
10695#if defined(MBEDTLS_ECP_C)
10696 conf->curve_list = mbedtls_ecp_grp_id_list();
10697#endif
10698
10699#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10700 conf->dhm_min_bitlen = 1024;
10701#endif
10702 }
10703
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010704 return( 0 );
10705}
10706
10707/*
10708 * Free mbedtls_ssl_config
10709 */
10710void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10711{
10712#if defined(MBEDTLS_DHM_C)
10713 mbedtls_mpi_free( &conf->dhm_P );
10714 mbedtls_mpi_free( &conf->dhm_G );
10715#endif
10716
10717#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10718 if( conf->psk != NULL )
10719 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010720 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010721 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010722 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010724 }
10725
10726 if( conf->psk_identity != NULL )
10727 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010728 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010729 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010730 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010731 conf->psk_identity_len = 0;
10732 }
10733#endif
10734
10735#if defined(MBEDTLS_X509_CRT_PARSE_C)
10736 ssl_key_cert_free( conf->key_cert );
10737#endif
10738
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010739 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010740}
10741
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010742#if defined(MBEDTLS_PK_C) && \
10743 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010744/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010745 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010746 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010748{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749#if defined(MBEDTLS_RSA_C)
10750 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10751 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010752#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753#if defined(MBEDTLS_ECDSA_C)
10754 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10755 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010758}
10759
Hanno Becker7e5437a2017-04-28 17:15:26 +010010760unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10761{
10762 switch( type ) {
10763 case MBEDTLS_PK_RSA:
10764 return( MBEDTLS_SSL_SIG_RSA );
10765 case MBEDTLS_PK_ECDSA:
10766 case MBEDTLS_PK_ECKEY:
10767 return( MBEDTLS_SSL_SIG_ECDSA );
10768 default:
10769 return( MBEDTLS_SSL_SIG_ANON );
10770 }
10771}
10772
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010773mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010774{
10775 switch( sig )
10776 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777#if defined(MBEDTLS_RSA_C)
10778 case MBEDTLS_SSL_SIG_RSA:
10779 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010780#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#if defined(MBEDTLS_ECDSA_C)
10782 case MBEDTLS_SSL_SIG_ECDSA:
10783 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010784#endif
10785 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010786 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010787 }
10788}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010789#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010790
Hanno Becker7e5437a2017-04-28 17:15:26 +010010791#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10792 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10793
10794/* Find an entry in a signature-hash set matching a given hash algorithm. */
10795mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10796 mbedtls_pk_type_t sig_alg )
10797{
10798 switch( sig_alg )
10799 {
10800 case MBEDTLS_PK_RSA:
10801 return( set->rsa );
10802 case MBEDTLS_PK_ECDSA:
10803 return( set->ecdsa );
10804 default:
10805 return( MBEDTLS_MD_NONE );
10806 }
10807}
10808
10809/* Add a signature-hash-pair to a signature-hash set */
10810void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10811 mbedtls_pk_type_t sig_alg,
10812 mbedtls_md_type_t md_alg )
10813{
10814 switch( sig_alg )
10815 {
10816 case MBEDTLS_PK_RSA:
10817 if( set->rsa == MBEDTLS_MD_NONE )
10818 set->rsa = md_alg;
10819 break;
10820
10821 case MBEDTLS_PK_ECDSA:
10822 if( set->ecdsa == MBEDTLS_MD_NONE )
10823 set->ecdsa = md_alg;
10824 break;
10825
10826 default:
10827 break;
10828 }
10829}
10830
10831/* Allow exactly one hash algorithm for each signature. */
10832void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10833 mbedtls_md_type_t md_alg )
10834{
10835 set->rsa = md_alg;
10836 set->ecdsa = md_alg;
10837}
10838
10839#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10840 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10841
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010842/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010843 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010845mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010846{
10847 switch( hash )
10848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849#if defined(MBEDTLS_MD5_C)
10850 case MBEDTLS_SSL_HASH_MD5:
10851 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010852#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_SHA1_C)
10854 case MBEDTLS_SSL_HASH_SHA1:
10855 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857#if defined(MBEDTLS_SHA256_C)
10858 case MBEDTLS_SSL_HASH_SHA224:
10859 return( MBEDTLS_MD_SHA224 );
10860 case MBEDTLS_SSL_HASH_SHA256:
10861 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010862#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#if defined(MBEDTLS_SHA512_C)
10864 case MBEDTLS_SSL_HASH_SHA384:
10865 return( MBEDTLS_MD_SHA384 );
10866 case MBEDTLS_SSL_HASH_SHA512:
10867 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010868#endif
10869 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010870 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010871 }
10872}
10873
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010874/*
10875 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10876 */
10877unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10878{
10879 switch( md )
10880 {
10881#if defined(MBEDTLS_MD5_C)
10882 case MBEDTLS_MD_MD5:
10883 return( MBEDTLS_SSL_HASH_MD5 );
10884#endif
10885#if defined(MBEDTLS_SHA1_C)
10886 case MBEDTLS_MD_SHA1:
10887 return( MBEDTLS_SSL_HASH_SHA1 );
10888#endif
10889#if defined(MBEDTLS_SHA256_C)
10890 case MBEDTLS_MD_SHA224:
10891 return( MBEDTLS_SSL_HASH_SHA224 );
10892 case MBEDTLS_MD_SHA256:
10893 return( MBEDTLS_SSL_HASH_SHA256 );
10894#endif
10895#if defined(MBEDTLS_SHA512_C)
10896 case MBEDTLS_MD_SHA384:
10897 return( MBEDTLS_SSL_HASH_SHA384 );
10898 case MBEDTLS_MD_SHA512:
10899 return( MBEDTLS_SSL_HASH_SHA512 );
10900#endif
10901 default:
10902 return( MBEDTLS_SSL_HASH_NONE );
10903 }
10904}
10905
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010906#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010907/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010908 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010909 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010910 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010911int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010912{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010913 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010914
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010915 if( ssl->conf->curve_list == NULL )
10916 return( -1 );
10917
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010918 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010919 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010920 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010921
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010922 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010923}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010924#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010925
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010926#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010927/*
10928 * Check if a hash proposed by the peer is in our list.
10929 * Return 0 if we're willing to use it, -1 otherwise.
10930 */
10931int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10932 mbedtls_md_type_t md )
10933{
10934 const int *cur;
10935
10936 if( ssl->conf->sig_hashes == NULL )
10937 return( -1 );
10938
10939 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10940 if( *cur == (int) md )
10941 return( 0 );
10942
10943 return( -1 );
10944}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010945#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010947#if defined(MBEDTLS_X509_CRT_PARSE_C)
10948int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10949 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010950 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010951 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010952{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010953 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010954#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010955 int usage = 0;
10956#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010957#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010958 const char *ext_oid;
10959 size_t ext_len;
10960#endif
10961
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010962#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10963 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010964 ((void) cert);
10965 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010966 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010967#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010969#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10970 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010971 {
10972 /* Server part of the key exchange */
10973 switch( ciphersuite->key_exchange )
10974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010975 case MBEDTLS_KEY_EXCHANGE_RSA:
10976 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010977 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010978 break;
10979
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010980 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10981 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10982 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10983 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010984 break;
10985
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010986 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10987 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010988 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010989 break;
10990
10991 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010992 case MBEDTLS_KEY_EXCHANGE_NONE:
10993 case MBEDTLS_KEY_EXCHANGE_PSK:
10994 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10995 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010996 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010997 usage = 0;
10998 }
10999 }
11000 else
11001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011002 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11003 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011004 }
11005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011007 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011008 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011009 ret = -1;
11010 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011011#else
11012 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011013#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11016 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011018 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11019 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011020 }
11021 else
11022 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011023 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11024 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011025 }
11026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011028 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011029 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011030 ret = -1;
11031 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011032#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011033
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011034 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011037
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011038/*
11039 * Convert version numbers to/from wire format
11040 * and, for DTLS, to/from TLS equivalent.
11041 *
11042 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011043 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011044 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11045 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11046 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011047void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011048 unsigned char ver[2] )
11049{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011050#if defined(MBEDTLS_SSL_PROTO_DTLS)
11051 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011053 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11055
11056 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11057 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11058 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011059 else
11060#else
11061 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011062#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011063 {
11064 ver[0] = (unsigned char) major;
11065 ver[1] = (unsigned char) minor;
11066 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011067}
11068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011069void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011070 const unsigned char ver[2] )
11071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011072#if defined(MBEDTLS_SSL_PROTO_DTLS)
11073 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011074 {
11075 *major = 255 - ver[0] + 2;
11076 *minor = 255 - ver[1] + 1;
11077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011078 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011079 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11080 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011081 else
11082#else
11083 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011084#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011085 {
11086 *major = ver[0];
11087 *minor = ver[1];
11088 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011089}
11090
Simon Butcher99000142016-10-13 17:21:01 +010011091int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11092{
11093#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11094 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11095 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11096
11097 switch( md )
11098 {
11099#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11100#if defined(MBEDTLS_MD5_C)
11101 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011102 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011103#endif
11104#if defined(MBEDTLS_SHA1_C)
11105 case MBEDTLS_SSL_HASH_SHA1:
11106 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11107 break;
11108#endif
11109#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11110#if defined(MBEDTLS_SHA512_C)
11111 case MBEDTLS_SSL_HASH_SHA384:
11112 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11113 break;
11114#endif
11115#if defined(MBEDTLS_SHA256_C)
11116 case MBEDTLS_SSL_HASH_SHA256:
11117 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11118 break;
11119#endif
11120 default:
11121 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11122 }
11123
11124 return 0;
11125#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11126 (void) ssl;
11127 (void) md;
11128
11129 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11130#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11131}
11132
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011133#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11134 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11135int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11136 unsigned char *output,
11137 unsigned char *data, size_t data_len )
11138{
11139 int ret = 0;
11140 mbedtls_md5_context mbedtls_md5;
11141 mbedtls_sha1_context mbedtls_sha1;
11142
11143 mbedtls_md5_init( &mbedtls_md5 );
11144 mbedtls_sha1_init( &mbedtls_sha1 );
11145
11146 /*
11147 * digitally-signed struct {
11148 * opaque md5_hash[16];
11149 * opaque sha_hash[20];
11150 * };
11151 *
11152 * md5_hash
11153 * MD5(ClientHello.random + ServerHello.random
11154 * + ServerParams);
11155 * sha_hash
11156 * SHA(ClientHello.random + ServerHello.random
11157 * + ServerParams);
11158 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011159 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011160 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011161 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011162 goto exit;
11163 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011164 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011165 ssl->handshake->randbytes, 64 ) ) != 0 )
11166 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011168 goto exit;
11169 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011170 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011171 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011173 goto exit;
11174 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011175 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011176 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011177 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011178 goto exit;
11179 }
11180
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011181 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011182 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011183 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184 goto exit;
11185 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011186 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011187 ssl->handshake->randbytes, 64 ) ) != 0 )
11188 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011189 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011190 goto exit;
11191 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011192 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011193 data_len ) ) != 0 )
11194 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011195 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011196 goto exit;
11197 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011198 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011199 output + 16 ) ) != 0 )
11200 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011202 goto exit;
11203 }
11204
11205exit:
11206 mbedtls_md5_free( &mbedtls_md5 );
11207 mbedtls_sha1_free( &mbedtls_sha1 );
11208
11209 if( ret != 0 )
11210 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11211 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11212
11213 return( ret );
11214
11215}
11216#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11217 MBEDTLS_SSL_PROTO_TLS1_1 */
11218
11219#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11220 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011221
11222#if defined(MBEDTLS_USE_PSA_CRYPTO)
11223int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11224 unsigned char *hash, size_t *hashlen,
11225 unsigned char *data, size_t data_len,
11226 mbedtls_md_type_t md_alg )
11227{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011228 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011229 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011230 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11231
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011233
11234 if( ( status = psa_hash_setup( &hash_operation,
11235 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011236 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011237 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011238 goto exit;
11239 }
11240
Andrzej Kurek814feff2019-01-14 04:35:19 -050011241 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11242 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011243 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011244 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011245 goto exit;
11246 }
11247
Andrzej Kurek814feff2019-01-14 04:35:19 -050011248 if( ( status = psa_hash_update( &hash_operation,
11249 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011250 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011251 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011252 goto exit;
11253 }
11254
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11256 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011257 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011258 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011259 goto exit;
11260 }
11261
11262exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011263 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011264 {
11265 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11266 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011267 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011268 {
11269 case PSA_ERROR_NOT_SUPPORTED:
11270 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011271 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011272 case PSA_ERROR_BUFFER_TOO_SMALL:
11273 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11274 case PSA_ERROR_INSUFFICIENT_MEMORY:
11275 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11276 default:
11277 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11278 }
11279 }
11280 return( 0 );
11281}
11282
11283#else
11284
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011285int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011286 unsigned char *hash, size_t *hashlen,
11287 unsigned char *data, size_t data_len,
11288 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011289{
11290 int ret = 0;
11291 mbedtls_md_context_t ctx;
11292 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011293 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011294
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011295 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011296
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011297 mbedtls_md_init( &ctx );
11298
11299 /*
11300 * digitally-signed struct {
11301 * opaque client_random[32];
11302 * opaque server_random[32];
11303 * ServerDHParams params;
11304 * };
11305 */
11306 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11307 {
11308 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11309 goto exit;
11310 }
11311 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11312 {
11313 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11314 goto exit;
11315 }
11316 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11317 {
11318 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11319 goto exit;
11320 }
11321 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11322 {
11323 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11324 goto exit;
11325 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011326 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011327 {
11328 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11329 goto exit;
11330 }
11331
11332exit:
11333 mbedtls_md_free( &ctx );
11334
11335 if( ret != 0 )
11336 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11337 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11338
11339 return( ret );
11340}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011341#endif /* MBEDTLS_USE_PSA_CRYPTO */
11342
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011343#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11344 MBEDTLS_SSL_PROTO_TLS1_2 */
11345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011346#endif /* MBEDTLS_SSL_TLS_C */