blob: d9e777d8a704011ad659cdcacb1723f1b3ac2d7c [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
Jaeden Amero2de07f12019-06-05 13:32:08 +0100956#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
957 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000958 transform->encrypt_then_mac = session->encrypt_then_mac;
959#endif
960 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000961
962 ciphersuite_info = handshake->ciphersuite_info;
963 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100964 if( cipher_info == NULL )
965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000967 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100969 }
970
Hanno Beckere694c3e2017-12-27 21:34:08 +0000971 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100972 if( md_info == NULL )
973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000975 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200976 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100977 }
978
Hanno Beckera0e20d02019-05-15 14:03:01 +0100979#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100980 /* Copy own and peer's CID if the use of the CID
981 * extension has been negotiated. */
982 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
983 {
984 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100985
Hanno Becker05154c32019-05-03 15:23:51 +0100986 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100987 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100988 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100989 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100990
991 transform->out_cid_len = ssl->handshake->peer_cid_len;
992 memcpy( transform->out_cid, ssl->handshake->peer_cid,
993 ssl->handshake->peer_cid_len );
994 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
995 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100996 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100997#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +0100998
Paul Bakker5121ce52009-01-03 21:22:43 +0000999 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001001 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_SSL_PROTO_SSL3)
1003 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001004 {
Paul Bakker48916f92012-09-16 19:57:18 +00001005 handshake->tls_prf = ssl3_prf;
1006 handshake->calc_verify = ssl_calc_verify_ssl;
1007 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009 else
1010#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1012 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 {
Paul Bakker48916f92012-09-16 19:57:18 +00001014 handshake->tls_prf = tls1_prf;
1015 handshake->calc_verify = ssl_calc_verify_tls;
1016 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001017 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001018 else
1019#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001020#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1021#if defined(MBEDTLS_SHA512_C)
1022 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001023 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001024 {
Paul Bakker48916f92012-09-16 19:57:18 +00001025 handshake->tls_prf = tls_prf_sha384;
1026 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1027 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001028 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001029 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#if defined(MBEDTLS_SHA256_C)
1032 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001033 {
Paul Bakker48916f92012-09-16 19:57:18 +00001034 handshake->tls_prf = tls_prf_sha256;
1035 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1036 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001037 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001038 else
1039#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001040#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001041 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001042 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1043 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001044 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001045
1046 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001047 * SSLv3:
1048 * master =
1049 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1050 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1051 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001052 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001053 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 * master = PRF( premaster, "master secret", randbytes )[0..47]
1055 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001056 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001057 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001058 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1059 }
1060 else
1061 {
1062 /* The label for the KDF used for key expansion.
1063 * This is either "master secret" or "extended master secret"
1064 * depending on whether the Extended Master Secret extension
1065 * is used. */
1066 char const *lbl = "master secret";
1067
1068 /* The salt for the KDF used for key expansion.
1069 * - If the Extended Master Secret extension is not used,
1070 * this is ClientHello.Random + ServerHello.Random
1071 * (see Sect. 8.1 in RFC 5246).
1072 * - If the Extended Master Secret extension is used,
1073 * this is the transcript of the handshake so far.
1074 * (see Sect. 4 in RFC 7627). */
1075 unsigned char const *salt = handshake->randbytes;
1076 size_t salt_len = 64;
1077
1078#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001079 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001081 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001082
Hanno Becker35b23c72018-10-23 12:10:41 +01001083 lbl = "extended master secret";
1084 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001085 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1087 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001089#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001090 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1091 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001092 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001093 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001095#endif /* MBEDTLS_SHA512_C */
1096 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001097 }
1098 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001099#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001100 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001101
Hanno Becker35b23c72018-10-23 12:10:41 +01001102 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001103 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001104#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1105
Hanno Becker7d0a5692018-10-23 15:26:22 +01001106#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1107 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1108 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1109 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1110 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001111 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001112 /* Perform PSK-to-MS expansion in a single step. */
1113 psa_status_t status;
1114 psa_algorithm_t alg;
1115 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001116 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001117
1118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1119
1120 psk = ssl->conf->psk_opaque;
1121 if( ssl->handshake->psk_opaque != 0 )
1122 psk = ssl->handshake->psk_opaque;
1123
Hanno Becker22bf1452019-04-05 11:21:08 +01001124 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001125 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1126 else
1127 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1128
1129 status = psa_key_derivation( &generator, psk, alg,
1130 salt, salt_len,
1131 (unsigned char const *) lbl,
1132 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001133 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001134 if( status != PSA_SUCCESS )
1135 {
1136 psa_generator_abort( &generator );
1137 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1138 }
1139
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001140 status = psa_generator_read( &generator, session->master,
1141 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001142 if( status != PSA_SUCCESS )
1143 {
1144 psa_generator_abort( &generator );
1145 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1146 }
1147
1148 status = psa_generator_abort( &generator );
1149 if( status != PSA_SUCCESS )
1150 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001151 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001152 else
1153#endif
1154 {
1155 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1156 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001157 session->master,
1158 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001159 if( ret != 0 )
1160 {
1161 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1162 return( ret );
1163 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001164
Hanno Becker7d0a5692018-10-23 15:26:22 +01001165 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1166 handshake->premaster,
1167 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001168
Hanno Becker7d0a5692018-10-23 15:26:22 +01001169 mbedtls_platform_zeroize( handshake->premaster,
1170 sizeof(handshake->premaster) );
1171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001173
1174 /*
1175 * Swap the client and server random values.
1176 */
Paul Bakker48916f92012-09-16 19:57:18 +00001177 memcpy( tmp, handshake->randbytes, 64 );
1178 memcpy( handshake->randbytes, tmp + 32, 32 );
1179 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001180 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001181
1182 /*
1183 * SSLv3:
1184 * key block =
1185 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1186 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1187 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1188 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1189 * ...
1190 *
1191 * TLSv1:
1192 * key block = PRF( master, "key expansion", randbytes )
1193 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001194 ret = handshake->tls_prf( session->master, 48, "key expansion",
1195 handshake->randbytes, 64, keyblk, 256 );
1196 if( ret != 0 )
1197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001199 return( ret );
1200 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001202 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1203 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1204 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1205 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1206 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001207
Paul Bakker5121ce52009-01-03 21:22:43 +00001208 /*
1209 * Determine the appropriate key, IV and MAC length.
1210 */
Paul Bakker68884e32013-01-07 18:20:04 +01001211
Hanno Becker88aaf652017-12-27 08:17:40 +00001212 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001213
Hanno Becker8031d062018-01-03 15:32:31 +00001214#if defined(MBEDTLS_GCM_C) || \
1215 defined(MBEDTLS_CCM_C) || \
1216 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001217 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001218 cipher_info->mode == MBEDTLS_MODE_CCM ||
1219 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001220 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001221 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001222
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001223 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001224 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001225 transform->taglen =
1226 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001227
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001228 /* All modes haves 96-bit IVs;
1229 * GCM and CCM has 4 implicit and 8 explicit bytes
1230 * ChachaPoly has all 12 bytes implicit
1231 */
Paul Bakker68884e32013-01-07 18:20:04 +01001232 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001233 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1234 transform->fixed_ivlen = 12;
1235 else
1236 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001237
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001238 /* Minimum length of encrypted record */
1239 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001240 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001241 }
1242 else
Hanno Becker8031d062018-01-03 15:32:31 +00001243#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1244#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1245 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1246 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001247 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001248 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001249 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1250 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001251 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001252 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001253 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001254 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001255
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001256 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001257 mac_key_len = mbedtls_md_get_size( md_info );
1258 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001260#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001261 /*
1262 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1263 * (rfc 6066 page 13 or rfc 2104 section 4),
1264 * so we only need to adjust the length here.
1265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001268 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001269
1270#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1271 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001272 * HMAC implementation which also truncates the key
1273 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001274 mac_key_len = transform->maclen;
1275#endif
1276 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001277#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001278
1279 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001280 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001281
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001282 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001284 transform->minlen = transform->maclen;
1285 else
Paul Bakker68884e32013-01-07 18:20:04 +01001286 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001287 /*
1288 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001289 * 1. if EtM is in use: one block plus MAC
1290 * otherwise: * first multiple of blocklen greater than maclen
1291 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001292 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001293#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1294 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001295 {
1296 transform->minlen = transform->maclen
1297 + cipher_info->block_size;
1298 }
1299 else
1300#endif
1301 {
1302 transform->minlen = transform->maclen
1303 + cipher_info->block_size
1304 - transform->maclen % cipher_info->block_size;
1305 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001307#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1308 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1309 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001310 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001311 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001312#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001313#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1314 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1315 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001316 {
1317 transform->minlen += transform->ivlen;
1318 }
1319 else
1320#endif
1321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001322 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001323 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1324 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001325 }
Paul Bakker68884e32013-01-07 18:20:04 +01001326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001327 }
Hanno Becker8031d062018-01-03 15:32:31 +00001328 else
1329#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1330 {
1331 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1332 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1333 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001334
Hanno Becker88aaf652017-12-27 08:17:40 +00001335 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1336 (unsigned) keylen,
1337 (unsigned) transform->minlen,
1338 (unsigned) transform->ivlen,
1339 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001340
1341 /*
1342 * Finally setup the cipher contexts, IVs and MAC secrets.
1343 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001344#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001345 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001346 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001347 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001348 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001349
Paul Bakker68884e32013-01-07 18:20:04 +01001350 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001351 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001352
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001353 /*
1354 * This is not used in TLS v1.1.
1355 */
Paul Bakker48916f92012-09-16 19:57:18 +00001356 iv_copy_len = ( transform->fixed_ivlen ) ?
1357 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001358 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1359 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001360 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001361 }
1362 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001363#endif /* MBEDTLS_SSL_CLI_C */
1364#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001365 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001366 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001367 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001368 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001369
Hanno Becker81c7b182017-11-09 18:39:33 +00001370 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001371 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001373 /*
1374 * This is not used in TLS v1.1.
1375 */
Paul Bakker48916f92012-09-16 19:57:18 +00001376 iv_copy_len = ( transform->fixed_ivlen ) ?
1377 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001378 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1379 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001380 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001381 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001382 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001386 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1387 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001388 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001389
Hanno Beckerd56ed242018-01-03 15:32:51 +00001390#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001391#if defined(MBEDTLS_SSL_PROTO_SSL3)
1392 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001393 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001394 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001397 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1398 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001399 }
1400
Hanno Becker81c7b182017-11-09 18:39:33 +00001401 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1402 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001403 }
1404 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001405#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1406#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1407 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1408 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001409 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001410 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1411 For AEAD-based ciphersuites, there is nothing to do here. */
1412 if( mac_key_len != 0 )
1413 {
1414 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1415 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1416 }
Paul Bakker68884e32013-01-07 18:20:04 +01001417 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001418 else
1419#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001422 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1423 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001424 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001425#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001427#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1428 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001429 {
1430 int ret = 0;
1431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001433
Hanno Becker88aaf652017-12-27 08:17:40 +00001434 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001435 transform->iv_enc, transform->iv_dec,
1436 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001437 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001438 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001440 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001441 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1442 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001443 }
1444 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001445#else
1446 ((void) mac_dec);
1447 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001448#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001449
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001450#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1451 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001452 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001453 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1454 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001455 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001456 iv_copy_len );
1457 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001458
1459 if( ssl->conf->f_export_keys_ext != NULL )
1460 {
1461 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1462 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001463 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001464 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001465 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001466 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001467 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001468 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001469#endif
1470
Hanno Beckerf704bef2018-11-16 15:21:18 +00001471#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001472
1473 /* Only use PSA-based ciphers for TLS-1.2.
1474 * That's relevant at least for TLS-1.0, where
1475 * we assume that mbedtls_cipher_crypt() updates
1476 * the structure field for the IV, which the PSA-based
1477 * implementation currently doesn't. */
1478#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1479 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001480 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001481 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001482 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001483 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1484 {
1485 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001486 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001487 }
1488
1489 if( ret == 0 )
1490 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001491 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001492 psa_fallthrough = 0;
1493 }
1494 else
1495 {
1496 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1497 psa_fallthrough = 1;
1498 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001500 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001501 psa_fallthrough = 1;
1502#else
1503 psa_fallthrough = 1;
1504#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001505
Hanno Beckercb1cc802018-11-17 22:27:38 +00001506 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001507#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001508 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001509 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001510 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001511 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001512 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001513 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001514
Hanno Beckerf704bef2018-11-16 15:21:18 +00001515#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001516 /* Only use PSA-based ciphers for TLS-1.2.
1517 * That's relevant at least for TLS-1.0, where
1518 * we assume that mbedtls_cipher_crypt() updates
1519 * the structure field for the IV, which the PSA-based
1520 * implementation currently doesn't. */
1521#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1522 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001523 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001524 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001525 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001526 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1527 {
1528 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001529 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001530 }
1531
1532 if( ret == 0 )
1533 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001534 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001535 psa_fallthrough = 0;
1536 }
1537 else
1538 {
1539 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1540 psa_fallthrough = 1;
1541 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001542 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001543 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001544 psa_fallthrough = 1;
1545#else
1546 psa_fallthrough = 1;
1547#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001548
Hanno Beckercb1cc802018-11-17 22:27:38 +00001549 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001550#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001551 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001552 cipher_info ) ) != 0 )
1553 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001554 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001555 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001556 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001559 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001563 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001564 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001567 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001571 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001572 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574#if defined(MBEDTLS_CIPHER_MODE_CBC)
1575 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001581 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001582 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1585 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001586 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001588 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001589 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001590 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001591#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Paul Bakker5121ce52009-01-03 21:22:43 +00001593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001595 // Initialize compression
1596 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001598 {
Paul Bakker16770332013-10-11 09:59:44 +02001599 if( ssl->compress_buf == NULL )
1600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001602 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001603 if( ssl->compress_buf == NULL )
1604 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001606 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001607 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1608 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001609 }
1610 }
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613
Paul Bakker48916f92012-09-16 19:57:18 +00001614 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1615 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001617 if( deflateInit( &transform->ctx_deflate,
1618 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001619 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001622 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1623 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001624 }
1625 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001628 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001629end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001630 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1631 mbedtls_platform_zeroize( handshake->randbytes,
1632 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001633 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634}
1635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001636#if defined(MBEDTLS_SSL_PROTO_SSL3)
1637void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001638{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001639 mbedtls_md5_context md5;
1640 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001641 unsigned char pad_1[48];
1642 unsigned char pad_2[48];
1643
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001645
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001646 mbedtls_md5_init( &md5 );
1647 mbedtls_sha1_init( &sha1 );
1648
1649 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1650 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Paul Bakker380da532012-04-18 16:10:25 +00001652 memset( pad_1, 0x36, 48 );
1653 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001655 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1656 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1657 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001658
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001659 mbedtls_md5_starts_ret( &md5 );
1660 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1661 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1662 mbedtls_md5_update_ret( &md5, hash, 16 );
1663 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001664
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001665 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1666 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1667 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001668
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001669 mbedtls_sha1_starts_ret( &sha1 );
1670 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1671 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1672 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1673 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001675 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1676 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001678 mbedtls_md5_free( &md5 );
1679 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001680
Paul Bakker380da532012-04-18 16:10:25 +00001681 return;
1682}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001685#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1686void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001687{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001688 mbedtls_md5_context md5;
1689 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001692
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001693 mbedtls_md5_init( &md5 );
1694 mbedtls_sha1_init( &sha1 );
1695
1696 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1697 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001698
Andrzej Kurekeb342242019-01-29 09:14:33 -05001699 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001700 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1703 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001704
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001705 mbedtls_md5_free( &md5 );
1706 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001707
Paul Bakker380da532012-04-18 16:10:25 +00001708 return;
1709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001712#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1713#if defined(MBEDTLS_SHA256_C)
1714void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001715{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001716#if defined(MBEDTLS_USE_PSA_CRYPTO)
1717 size_t hash_size;
1718 psa_status_t status;
1719 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1720
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1722 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1723 if( status != PSA_SUCCESS )
1724 {
1725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1726 return;
1727 }
1728
1729 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1730 if( status != PSA_SUCCESS )
1731 {
1732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1733 return;
1734 }
1735 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1737#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001738 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001739
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001740 mbedtls_sha256_init( &sha256 );
1741
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001743
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001744 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001745 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001746
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1748 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001749
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001750 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001751#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001752 return;
1753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001756#if defined(MBEDTLS_SHA512_C)
1757void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001758{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001759#if defined(MBEDTLS_USE_PSA_CRYPTO)
1760 size_t hash_size;
1761 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001762 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001763
1764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001765 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001766 if( status != PSA_SUCCESS )
1767 {
1768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1769 return;
1770 }
1771
Andrzej Kurek972fba52019-01-30 03:29:12 -05001772 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001773 if( status != PSA_SUCCESS )
1774 {
1775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1776 return;
1777 }
1778 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1779 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1780#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001781 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001782
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001783 mbedtls_sha512_init( &sha512 );
1784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001785 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001786
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001787 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001788 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001793 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001794#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001795 return;
1796}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001797#endif /* MBEDTLS_SHA512_C */
1798#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001800#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1801int 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 +02001802{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001803 unsigned char *p = ssl->handshake->premaster;
1804 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001805 const unsigned char *psk = ssl->conf->psk;
1806 size_t psk_len = ssl->conf->psk_len;
1807
1808 /* If the psk callback was called, use its result */
1809 if( ssl->handshake->psk != NULL )
1810 {
1811 psk = ssl->handshake->psk;
1812 psk_len = ssl->handshake->psk_len;
1813 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001814
1815 /*
1816 * PMS = struct {
1817 * opaque other_secret<0..2^16-1>;
1818 * opaque psk<0..2^16-1>;
1819 * };
1820 * with "other_secret" depending on the particular key exchange
1821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1823 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001824 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001825 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001826 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001827
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001828 *(p++) = (unsigned char)( psk_len >> 8 );
1829 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001830
1831 if( end < p || (size_t)( end - p ) < psk_len )
1832 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1833
1834 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001835 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001836 }
1837 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1839#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1840 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001841 {
1842 /*
1843 * other_secret already set by the ClientKeyExchange message,
1844 * and is 48 bytes long
1845 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001846 if( end - p < 2 )
1847 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1848
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001849 *p++ = 0;
1850 *p++ = 48;
1851 p += 48;
1852 }
1853 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001854#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1855#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1856 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001857 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001858 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001859 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001861 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001863 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001864 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001867 return( ret );
1868 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001869 *(p++) = (unsigned char)( len >> 8 );
1870 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001871 p += len;
1872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001873 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001874 }
1875 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1877#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1878 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001880 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001881 size_t zlen;
1882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001884 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001885 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001887 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001888 return( ret );
1889 }
1890
1891 *(p++) = (unsigned char)( zlen >> 8 );
1892 *(p++) = (unsigned char)( zlen );
1893 p += zlen;
1894
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001895 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1896 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001897 }
1898 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001900 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001901 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1902 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001903 }
1904
1905 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001906 if( end - p < 2 )
1907 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001908
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001909 *(p++) = (unsigned char)( psk_len >> 8 );
1910 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001911
1912 if( end < p || (size_t)( end - p ) < psk_len )
1913 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1914
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001915 memcpy( p, psk, psk_len );
1916 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001917
1918 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1919
1920 return( 0 );
1921}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001924#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001925/*
1926 * SSLv3.0 MAC functions
1927 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001928#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001929static void ssl_mac( mbedtls_md_context_t *md_ctx,
1930 const unsigned char *secret,
1931 const unsigned char *buf, size_t len,
1932 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001933 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001934{
1935 unsigned char header[11];
1936 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001937 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1939 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001940
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001941 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001942 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001943 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001944 else
Paul Bakker68884e32013-01-07 18:20:04 +01001945 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001946
1947 memcpy( header, ctr, 8 );
1948 header[ 8] = (unsigned char) type;
1949 header[ 9] = (unsigned char)( len >> 8 );
1950 header[10] = (unsigned char)( len );
1951
Paul Bakker68884e32013-01-07 18:20:04 +01001952 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001953 mbedtls_md_starts( md_ctx );
1954 mbedtls_md_update( md_ctx, secret, md_size );
1955 mbedtls_md_update( md_ctx, padding, padlen );
1956 mbedtls_md_update( md_ctx, header, 11 );
1957 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001958 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001959
Paul Bakker68884e32013-01-07 18:20:04 +01001960 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001961 mbedtls_md_starts( md_ctx );
1962 mbedtls_md_update( md_ctx, secret, md_size );
1963 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001964 mbedtls_md_update( md_ctx, out, md_size );
1965 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001966}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001967#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001968
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001969/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001970 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001971#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001972 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1973 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1974 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1975/* This function makes sure every byte in the memory region is accessed
1976 * (in ascending addresses order) */
1977static void ssl_read_memory( unsigned char *p, size_t len )
1978{
1979 unsigned char acc = 0;
1980 volatile unsigned char force;
1981
1982 for( ; len != 0; p++, len-- )
1983 acc ^= *p;
1984
1985 force = acc;
1986 (void) force;
1987}
1988#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1989
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001990/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001991 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001992 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001993
Hanno Beckera0e20d02019-05-15 14:03:01 +01001994#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001995/* This functions transforms a DTLS plaintext fragment and a record content
1996 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001997 *
1998 * struct {
1999 * opaque content[DTLSPlaintext.length];
2000 * ContentType real_type;
2001 * uint8 zeros[length_of_padding];
2002 * } DTLSInnerPlaintext;
2003 *
2004 * Input:
2005 * - `content`: The beginning of the buffer holding the
2006 * plaintext to be wrapped.
2007 * - `*content_size`: The length of the plaintext in Bytes.
2008 * - `max_len`: The number of Bytes available starting from
2009 * `content`. This must be `>= *content_size`.
2010 * - `rec_type`: The desired record content type.
2011 *
2012 * Output:
2013 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2014 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2015 *
2016 * Returns:
2017 * - `0` on success.
2018 * - A negative error code if `max_len` didn't offer enough space
2019 * for the expansion.
2020 */
2021static int ssl_cid_build_inner_plaintext( unsigned char *content,
2022 size_t *content_size,
2023 size_t remaining,
2024 uint8_t rec_type )
2025{
2026 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002027 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2028 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2029 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002030
2031 /* Write real content type */
2032 if( remaining == 0 )
2033 return( -1 );
2034 content[ len ] = rec_type;
2035 len++;
2036 remaining--;
2037
2038 if( remaining < pad )
2039 return( -1 );
2040 memset( content + len, 0, pad );
2041 len += pad;
2042 remaining -= pad;
2043
2044 *content_size = len;
2045 return( 0 );
2046}
2047
Hanno Becker07dc97d2019-05-20 15:08:01 +01002048/* This function parses a DTLSInnerPlaintext structure.
2049 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002050static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2051 size_t *content_size,
2052 uint8_t *rec_type )
2053{
2054 size_t remaining = *content_size;
2055
2056 /* Determine length of padding by skipping zeroes from the back. */
2057 do
2058 {
2059 if( remaining == 0 )
2060 return( -1 );
2061 remaining--;
2062 } while( content[ remaining ] == 0 );
2063
2064 *content_size = remaining;
2065 *rec_type = content[ remaining ];
2066
2067 return( 0 );
2068}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002069#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002070
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002071/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002072 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002073static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002074 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002075 mbedtls_record *rec )
2076{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002077 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002078 *
2079 * additional_data = seq_num + TLSCompressed.type +
2080 * TLSCompressed.version + TLSCompressed.length;
2081 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002082 * For the CID extension, this is extended as follows
2083 * (quoting draft-ietf-tls-dtls-connection-id-05,
2084 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002085 *
2086 * additional_data = seq_num + DTLSPlaintext.type +
2087 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002088 * cid +
2089 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002090 * length_of_DTLSInnerPlaintext;
2091 */
2092
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002093 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2094 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002095 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002096
Hanno Beckera0e20d02019-05-15 14:03:01 +01002097#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002098 if( rec->cid_len != 0 )
2099 {
2100 memcpy( add_data + 11, rec->cid, rec->cid_len );
2101 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2102 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2103 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2104 *add_data_len = 13 + 1 + rec->cid_len;
2105 }
2106 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002107#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002108 {
2109 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2110 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2111 *add_data_len = 13;
2112 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002113}
2114
Hanno Beckera18d1322018-01-03 14:27:32 +00002115int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2116 mbedtls_ssl_transform *transform,
2117 mbedtls_record *rec,
2118 int (*f_rng)(void *, unsigned char *, size_t),
2119 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002120{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002121 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002122 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002123 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002124 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002125 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002126 size_t post_avail;
2127
2128 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002129#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002130 ((void) ssl);
2131#endif
2132
2133 /* The PRNG is used for dynamic IV generation that's used
2134 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2135#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2136 ( defined(MBEDTLS_AES_C) || \
2137 defined(MBEDTLS_ARIA_C) || \
2138 defined(MBEDTLS_CAMELLIA_C) ) && \
2139 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2140 ((void) f_rng);
2141 ((void) p_rng);
2142#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002144 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002145
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002146 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002147 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2149 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2150 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002151 if( rec == NULL
2152 || rec->buf == NULL
2153 || rec->buf_len < rec->data_offset
2154 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002155#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002156 || rec->cid_len != 0
2157#endif
2158 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002159 {
2160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002161 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002162 }
2163
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002164 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002165 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002166 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002167 data, rec->data_len );
2168
2169 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2170
2171 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2172 {
2173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2174 (unsigned) rec->data_len,
2175 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2176 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2177 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002178
Hanno Beckera0e20d02019-05-15 14:03:01 +01002179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002180 /*
2181 * Add CID information
2182 */
2183 rec->cid_len = transform->out_cid_len;
2184 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2185 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002186
2187 if( rec->cid_len != 0 )
2188 {
2189 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002190 * Wrap plaintext into DTLSInnerPlaintext structure.
2191 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002192 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002193 * Note that this changes `rec->data_len`, and hence
2194 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002195 */
2196 if( ssl_cid_build_inner_plaintext( data,
2197 &rec->data_len,
2198 post_avail,
2199 rec->type ) != 0 )
2200 {
2201 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2202 }
2203
2204 rec->type = MBEDTLS_SSL_MSG_CID;
2205 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002206#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002207
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002208 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2209
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002211 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002212 */
Hanno Becker52344c22018-01-03 15:24:20 +00002213#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002214 if( mode == MBEDTLS_MODE_STREAM ||
2215 ( mode == MBEDTLS_MODE_CBC
2216#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002217 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002218#endif
2219 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002220 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002221 if( post_avail < transform->maclen )
2222 {
2223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2224 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2225 }
2226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002227#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002228 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002229 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002230 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002231 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2232 data, rec->data_len, rec->ctr, rec->type, mac );
2233 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002234 }
2235 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002237#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2238 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002239 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002240 {
Hanno Becker992b6872017-11-09 18:57:39 +00002241 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2242
Hanno Beckercab87e62019-04-29 13:52:53 +01002243 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002244
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002246 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002247 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2248 data, rec->data_len );
2249 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2250 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2251
2252 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002253 }
2254 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002255#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002256 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002257 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2258 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002259 }
2260
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002261 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2262 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002263
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264 rec->data_len += transform->maclen;
2265 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002266 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002267 }
Hanno Becker52344c22018-01-03 15:24:20 +00002268#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002269
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002270 /*
2271 * Encrypt
2272 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002273#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2274 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002275 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002276 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002277 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002279 "including %d bytes of padding",
2280 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002281
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002282 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2283 transform->iv_enc, transform->ivlen,
2284 data, rec->data_len,
2285 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002286 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002287 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002288 return( ret );
2289 }
2290
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002291 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002295 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002296 }
Paul Bakker68884e32013-01-07 18:20:04 +01002297 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002298#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002299
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002300#if defined(MBEDTLS_GCM_C) || \
2301 defined(MBEDTLS_CCM_C) || \
2302 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002303 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002304 mode == MBEDTLS_MODE_CCM ||
2305 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002306 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002307 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002308 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002309 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002310
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002311 /* Check that there's space for both the authentication tag
2312 * and the explicit IV before and after the record content. */
2313 if( post_avail < transform->taglen ||
2314 rec->data_offset < explicit_iv_len )
2315 {
2316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2317 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2318 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002319
Paul Bakker68884e32013-01-07 18:20:04 +01002320 /*
2321 * Generate IV
2322 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002323 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2324 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002325 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002326 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002327 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2328 explicit_iv_len );
2329 /* Prefix record content with explicit IV. */
2330 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331 }
2332 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2333 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002334 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002335 unsigned char i;
2336
2337 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2338
2339 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002340 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002341 }
2342 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002343 {
2344 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002345 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2346 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002347 }
2348
Hanno Beckercab87e62019-04-29 13:52:53 +01002349 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002350
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2352 iv, transform->ivlen );
2353 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002354 data - explicit_iv_len, explicit_iv_len );
2355 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002356 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002360
Paul Bakker68884e32013-01-07 18:20:04 +01002361 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002362 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002363 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002364
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002365 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002367 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002368 data, rec->data_len, /* source */
2369 data, &rec->data_len, /* destination */
2370 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002372 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002373 return( ret );
2374 }
2375
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2377 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002378
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 rec->data_len += transform->taglen + explicit_iv_len;
2380 rec->data_offset -= explicit_iv_len;
2381 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002382 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002383 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002384 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002385#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2386#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002387 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002388 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002389 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002390 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002391 size_t padlen, i;
2392 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002393
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002394 /* Currently we're always using minimal padding
2395 * (up to 255 bytes would be allowed). */
2396 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2397 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002398 padlen = 0;
2399
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002400 /* Check there's enough space in the buffer for the padding. */
2401 if( post_avail < padlen + 1 )
2402 {
2403 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2404 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2405 }
2406
Paul Bakker5121ce52009-01-03 21:22:43 +00002407 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002408 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002409
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002410 rec->data_len += padlen + 1;
2411 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002413#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002414 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002415 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2416 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002418 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002419 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 if( f_rng == NULL )
2421 {
2422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2423 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2424 }
2425
2426 if( rec->data_offset < transform->ivlen )
2427 {
2428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2429 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2430 }
2431
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002432 /*
2433 * Generate IV
2434 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002435 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002436 if( ret != 0 )
2437 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002438
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002439 memcpy( data - transform->ivlen, transform->iv_enc,
2440 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002441
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002442 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002446 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002447 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002448 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002449
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002450 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2451 transform->iv_enc,
2452 transform->ivlen,
2453 data, rec->data_len,
2454 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002457 return( ret );
2458 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002459
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2463 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002464 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002465
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002466#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002467 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002468 {
2469 /*
2470 * Save IV in SSL3 and TLS1
2471 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002472 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2473 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002474 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002475 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002476#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002477 {
2478 data -= transform->ivlen;
2479 rec->data_offset -= transform->ivlen;
2480 rec->data_len += transform->ivlen;
2481 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002483#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002484 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002485 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002486 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2487
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002488 /*
2489 * MAC(MAC_write_key, seq_num +
2490 * TLSCipherText.type +
2491 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002492 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002493 * IV + // except for TLS 1.0
2494 * ENC(content + padding + padding_length));
2495 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496
2497 if( post_avail < transform->maclen)
2498 {
2499 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2500 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2501 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502
Hanno Beckercab87e62019-04-29 13:52:53 +01002503 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002505 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002506 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002507 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002508
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002510 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002511 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2512 data, rec->data_len );
2513 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2514 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002518 rec->data_len += transform->maclen;
2519 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002520 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002521 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002522#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002523 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002524 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002525#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002526 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002530 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002531
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002532 /* Make extra sure authentication was performed, exactly once */
2533 if( auth_done != 1 )
2534 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2536 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002537 }
2538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002539 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002540
2541 return( 0 );
2542}
2543
Hanno Beckera18d1322018-01-03 14:27:32 +00002544int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2545 mbedtls_ssl_transform *transform,
2546 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002547{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002548 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002549 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002551#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002552 size_t padlen = 0, correct = 1;
2553#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002554 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002555 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002556 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002557
Hanno Beckera18d1322018-01-03 14:27:32 +00002558#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 ((void) ssl);
2560#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002563 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002564 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002565 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2566 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2567 }
2568 if( rec == NULL ||
2569 rec->buf == NULL ||
2570 rec->buf_len < rec->data_offset ||
2571 rec->buf_len - rec->data_offset < rec->data_len )
2572 {
2573 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002574 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002575 }
2576
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002577 data = rec->buf + rec->data_offset;
2578 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002579
Hanno Beckera0e20d02019-05-15 14:03:01 +01002580#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002581 /*
2582 * Match record's CID with incoming CID.
2583 */
Hanno Becker938489a2019-05-08 13:02:22 +01002584 if( rec->cid_len != transform->in_cid_len ||
2585 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2586 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002587 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002588 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002589#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002591#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2592 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002593 {
2594 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002595 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2596 transform->iv_dec,
2597 transform->ivlen,
2598 data, rec->data_len,
2599 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002600 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002601 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002602 return( ret );
2603 }
2604
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002605 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002606 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002607 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2608 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002609 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002610 }
Paul Bakker68884e32013-01-07 18:20:04 +01002611 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613#if defined(MBEDTLS_GCM_C) || \
2614 defined(MBEDTLS_CCM_C) || \
2615 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002616 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002617 mode == MBEDTLS_MODE_CCM ||
2618 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002619 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002622
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002623 /*
2624 * Compute and update sizes
2625 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002626 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002627 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002628 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002629 "+ taglen (%d)", rec->data_len,
2630 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002631 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002632 }
Paul Bakker68884e32013-01-07 18:20:04 +01002633
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002634 /*
2635 * Prepare IV
2636 */
2637 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2638 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002639 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002640 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002641 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002642
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002643 }
2644 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2645 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002646 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002647 unsigned char i;
2648
2649 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2650
2651 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002652 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002653 }
2654 else
2655 {
2656 /* Reminder if we ever add an AEAD mode with a different size */
2657 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2658 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2659 }
2660
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002661 data += explicit_iv_len;
2662 rec->data_offset += explicit_iv_len;
2663 rec->data_len -= explicit_iv_len + transform->taglen;
2664
Hanno Beckercab87e62019-04-29 13:52:53 +01002665 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002667 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668
2669 memcpy( transform->iv_dec + transform->fixed_ivlen,
2670 data - explicit_iv_len, explicit_iv_len );
2671
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002672 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002674 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002675
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002677 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002678 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002679 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002680 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2681 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002682 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002683 data, rec->data_len,
2684 data, &olen,
2685 data + rec->data_len,
2686 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002690 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2691 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002692
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002693 return( ret );
2694 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002695 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002696
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002697 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002699 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2700 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002701 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002702 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002703 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002704#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2705#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002706 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002708 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002709 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002710
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 /*
Paul Bakker45829992013-01-03 14:52:21 +01002712 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002713 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002714#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002715 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2716 {
2717 /* The ciphertext is prefixed with the CBC IV. */
2718 minlen += transform->ivlen;
2719 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002720#endif
Paul Bakker45829992013-01-03 14:52:21 +01002721
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002722 /* Size considerations:
2723 *
2724 * - The CBC cipher text must not be empty and hence
2725 * at least of size transform->ivlen.
2726 *
2727 * Together with the potential IV-prefix, this explains
2728 * the first of the two checks below.
2729 *
2730 * - The record must contain a MAC, either in plain or
2731 * encrypted, depending on whether Encrypt-then-MAC
2732 * is used or not.
2733 * - If it is, the message contains the IV-prefix,
2734 * the CBC ciphertext, and the MAC.
2735 * - If it is not, the padded plaintext, and hence
2736 * the CBC ciphertext, has at least length maclen + 1
2737 * because there is at least the padding length byte.
2738 *
2739 * As the CBC ciphertext is not empty, both cases give the
2740 * lower bound minlen + maclen + 1 on the record size, which
2741 * we test for in the second check below.
2742 */
2743 if( rec->data_len < minlen + transform->ivlen ||
2744 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002745 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002747 "+ 1 ) ( + expl IV )", rec->data_len,
2748 transform->ivlen,
2749 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002750 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002751 }
2752
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002753 /*
2754 * Authenticate before decrypt if enabled
2755 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002756#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002757 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002758 {
Hanno Becker992b6872017-11-09 18:57:39 +00002759 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002761 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002762
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002763 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2764 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002765
Hanno Beckercab87e62019-04-29 13:52:53 +01002766 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002767
Hanno Beckercab87e62019-04-29 13:52:53 +01002768 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2769 add_data_len );
2770 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2771 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002772 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2773 data, rec->data_len );
2774 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2775 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002776
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002777 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2778 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002779 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002780 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002781
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002782 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2783 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002788 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002789 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002791
2792 /*
2793 * Check length sanity
2794 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002795 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002798 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002799 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002800 }
2801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002802#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002804 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002807 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002808 /* This is safe because data_len >= minlen + maclen + 1 initially,
2809 * and at this point we have at most subtracted maclen (note that
2810 * minlen == transform->ivlen here). */
2811 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002812
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002813 data += transform->ivlen;
2814 rec->data_offset += transform->ivlen;
2815 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002816 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002817#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002818
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002819 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2820 transform->iv_dec, transform->ivlen,
2821 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002824 return( ret );
2825 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002826
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002827 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2830 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002831 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002833#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002834 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002835 {
2836 /*
2837 * Save IV in SSL3 and TLS1
2838 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002839 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2840 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002841 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002842#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002843
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002844 /* Safe since data_len >= minlen + maclen + 1, so after having
2845 * subtracted at most minlen and maclen up to this point,
2846 * data_len > 0. */
2847 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002848
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002849 if( auth_done == 1 )
2850 {
2851 correct *= ( rec->data_len >= padlen + 1 );
2852 padlen *= ( rec->data_len >= padlen + 1 );
2853 }
2854 else
Paul Bakker45829992013-01-03 14:52:21 +01002855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002856#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002857 if( rec->data_len < transform->maclen + padlen + 1 )
2858 {
2859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2860 rec->data_len,
2861 transform->maclen,
2862 padlen + 1 ) );
2863 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002864#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002865
2866 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2867 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002869
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002870 padlen++;
2871
2872 /* Regardless of the validity of the padding,
2873 * we have data_len >= padlen here. */
2874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002875#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002878 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002880#if defined(MBEDTLS_SSL_DEBUG_ALL)
2881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002882 "should be no more than %d",
2883 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002884#endif
Paul Bakker45829992013-01-03 14:52:21 +01002885 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 }
2887 }
2888 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002889#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2890#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2891 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002893 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002894 /* The padding check involves a series of up to 256
2895 * consecutive memory reads at the end of the record
2896 * plaintext buffer. In order to hide the length and
2897 * validity of the padding, always perform exactly
2898 * `min(256,plaintext_len)` reads (but take into account
2899 * only the last `padlen` bytes for the padding check). */
2900 size_t pad_count = 0;
2901 size_t real_count = 0;
2902 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002903
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 /* Index of first padding byte; it has been ensured above
2905 * that the subtraction is safe. */
2906 size_t const padding_idx = rec->data_len - padlen;
2907 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2908 size_t const start_idx = rec->data_len - num_checks;
2909 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002910
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002911 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002912 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002913 real_count |= ( idx >= padding_idx );
2914 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002915 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002916 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002919 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002921#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002922 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002923 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002924 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002925#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2926 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002930 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002931
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002932 /* If the padding was found to be invalid, padlen == 0
2933 * and the subtraction is safe. If the padding was found valid,
2934 * padlen hasn't been changed and the previous assertion
2935 * data_len >= padlen still holds. */
2936 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002937 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002938 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002939#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002940 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002941 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002942 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2943 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002944 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002945
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002946#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002947 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002948 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002949#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002950
2951 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002952 * Authenticate if not done yet.
2953 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002954 */
Hanno Becker52344c22018-01-03 15:24:20 +00002955#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002956 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002957 {
Hanno Becker992b6872017-11-09 18:57:39 +00002958 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002959
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002960 /* If the initial value of padlen was such that
2961 * data_len < maclen + padlen + 1, then padlen
2962 * got reset to 1, and the initial check
2963 * data_len >= minlen + maclen + 1
2964 * guarantees that at this point we still
2965 * have at least data_len >= maclen.
2966 *
2967 * If the initial value of padlen was such that
2968 * data_len >= maclen + padlen + 1, then we have
2969 * subtracted either padlen + 1 (if the padding was correct)
2970 * or 0 (if the padding was incorrect) since then,
2971 * hence data_len >= maclen in any case.
2972 */
2973 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Hanno Beckercab87e62019-04-29 13:52:53 +01002975 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002977#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002979 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002980 ssl_mac( &transform->md_ctx_dec,
2981 transform->mac_dec,
2982 data, rec->data_len,
2983 rec->ctr, rec->type,
2984 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002985 }
2986 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002987#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2988#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2989 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002990 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002991 {
2992 /*
2993 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002994 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002995 *
2996 * Known timing attacks:
2997 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2998 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002999 * To compensate for different timings for the MAC calculation
3000 * depending on how much padding was removed (which is determined
3001 * by padlen), process extra_run more blocks through the hash
3002 * function.
3003 *
3004 * The formula in the paper is
3005 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3006 * where L1 is the size of the header plus the decrypted message
3007 * plus CBC padding and L2 is the size of the header plus the
3008 * decrypted message. This is for an underlying hash function
3009 * with 64-byte blocks.
3010 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3011 * correctly. We round down instead of up, so -56 is the correct
3012 * value for our calculations instead of -55.
3013 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003014 * Repeat the formula rather than defining a block_size variable.
3015 * This avoids requiring division by a variable at runtime
3016 * (which would be marginally less efficient and would require
3017 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003018 */
3019 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003020 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003021
3022 /*
3023 * The next two sizes are the minimum and maximum values of
3024 * in_msglen over all padlen values.
3025 *
3026 * They're independent of padlen, since we previously did
3027 * in_msglen -= padlen.
3028 *
3029 * Note that max_len + maclen is never more than the buffer
3030 * length, as we previously did in_msglen -= maclen too.
3031 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003032 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003033 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3034
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003035 memset( tmp, 0, sizeof( tmp ) );
3036
3037 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003038 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003039#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3040 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003041 case MBEDTLS_MD_MD5:
3042 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003044 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003045 extra_run =
3046 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3047 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 break;
3049#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003050#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003051 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003052 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003053 extra_run =
3054 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3055 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003056 break;
3057#endif
3058 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003059 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003060 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3061 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003062
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003063 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003064
Hanno Beckercab87e62019-04-29 13:52:53 +01003065 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3066 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003067 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3068 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003069 /* Make sure we access everything even when padlen > 0. This
3070 * makes the synchronisation requirements for just-in-time
3071 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003072 ssl_read_memory( data + rec->data_len, padlen );
3073 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003074
3075 /* Call mbedtls_md_process at least once due to cache attacks
3076 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003077 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003079
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003080 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003081
3082 /* Make sure we access all the memory that could contain the MAC,
3083 * before we check it in the next code block. This makes the
3084 * synchronisation requirements for just-in-time Prime+Probe
3085 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003086 ssl_read_memory( data + min_len,
3087 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003088 }
3089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3091 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003092 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3094 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003095 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003096
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003097#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003098 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3099 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003100#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003101
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003102 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3103 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#if defined(MBEDTLS_SSL_DEBUG_ALL)
3106 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003107#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003108 correct = 0;
3109 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003110 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003111 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003112
3113 /*
3114 * Finally check the correct flag
3115 */
3116 if( correct == 0 )
3117 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003118#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003119
3120 /* Make extra sure authentication was performed, exactly once */
3121 if( auth_done != 1 )
3122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3124 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003125 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003126
Hanno Beckera0e20d02019-05-15 14:03:01 +01003127#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003128 if( rec->cid_len != 0 )
3129 {
3130 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3131 &rec->type );
3132 if( ret != 0 )
3133 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3134 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003135#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003138
3139 return( 0 );
3140}
3141
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003142#undef MAC_NONE
3143#undef MAC_PLAINTEXT
3144#undef MAC_CIPHERTEXT
3145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003147/*
3148 * Compression/decompression functions
3149 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003151{
3152 int ret;
3153 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003154 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003156 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003160 if( len_pre == 0 )
3161 return( 0 );
3162
Paul Bakker2770fbd2012-07-03 13:30:23 +00003163 memcpy( msg_pre, ssl->out_msg, len_pre );
3164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003165 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003166 ssl->out_msglen ) );
3167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003168 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003169 ssl->out_msg, ssl->out_msglen );
3170
Paul Bakker48916f92012-09-16 19:57:18 +00003171 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3172 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3173 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003174 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175
Paul Bakker48916f92012-09-16 19:57:18 +00003176 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003177 if( ret != Z_OK )
3178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3180 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181 }
3182
Angus Grattond8213d02016-05-25 20:56:48 +10003183 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003184 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003186 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003187 ssl->out_msglen ) );
3188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003189 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003190 ssl->out_msg, ssl->out_msglen );
3191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003193
3194 return( 0 );
3195}
3196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003197static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198{
3199 int ret;
3200 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003201 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003203 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003206
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003207 if( len_pre == 0 )
3208 return( 0 );
3209
Paul Bakker2770fbd2012-07-03 13:30:23 +00003210 memcpy( msg_pre, ssl->in_msg, len_pre );
3211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003213 ssl->in_msglen ) );
3214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003215 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216 ssl->in_msg, ssl->in_msglen );
3217
Paul Bakker48916f92012-09-16 19:57:18 +00003218 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3219 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3220 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003221 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003222 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003223
Paul Bakker48916f92012-09-16 19:57:18 +00003224 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 if( ret != Z_OK )
3226 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3228 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003229 }
3230
Angus Grattond8213d02016-05-25 20:56:48 +10003231 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003232 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003235 ssl->in_msglen ) );
3236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003238 ssl->in_msg, ssl->in_msglen );
3239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003241
3242 return( 0 );
3243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003245
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003246#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3247static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003249#if defined(MBEDTLS_SSL_PROTO_DTLS)
3250static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003251{
3252 /* If renegotiation is not enforced, retransmit until we would reach max
3253 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003254 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003255 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003256 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003257 unsigned char doublings = 1;
3258
3259 while( ratio != 0 )
3260 {
3261 ++doublings;
3262 ratio >>= 1;
3263 }
3264
3265 if( ++ssl->renego_records_seen > doublings )
3266 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003267 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003268 return( 0 );
3269 }
3270 }
3271
3272 return( ssl_write_hello_request( ssl ) );
3273}
3274#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003275#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003276
Paul Bakker5121ce52009-01-03 21:22:43 +00003277/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003278 * Fill the input message buffer by appending data to it.
3279 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003280 *
3281 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3282 * available (from this read and/or a previous one). Otherwise, an error code
3283 * is returned (possibly EOF or WANT_READ).
3284 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003285 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3286 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3287 * since we always read a whole datagram at once.
3288 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003289 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003290 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003292int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003293{
Paul Bakker23986e52011-04-24 08:57:21 +00003294 int ret;
3295 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003298
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003299 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003302 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003303 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003304 }
3305
Angus Grattond8213d02016-05-25 20:56:48 +10003306 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3309 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003310 }
3311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003312#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003313 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003314 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003315 uint32_t timeout;
3316
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003317 /* Just to be sure */
3318 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3319 {
3320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3321 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3322 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3323 }
3324
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003325 /*
3326 * The point is, we need to always read a full datagram at once, so we
3327 * sometimes read more then requested, and handle the additional data.
3328 * It could be the rest of the current record (while fetching the
3329 * header) and/or some other records in the same datagram.
3330 */
3331
3332 /*
3333 * Move to the next record in the already read datagram if applicable
3334 */
3335 if( ssl->next_record_offset != 0 )
3336 {
3337 if( ssl->in_left < ssl->next_record_offset )
3338 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003339 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3340 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003341 }
3342
3343 ssl->in_left -= ssl->next_record_offset;
3344
3345 if( ssl->in_left != 0 )
3346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003348 ssl->next_record_offset ) );
3349 memmove( ssl->in_hdr,
3350 ssl->in_hdr + ssl->next_record_offset,
3351 ssl->in_left );
3352 }
3353
3354 ssl->next_record_offset = 0;
3355 }
3356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003357 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003358 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003359
3360 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003361 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003362 */
3363 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003366 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003367 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003368
3369 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003370 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003371 * are not at the beginning of a new record, the caller did something
3372 * wrong.
3373 */
3374 if( ssl->in_left != 0 )
3375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3377 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003378 }
3379
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003380 /*
3381 * Don't even try to read if time's out already.
3382 * This avoids by-passing the timer when repeatedly receiving messages
3383 * that will end up being dropped.
3384 */
3385 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003386 {
3387 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003388 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003389 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003390 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003391 {
Angus Grattond8213d02016-05-25 20:56:48 +10003392 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003393
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003394 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003395 timeout = ssl->handshake->retransmit_timeout;
3396 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003397 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003399 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003401 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003402 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3403 timeout );
3404 else
3405 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003407 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003408
3409 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003410 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003411 }
3412
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003413 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003416 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003419 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003420 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3421 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003423 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003424 }
3425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003429 return( ret );
3430 }
3431
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003432 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003433 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003435 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003437 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003438 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003439 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003441 return( ret );
3442 }
3443
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003444 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003445 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003446#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003447 }
3448
Paul Bakker5121ce52009-01-03 21:22:43 +00003449 if( ret < 0 )
3450 return( ret );
3451
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003452 ssl->in_left = ret;
3453 }
3454 else
3455#endif
3456 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003458 ssl->in_left, nb_want ) );
3459
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003460 while( ssl->in_left < nb_want )
3461 {
3462 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003463
3464 if( ssl_check_timer( ssl ) != 0 )
3465 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3466 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003467 {
3468 if( ssl->f_recv_timeout != NULL )
3469 {
3470 ret = ssl->f_recv_timeout( ssl->p_bio,
3471 ssl->in_hdr + ssl->in_left, len,
3472 ssl->conf->read_timeout );
3473 }
3474 else
3475 {
3476 ret = ssl->f_recv( ssl->p_bio,
3477 ssl->in_hdr + ssl->in_left, len );
3478 }
3479 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003482 ssl->in_left, nb_want ) );
3483 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003484
3485 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003486 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003487
3488 if( ret < 0 )
3489 return( ret );
3490
mohammad160352aecb92018-03-28 23:41:40 -07003491 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003492 {
Darryl Green11999bb2018-03-13 15:22:58 +00003493 MBEDTLS_SSL_DEBUG_MSG( 1,
3494 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003495 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003496 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3497 }
3498
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003499 ssl->in_left += ret;
3500 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003501 }
3502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003503 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003504
3505 return( 0 );
3506}
3507
3508/*
3509 * Flush any data not yet written
3510 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003511int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003512{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003513 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003514 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003517
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003518 if( ssl->f_send == NULL )
3519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003521 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003522 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003523 }
3524
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003525 /* Avoid incrementing counter if data is flushed */
3526 if( ssl->out_left == 0 )
3527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003529 return( 0 );
3530 }
3531
Paul Bakker5121ce52009-01-03 21:22:43 +00003532 while( ssl->out_left > 0 )
3533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003534 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003535 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003536
Hanno Becker2b1e3542018-08-06 11:19:13 +01003537 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003538 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003539
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003540 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003541
3542 if( ret <= 0 )
3543 return( ret );
3544
mohammad160352aecb92018-03-28 23:41:40 -07003545 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003546 {
Darryl Green11999bb2018-03-13 15:22:58 +00003547 MBEDTLS_SSL_DEBUG_MSG( 1,
3548 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003549 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003550 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3551 }
3552
Paul Bakker5121ce52009-01-03 21:22:43 +00003553 ssl->out_left -= ret;
3554 }
3555
Hanno Becker2b1e3542018-08-06 11:19:13 +01003556#if defined(MBEDTLS_SSL_PROTO_DTLS)
3557 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003558 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003559 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003560 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003561 else
3562#endif
3563 {
3564 ssl->out_hdr = ssl->out_buf + 8;
3565 }
3566 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003569
3570 return( 0 );
3571}
3572
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003573/*
3574 * Functions to handle the DTLS retransmission state machine
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577/*
3578 * Append current handshake message to current outgoing flight
3579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003581{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003582 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003583 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3584 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3585 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586
3587 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003588 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003592 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593 }
3594
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003595 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003596 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003598 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003599 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003600 }
3601
3602 /* Copy current handshake message with headers */
3603 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3604 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003605 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003606 msg->next = NULL;
3607
3608 /* Append to the current flight */
3609 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003610 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003611 else
3612 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003613 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003614 while( cur->next != NULL )
3615 cur = cur->next;
3616 cur->next = msg;
3617 }
3618
Hanno Becker3b235902018-08-06 09:54:53 +01003619 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003620 return( 0 );
3621}
3622
3623/*
3624 * Free the current flight of handshake messages
3625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003627{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 mbedtls_ssl_flight_item *cur = flight;
3629 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003630
3631 while( cur != NULL )
3632 {
3633 next = cur->next;
3634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635 mbedtls_free( cur->p );
3636 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003637
3638 cur = next;
3639 }
3640}
3641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3643static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003644#endif
3645
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003646/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003647 * Swap transform_out and out_ctr with the alternative ones
3648 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003651 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003652 unsigned char tmp_out_ctr[8];
3653
3654 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003657 return;
3658 }
3659
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003660 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003662 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003663 tmp_transform = ssl->transform_out;
3664 ssl->transform_out = ssl->handshake->alt_transform_out;
3665 ssl->handshake->alt_transform_out = tmp_transform;
3666
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003667 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003668 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3669 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003670 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003671
3672 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003673 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003675#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3676 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3681 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003682 }
3683 }
3684#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003685}
3686
3687/*
3688 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003689 */
3690int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3691{
3692 int ret = 0;
3693
3694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3695
3696 ret = mbedtls_ssl_flight_transmit( ssl );
3697
3698 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3699
3700 return( ret );
3701}
3702
3703/*
3704 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705 *
3706 * Need to remember the current message in case flush_output returns
3707 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003708 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003709 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003710int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003711{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003712 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003715 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003716 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003718
3719 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003720 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003721 ssl_swap_epochs( ssl );
3722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003723 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003724 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003725
3726 while( ssl->handshake->cur_msg != NULL )
3727 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003728 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003729 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003730
Hanno Beckere1dcb032018-08-17 16:47:58 +01003731 int const is_finished =
3732 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3733 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3734
Hanno Becker04da1892018-08-14 13:22:10 +01003735 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3736 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3737
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003738 /* Swap epochs before sending Finished: we can't do it after
3739 * sending ChangeCipherSpec, in case write returns WANT_READ.
3740 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003741 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003742 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003743 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003744 ssl_swap_epochs( ssl );
3745 }
3746
Hanno Becker67bc7c32018-08-06 11:33:50 +01003747 ret = ssl_get_remaining_payload_in_datagram( ssl );
3748 if( ret < 0 )
3749 return( ret );
3750 max_frag_len = (size_t) ret;
3751
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003752 /* CCS is copied as is, while HS messages may need fragmentation */
3753 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3754 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003755 if( max_frag_len == 0 )
3756 {
3757 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3758 return( ret );
3759
3760 continue;
3761 }
3762
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003763 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003764 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003765 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003766
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003767 /* Update position inside current message */
3768 ssl->handshake->cur_msg_p += cur->len;
3769 }
3770 else
3771 {
3772 const unsigned char * const p = ssl->handshake->cur_msg_p;
3773 const size_t hs_len = cur->len - 12;
3774 const size_t frag_off = p - ( cur->p + 12 );
3775 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003776 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003777
Hanno Beckere1dcb032018-08-17 16:47:58 +01003778 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003779 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003780 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003781 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003782
Hanno Becker67bc7c32018-08-06 11:33:50 +01003783 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3784 return( ret );
3785
3786 continue;
3787 }
3788 max_hs_frag_len = max_frag_len - 12;
3789
3790 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3791 max_hs_frag_len : rem_len;
3792
3793 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003794 {
3795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003796 (unsigned) cur_hs_frag_len,
3797 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003798 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003799
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003800 /* Messages are stored with handshake headers as if not fragmented,
3801 * copy beginning of headers then fill fragmentation fields.
3802 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3803 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003804
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3806 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3807 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3808
Hanno Becker67bc7c32018-08-06 11:33:50 +01003809 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3810 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3811 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003812
3813 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3814
Hanno Becker3f7b9732018-08-28 09:53:25 +01003815 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003816 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3817 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003818 ssl->out_msgtype = cur->type;
3819
3820 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003821 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003822 }
3823
3824 /* If done with the current message move to the next one if any */
3825 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3826 {
3827 if( cur->next != NULL )
3828 {
3829 ssl->handshake->cur_msg = cur->next;
3830 ssl->handshake->cur_msg_p = cur->next->p + 12;
3831 }
3832 else
3833 {
3834 ssl->handshake->cur_msg = NULL;
3835 ssl->handshake->cur_msg_p = NULL;
3836 }
3837 }
3838
3839 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003840 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003843 return( ret );
3844 }
3845 }
3846
Hanno Becker67bc7c32018-08-06 11:33:50 +01003847 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3848 return( ret );
3849
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003850 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3852 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003853 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003856 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3857 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003858
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003859 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003860
3861 return( 0 );
3862}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003863
3864/*
3865 * To be called when the last message of an incoming flight is received.
3866 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003867void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003868{
3869 /* We won't need to resend that one any more */
3870 ssl_flight_free( ssl->handshake->flight );
3871 ssl->handshake->flight = NULL;
3872 ssl->handshake->cur_msg = NULL;
3873
3874 /* The next incoming flight will start with this msg_seq */
3875 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3876
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003877 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003878 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003879
Hanno Becker0271f962018-08-16 13:23:47 +01003880 /* Clear future message buffering structure. */
3881 ssl_buffering_free( ssl );
3882
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003883 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003884 ssl_set_timer( ssl, 0 );
3885
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003886 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3887 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003888 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003890 }
3891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003892 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003893}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003894
3895/*
3896 * To be called when the last message of an outgoing flight is send.
3897 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003898void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003899{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003900 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003901 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3904 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003906 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003907 }
3908 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003910}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003911#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003912
Paul Bakker5121ce52009-01-03 21:22:43 +00003913/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003914 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003915 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003916
3917/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003918 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003919 *
3920 * - fill in handshake headers
3921 * - update handshake checksum
3922 * - DTLS: save message for resending
3923 * - then pass to the record layer
3924 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003925 * DTLS: except for HelloRequest, messages are only queued, and will only be
3926 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003927 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003928 * Inputs:
3929 * - ssl->out_msglen: 4 + actual handshake message len
3930 * (4 is the size of handshake headers for TLS)
3931 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3932 * - ssl->out_msg + 4: the handshake message body
3933 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003934 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003935 * - ssl->out_msglen: the length of the record contents
3936 * (including handshake headers but excluding record headers)
3937 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003938 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003939int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003940{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003941 int ret;
3942 const size_t hs_len = ssl->out_msglen - 4;
3943 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003944
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3946
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003947 /*
3948 * Sanity checks
3949 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003950 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003951 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3952 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003953 /* In SSLv3, the client might send a NoCertificate alert. */
3954#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3955 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3956 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3957 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3958#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3959 {
3960 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3961 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3962 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003963 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003964
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003965 /* Whenever we send anything different from a
3966 * HelloRequest we should be in a handshake - double check. */
3967 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3968 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003969 ssl->handshake == NULL )
3970 {
3971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3972 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3973 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003975#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003976 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003977 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003979 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3981 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003982 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003983#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003984
Hanno Beckerb50a2532018-08-06 11:52:54 +01003985 /* Double-check that we did not exceed the bounds
3986 * of the outgoing record buffer.
3987 * This should never fail as the various message
3988 * writing functions must obey the bounds of the
3989 * outgoing record buffer, but better be safe.
3990 *
3991 * Note: We deliberately do not check for the MTU or MFL here.
3992 */
3993 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3994 {
3995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3996 "size %u, maximum %u",
3997 (unsigned) ssl->out_msglen,
3998 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3999 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4000 }
4001
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004002 /*
4003 * Fill handshake headers
4004 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004005 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004006 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004007 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4008 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4009 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004010
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004011 /*
4012 * DTLS has additional fields in the Handshake layer,
4013 * between the length field and the actual payload:
4014 * uint16 message_seq;
4015 * uint24 fragment_offset;
4016 * uint24 fragment_length;
4017 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004018#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004019 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004020 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004021 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004022 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004023 {
4024 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4025 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004026 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004027 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4029 }
4030
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004031 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004033
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004034 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004035 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004036 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004037 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4038 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4039 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004040 }
4041 else
4042 {
4043 ssl->out_msg[4] = 0;
4044 ssl->out_msg[5] = 0;
4045 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004046
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004047 /* Handshake hashes are computed without fragmentation,
4048 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004049 memset( ssl->out_msg + 6, 0x00, 3 );
4050 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004051 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004052#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004053
Hanno Becker0207e532018-08-28 10:28:28 +01004054 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004055 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4056 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004057 }
4058
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004059 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004061 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004062 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4063 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064 {
4065 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004067 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068 return( ret );
4069 }
4070 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004072#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004073 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004074 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004075 {
4076 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4077 return( ret );
4078 }
4079 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004080
4081 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4082
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004083 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004084}
4085
4086/*
4087 * Record layer functions
4088 */
4089
4090/*
4091 * Write current record.
4092 *
4093 * Uses:
4094 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4095 * - ssl->out_msglen: length of the record content (excl headers)
4096 * - ssl->out_msg: record content
4097 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004098int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004099{
4100 int ret, done = 0;
4101 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004102 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004103
4104 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004107 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004109 {
4110 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004112 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004113 return( ret );
4114 }
4115
4116 len = ssl->out_msglen;
4117 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004120#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4121 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004122 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004125 ret = mbedtls_ssl_hw_record_write( ssl );
4126 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4129 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004130 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004131
4132 if( ret == 0 )
4133 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004134 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004136 if( !done )
4137 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004138 unsigned i;
4139 size_t protected_record_size;
4140
Hanno Becker6430faf2019-05-08 11:57:13 +01004141 /* Skip writing the record content type to after the encryption,
4142 * as it may change when using the CID extension. */
4143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004145 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004146
Hanno Becker19859472018-08-06 09:40:20 +01004147 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004148 ssl->out_len[0] = (unsigned char)( len >> 8 );
4149 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004150
Paul Bakker48916f92012-09-16 19:57:18 +00004151 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004152 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004153 mbedtls_record rec;
4154
4155 rec.buf = ssl->out_iv;
4156 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4157 ( ssl->out_iv - ssl->out_buf );
4158 rec.data_len = ssl->out_msglen;
4159 rec.data_offset = ssl->out_msg - rec.buf;
4160
4161 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4162 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4163 ssl->conf->transport, rec.ver );
4164 rec.type = ssl->out_msgtype;
4165
Hanno Beckera0e20d02019-05-15 14:03:01 +01004166#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004167 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004168 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004169#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004170
Hanno Beckera18d1322018-01-03 14:27:32 +00004171 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004172 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004174 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004175 return( ret );
4176 }
4177
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004178 if( rec.data_offset != 0 )
4179 {
4180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4182 }
4183
Hanno Becker6430faf2019-05-08 11:57:13 +01004184 /* Update the record content type and CID. */
4185 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004186#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004187 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004188#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004189 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004190 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4191 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004192 }
4193
Hanno Becker5903de42019-05-03 14:46:38 +01004194 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004195
4196#if defined(MBEDTLS_SSL_PROTO_DTLS)
4197 /* In case of DTLS, double-check that we don't exceed
4198 * the remaining space in the datagram. */
4199 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4200 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004201 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004202 if( ret < 0 )
4203 return( ret );
4204
4205 if( protected_record_size > (size_t) ret )
4206 {
4207 /* Should never happen */
4208 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4209 }
4210 }
4211#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004212
Hanno Becker6430faf2019-05-08 11:57:13 +01004213 /* Now write the potentially updated record content type. */
4214 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004217 "version = [%d:%d], msglen = %d",
4218 ssl->out_hdr[0], ssl->out_hdr[1],
4219 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004221 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004222 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004223
4224 ssl->out_left += protected_record_size;
4225 ssl->out_hdr += protected_record_size;
4226 ssl_update_out_pointers( ssl, ssl->transform_out );
4227
Hanno Becker04484622018-08-06 09:49:38 +01004228 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4229 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4230 break;
4231
4232 /* The loop goes to its end iff the counter is wrapping */
4233 if( i == ssl_ep_len( ssl ) )
4234 {
4235 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4236 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4237 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004238 }
4239
Hanno Becker67bc7c32018-08-06 11:33:50 +01004240#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004241 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4242 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004243 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004244 size_t remaining;
4245 ret = ssl_get_remaining_payload_in_datagram( ssl );
4246 if( ret < 0 )
4247 {
4248 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4249 ret );
4250 return( ret );
4251 }
4252
4253 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004254 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004255 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004256 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004257 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004258 else
4259 {
Hanno Becker513815a2018-08-20 11:56:09 +01004260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004261 }
4262 }
4263#endif /* MBEDTLS_SSL_PROTO_DTLS */
4264
4265 if( ( flush == SSL_FORCE_FLUSH ) &&
4266 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004269 return( ret );
4270 }
4271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004272 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004273
4274 return( 0 );
4275}
4276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004277#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004278
4279static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4280{
4281 if( ssl->in_msglen < ssl->in_hslen ||
4282 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4283 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4284 {
4285 return( 1 );
4286 }
4287 return( 0 );
4288}
Hanno Becker44650b72018-08-16 12:51:11 +01004289
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004290static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004291{
4292 return( ( ssl->in_msg[9] << 16 ) |
4293 ( ssl->in_msg[10] << 8 ) |
4294 ssl->in_msg[11] );
4295}
4296
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004297static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004298{
4299 return( ( ssl->in_msg[6] << 16 ) |
4300 ( ssl->in_msg[7] << 8 ) |
4301 ssl->in_msg[8] );
4302}
4303
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004304static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004305{
4306 uint32_t msg_len, frag_off, frag_len;
4307
4308 msg_len = ssl_get_hs_total_len( ssl );
4309 frag_off = ssl_get_hs_frag_off( ssl );
4310 frag_len = ssl_get_hs_frag_len( ssl );
4311
4312 if( frag_off > msg_len )
4313 return( -1 );
4314
4315 if( frag_len > msg_len - frag_off )
4316 return( -1 );
4317
4318 if( frag_len + 12 > ssl->in_msglen )
4319 return( -1 );
4320
4321 return( 0 );
4322}
4323
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004324/*
4325 * Mark bits in bitmask (used for DTLS HS reassembly)
4326 */
4327static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4328{
4329 unsigned int start_bits, end_bits;
4330
4331 start_bits = 8 - ( offset % 8 );
4332 if( start_bits != 8 )
4333 {
4334 size_t first_byte_idx = offset / 8;
4335
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004336 /* Special case */
4337 if( len <= start_bits )
4338 {
4339 for( ; len != 0; len-- )
4340 mask[first_byte_idx] |= 1 << ( start_bits - len );
4341
4342 /* Avoid potential issues with offset or len becoming invalid */
4343 return;
4344 }
4345
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004346 offset += start_bits; /* Now offset % 8 == 0 */
4347 len -= start_bits;
4348
4349 for( ; start_bits != 0; start_bits-- )
4350 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4351 }
4352
4353 end_bits = len % 8;
4354 if( end_bits != 0 )
4355 {
4356 size_t last_byte_idx = ( offset + len ) / 8;
4357
4358 len -= end_bits; /* Now len % 8 == 0 */
4359
4360 for( ; end_bits != 0; end_bits-- )
4361 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4362 }
4363
4364 memset( mask + offset / 8, 0xFF, len / 8 );
4365}
4366
4367/*
4368 * Check that bitmask is full
4369 */
4370static int ssl_bitmask_check( unsigned char *mask, size_t len )
4371{
4372 size_t i;
4373
4374 for( i = 0; i < len / 8; i++ )
4375 if( mask[i] != 0xFF )
4376 return( -1 );
4377
4378 for( i = 0; i < len % 8; i++ )
4379 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4380 return( -1 );
4381
4382 return( 0 );
4383}
4384
Hanno Becker56e205e2018-08-16 09:06:12 +01004385/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004386static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004387 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004388{
Hanno Becker56e205e2018-08-16 09:06:12 +01004389 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004390
Hanno Becker56e205e2018-08-16 09:06:12 +01004391 alloc_len = 12; /* Handshake header */
4392 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004393
Hanno Beckerd07df862018-08-16 09:14:58 +01004394 if( add_bitmap )
4395 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004396
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004397 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004398}
Hanno Becker56e205e2018-08-16 09:06:12 +01004399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004400#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004401
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004402static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004403{
4404 return( ( ssl->in_msg[1] << 16 ) |
4405 ( ssl->in_msg[2] << 8 ) |
4406 ssl->in_msg[3] );
4407}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004408
Simon Butcher99000142016-10-13 17:21:01 +01004409int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004414 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004415 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004416 }
4417
Hanno Becker12555c62018-08-16 12:47:53 +01004418 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004421 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004422 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004425 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004426 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004427 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004428 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004429
Hanno Becker44650b72018-08-16 12:51:11 +01004430 if( ssl_check_hs_header( ssl ) != 0 )
4431 {
4432 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4433 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4434 }
4435
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004436 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004437 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4438 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4439 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4440 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004441 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004442 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4443 {
4444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4445 recv_msg_seq,
4446 ssl->handshake->in_msg_seq ) );
4447 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4448 }
4449
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004450 /* Retransmit only on last message from previous flight, to avoid
4451 * too many retransmissions.
4452 * Besides, No sane server ever retransmits HelloVerifyRequest */
4453 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004457 "message_seq = %d, start_of_flight = %d",
4458 recv_msg_seq,
4459 ssl->handshake->in_flight_start_seq ) );
4460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004462 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004463 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004464 return( ret );
4465 }
4466 }
4467 else
4468 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004470 "message_seq = %d, expected = %d",
4471 recv_msg_seq,
4472 ssl->handshake->in_msg_seq ) );
4473 }
4474
Hanno Becker90333da2017-10-10 11:27:13 +01004475 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004476 }
4477 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004478
Hanno Becker6d97ef52018-08-16 13:09:04 +01004479 /* Message reassembly is handled alongside buffering of future
4480 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004481 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004482 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004483 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004486 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004487 }
4488 }
4489 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004491 /* With TLS we don't handle fragmentation (for now) */
4492 if( ssl->in_msglen < ssl->in_hslen )
4493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4495 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004496 }
4497
Simon Butcher99000142016-10-13 17:21:01 +01004498 return( 0 );
4499}
4500
4501void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4502{
Hanno Becker0271f962018-08-16 13:23:47 +01004503 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004504
Hanno Becker0271f962018-08-16 13:23:47 +01004505 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004506 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004507 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004508 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004509
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004510 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004511#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004512 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004513 ssl->handshake != NULL )
4514 {
Hanno Becker0271f962018-08-16 13:23:47 +01004515 unsigned offset;
4516 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004517
Hanno Becker0271f962018-08-16 13:23:47 +01004518 /* Increment handshake sequence number */
4519 hs->in_msg_seq++;
4520
4521 /*
4522 * Clear up handshake buffering and reassembly structure.
4523 */
4524
4525 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004526 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004527
4528 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004529 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4530 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004531 offset++, hs_buf++ )
4532 {
4533 *hs_buf = *(hs_buf + 1);
4534 }
4535
4536 /* Create a fresh last entry */
4537 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004538 }
4539#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004540}
4541
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004542/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004543 * DTLS anti-replay: RFC 6347 4.1.2.6
4544 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004545 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4546 * Bit n is set iff record number in_window_top - n has been seen.
4547 *
4548 * Usually, in_window_top is the last record number seen and the lsb of
4549 * in_window is set. The only exception is the initial state (record number 0
4550 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004551 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004552#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4553static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004554{
4555 ssl->in_window_top = 0;
4556 ssl->in_window = 0;
4557}
4558
4559static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4560{
4561 return( ( (uint64_t) buf[0] << 40 ) |
4562 ( (uint64_t) buf[1] << 32 ) |
4563 ( (uint64_t) buf[2] << 24 ) |
4564 ( (uint64_t) buf[3] << 16 ) |
4565 ( (uint64_t) buf[4] << 8 ) |
4566 ( (uint64_t) buf[5] ) );
4567}
4568
4569/*
4570 * Return 0 if sequence number is acceptable, -1 otherwise
4571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004572int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004573{
4574 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4575 uint64_t bit;
4576
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004577 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004578 return( 0 );
4579
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580 if( rec_seqnum > ssl->in_window_top )
4581 return( 0 );
4582
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004583 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004584
4585 if( bit >= 64 )
4586 return( -1 );
4587
4588 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4589 return( -1 );
4590
4591 return( 0 );
4592}
4593
4594/*
4595 * Update replay window on new validated record
4596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004597void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004598{
4599 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4600
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004601 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004602 return;
4603
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004604 if( rec_seqnum > ssl->in_window_top )
4605 {
4606 /* Update window_top and the contents of the window */
4607 uint64_t shift = rec_seqnum - ssl->in_window_top;
4608
4609 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004610 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004612 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004613 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004614 ssl->in_window |= 1;
4615 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004616
4617 ssl->in_window_top = rec_seqnum;
4618 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004619 else
4620 {
4621 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004622 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004623
4624 if( bit < 64 ) /* Always true, but be extra sure */
4625 ssl->in_window |= (uint64_t) 1 << bit;
4626 }
4627}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004628#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004629
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004630#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004631/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004632static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4633
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004634/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004635 * Without any SSL context, check if a datagram looks like a ClientHello with
4636 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004637 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004638 *
4639 * - if cookie is valid, return 0
4640 * - if ClientHello looks superficially valid but cookie is not,
4641 * fill obuf and set olen, then
4642 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4643 * - otherwise return a specific error code
4644 */
4645static int ssl_check_dtls_clihlo_cookie(
4646 mbedtls_ssl_cookie_write_t *f_cookie_write,
4647 mbedtls_ssl_cookie_check_t *f_cookie_check,
4648 void *p_cookie,
4649 const unsigned char *cli_id, size_t cli_id_len,
4650 const unsigned char *in, size_t in_len,
4651 unsigned char *obuf, size_t buf_len, size_t *olen )
4652{
4653 size_t sid_len, cookie_len;
4654 unsigned char *p;
4655
4656 if( f_cookie_write == NULL || f_cookie_check == NULL )
4657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4658
4659 /*
4660 * Structure of ClientHello with record and handshake headers,
4661 * and expected values. We don't need to check a lot, more checks will be
4662 * done when actually parsing the ClientHello - skipping those checks
4663 * avoids code duplication and does not make cookie forging any easier.
4664 *
4665 * 0-0 ContentType type; copied, must be handshake
4666 * 1-2 ProtocolVersion version; copied
4667 * 3-4 uint16 epoch; copied, must be 0
4668 * 5-10 uint48 sequence_number; copied
4669 * 11-12 uint16 length; (ignored)
4670 *
4671 * 13-13 HandshakeType msg_type; (ignored)
4672 * 14-16 uint24 length; (ignored)
4673 * 17-18 uint16 message_seq; copied
4674 * 19-21 uint24 fragment_offset; copied, must be 0
4675 * 22-24 uint24 fragment_length; (ignored)
4676 *
4677 * 25-26 ProtocolVersion client_version; (ignored)
4678 * 27-58 Random random; (ignored)
4679 * 59-xx SessionID session_id; 1 byte len + sid_len content
4680 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4681 * ...
4682 *
4683 * Minimum length is 61 bytes.
4684 */
4685 if( in_len < 61 ||
4686 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4687 in[3] != 0 || in[4] != 0 ||
4688 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4689 {
4690 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4691 }
4692
4693 sid_len = in[59];
4694 if( sid_len > in_len - 61 )
4695 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4696
4697 cookie_len = in[60 + sid_len];
4698 if( cookie_len > in_len - 60 )
4699 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4700
4701 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4702 cli_id, cli_id_len ) == 0 )
4703 {
4704 /* Valid cookie */
4705 return( 0 );
4706 }
4707
4708 /*
4709 * If we get here, we've got an invalid cookie, let's prepare HVR.
4710 *
4711 * 0-0 ContentType type; copied
4712 * 1-2 ProtocolVersion version; copied
4713 * 3-4 uint16 epoch; copied
4714 * 5-10 uint48 sequence_number; copied
4715 * 11-12 uint16 length; olen - 13
4716 *
4717 * 13-13 HandshakeType msg_type; hello_verify_request
4718 * 14-16 uint24 length; olen - 25
4719 * 17-18 uint16 message_seq; copied
4720 * 19-21 uint24 fragment_offset; copied
4721 * 22-24 uint24 fragment_length; olen - 25
4722 *
4723 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4724 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4725 *
4726 * Minimum length is 28.
4727 */
4728 if( buf_len < 28 )
4729 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4730
4731 /* Copy most fields and adapt others */
4732 memcpy( obuf, in, 25 );
4733 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4734 obuf[25] = 0xfe;
4735 obuf[26] = 0xff;
4736
4737 /* Generate and write actual cookie */
4738 p = obuf + 28;
4739 if( f_cookie_write( p_cookie,
4740 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4741 {
4742 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4743 }
4744
4745 *olen = p - obuf;
4746
4747 /* Go back and fill length fields */
4748 obuf[27] = (unsigned char)( *olen - 28 );
4749
4750 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4751 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4752 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4753
4754 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4755 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4756
4757 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4758}
4759
4760/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004761 * Handle possible client reconnect with the same UDP quadruplet
4762 * (RFC 6347 Section 4.2.8).
4763 *
4764 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4765 * that looks like a ClientHello.
4766 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004767 * - if the input looks like a ClientHello without cookies,
4768 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004769 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004770 * - if the input looks like a ClientHello with a valid cookie,
4771 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004772 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004774 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004775 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004776 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4777 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004778 */
4779static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4780{
4781 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004782 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004783
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004784 ret = ssl_check_dtls_clihlo_cookie(
4785 ssl->conf->f_cookie_write,
4786 ssl->conf->f_cookie_check,
4787 ssl->conf->p_cookie,
4788 ssl->cli_id, ssl->cli_id_len,
4789 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004790 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004791
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004792 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4793
4794 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004795 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004796 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004797 * If the error is permanent we'll catch it later,
4798 * if it's not, then hopefully it'll work next time. */
4799 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4800
4801 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004802 }
4803
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004804 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004805 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004806 /* Got a valid cookie, partially reset context */
4807 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4808 {
4809 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4810 return( ret );
4811 }
4812
4813 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004814 }
4815
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004816 return( ret );
4817}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004818#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004819
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004820static int ssl_check_record_type( uint8_t record_type )
4821{
4822 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4823 record_type != MBEDTLS_SSL_MSG_ALERT &&
4824 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4825 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4826 {
4827 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4828 }
4829
4830 return( 0 );
4831}
4832
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004833/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004834 * ContentType type;
4835 * ProtocolVersion version;
4836 * uint16 epoch; // DTLS only
4837 * uint48 sequence_number; // DTLS only
4838 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004839 *
4840 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004841 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004842 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4843 *
4844 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004845 * 1. proceed with the record if this function returns 0
4846 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4847 * 3. return CLIENT_RECONNECT if this function return that value
4848 * 4. drop the whole datagram if this function returns anything else.
4849 * Point 2 is needed when the peer is resending, and we have already received
4850 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004851 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004852static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004853{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004854 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004855 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004856
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004857 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004858
Paul Bakker5121ce52009-01-03 21:22:43 +00004859 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004860 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004861
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004862 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004863#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004864 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4865 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4866 ssl->conf->cid_len != 0 )
4867 {
4868 /* Shift pointers to account for record header including CID
4869 * struct {
4870 * ContentType special_type = tls12_cid;
4871 * ProtocolVersion version;
4872 * uint16 epoch;
4873 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004874 * opaque cid[cid_length]; // Additional field compared to
4875 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004876 * uint16 length;
4877 * opaque enc_content[DTLSCiphertext.length];
4878 * } DTLSCiphertext;
4879 */
4880
4881 /* So far, we only support static CID lengths
4882 * fixed in the configuration. */
4883 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4884 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4885 }
4886 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004887#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004888 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004889 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004891
4892#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004893 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4894 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004895 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4896#endif /* MBEDTLS_SSL_PROTO_DTLS */
4897 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4898 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004900 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004901 }
4902
4903 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004904 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004905 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004906 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4907 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004908 }
4909
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004910 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4913 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004914 }
4915
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004916 /* Now that the total length of the record header is known, ensure
4917 * that the current datagram is large enough to hold it.
4918 * This would fail, for example, if we received a datagram of
4919 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4920 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4921 if( ret != 0 )
4922 {
4923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4924 return( ret );
4925 }
4926 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4927
4928 /* Parse and validate record length
4929 * This must happen after the CID parsing because
4930 * its position in the record header depends on
4931 * the presence of a CID. */
4932
4933 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004934 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004935 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004936 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004937 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4938 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004939 }
4940
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004941 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004942 "version = [%d:%d], msglen = %d",
4943 ssl->in_msgtype,
4944 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004945
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004946 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004947 * DTLS-related tests.
4948 * Check epoch before checking length constraint because
4949 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4950 * message gets duplicated before the corresponding Finished message,
4951 * the second ChangeCipherSpec should be discarded because it belongs
4952 * to an old epoch, but not because its length is shorter than
4953 * the minimum record length for packets using the new record transform.
4954 * Note that these two kinds of failures are handled differently,
4955 * as an unexpected record is silently skipped but an invalid
4956 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004957 */
4958#if defined(MBEDTLS_SSL_PROTO_DTLS)
4959 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4960 {
4961 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4962
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004963 /* Check epoch (and sequence number) with DTLS */
4964 if( rec_epoch != ssl->in_epoch )
4965 {
4966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4967 "expected %d, received %d",
4968 ssl->in_epoch, rec_epoch ) );
4969
4970#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4971 /*
4972 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4973 * access the first byte of record content (handshake type), as we
4974 * have an active transform (possibly iv_len != 0), so use the
4975 * fact that the record header len is 13 instead.
4976 */
4977 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4978 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4979 rec_epoch == 0 &&
4980 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4981 ssl->in_left > 13 &&
4982 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4983 {
4984 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4985 "from the same port" ) );
4986 return( ssl_handle_possible_reconnect( ssl ) );
4987 }
4988 else
4989#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004990 {
4991 /* Consider buffering the record. */
4992 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4993 {
4994 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4995 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4996 }
4997
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004998 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004999 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005000 }
5001
5002#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5003 /* Replay detection only works for the current epoch */
5004 if( rec_epoch == ssl->in_epoch &&
5005 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5006 {
5007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5008 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5009 }
5010#endif
5011 }
5012#endif /* MBEDTLS_SSL_PROTO_DTLS */
5013
Hanno Becker52c6dc62017-05-26 16:07:36 +01005014
5015 /* Check length against bounds of the current transform and version */
5016 if( ssl->transform_in == NULL )
5017 {
5018 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005019 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005020 {
5021 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5022 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5023 }
5024 }
5025 else
5026 {
5027 if( ssl->in_msglen < ssl->transform_in->minlen )
5028 {
5029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5030 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5031 }
5032
5033#if defined(MBEDTLS_SSL_PROTO_SSL3)
5034 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005035 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005036 {
5037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5038 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5039 }
5040#endif
5041#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5042 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5043 /*
5044 * TLS encrypted messages can have up to 256 bytes of padding
5045 */
5046 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5047 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005048 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005049 {
5050 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5051 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5052 }
5053#endif
5054 }
5055
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005056 return( 0 );
5057}
Paul Bakker5121ce52009-01-03 21:22:43 +00005058
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005059/*
5060 * If applicable, decrypt (and decompress) record content
5061 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005063{
5064 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005067 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005069#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5070 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005074 ret = mbedtls_ssl_hw_record_read( ssl );
5075 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5078 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005079 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005080
5081 if( ret == 0 )
5082 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005083 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005084#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005085 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005086 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005087 mbedtls_record rec;
5088
5089 rec.buf = ssl->in_iv;
5090 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5091 - ( ssl->in_iv - ssl->in_buf );
5092 rec.data_len = ssl->in_msglen;
5093 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005094#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005095 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005096 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005097#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005098
5099 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5100 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5101 ssl->conf->transport, rec.ver );
5102 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005103 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5104 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005105 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005106 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005107
Hanno Beckera0e20d02019-05-15 14:03:01 +01005108#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005109 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5110 ssl->conf->ignore_unexpected_cid
5111 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5112 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005113 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005114 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005115 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005116#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005117
Paul Bakker5121ce52009-01-03 21:22:43 +00005118 return( ret );
5119 }
5120
Hanno Becker6430faf2019-05-08 11:57:13 +01005121 if( ssl->in_msgtype != rec.type )
5122 {
5123 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5124 ssl->in_msgtype, rec.type ) );
5125 }
5126
5127 /* The record content type may change during decryption,
5128 * so re-read it. */
5129 ssl->in_msgtype = rec.type;
5130 /* Also update the input buffer, because unfortunately
5131 * the server-side ssl_parse_client_hello() reparses the
5132 * record header when receiving a ClientHello initiating
5133 * a renegotiation. */
5134 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005135 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005136 ssl->in_msglen = rec.data_len;
5137 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5138 ssl->in_len[1] = (unsigned char)( rec.data_len );
5139
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005140 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5141 ssl->in_msg, ssl->in_msglen );
5142
Hanno Beckera0e20d02019-05-15 14:03:01 +01005143#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005144 /* We have already checked the record content type
5145 * in ssl_parse_record_header(), failing or silently
5146 * dropping the record in the case of an unknown type.
5147 *
5148 * Since with the use of CIDs, the record content type
5149 * might change during decryption, re-check the record
5150 * content type, but treat a failure as fatal this time. */
5151 if( ssl_check_record_type( ssl->in_msgtype ) )
5152 {
5153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5154 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5155 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005156#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005157
Angus Grattond8213d02016-05-25 20:56:48 +10005158 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005159 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005160 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5161 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005162 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005163 else if( ssl->in_msglen == 0 )
5164 {
5165#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5166 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5167 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5168 {
5169 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5170 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5171 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5172 }
5173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5174
5175 ssl->nb_zero++;
5176
5177 /*
5178 * Three or more empty messages may be a DoS attack
5179 * (excessive CPU consumption).
5180 */
5181 if( ssl->nb_zero > 3 )
5182 {
5183 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005184 "messages, possible DoS attack" ) );
5185 /* Treat the records as if they were not properly authenticated,
5186 * thereby failing the connection if we see more than allowed
5187 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005188 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5189 }
5190 }
5191 else
5192 ssl->nb_zero = 0;
5193
5194#if defined(MBEDTLS_SSL_PROTO_DTLS)
5195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5196 {
5197 ; /* in_ctr read from peer, not maintained internally */
5198 }
5199 else
5200#endif
5201 {
5202 unsigned i;
5203 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5204 if( ++ssl->in_ctr[i - 1] != 0 )
5205 break;
5206
5207 /* The loop goes to its end iff the counter is wrapping */
5208 if( i == ssl_ep_len( ssl ) )
5209 {
5210 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5211 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5212 }
5213 }
5214
Paul Bakker5121ce52009-01-03 21:22:43 +00005215 }
5216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005218 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005220 {
5221 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005224 return( ret );
5225 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005226 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005228
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005230 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005233 }
5234#endif
5235
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005236 return( 0 );
5237}
5238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005239static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005240
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005241/*
5242 * Read a record.
5243 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005244 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5245 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5246 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005247 */
Hanno Becker1097b342018-08-15 14:09:41 +01005248
5249/* Helper functions for mbedtls_ssl_read_record(). */
5250static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005251static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5252static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005253
Hanno Becker327c93b2018-08-15 13:56:18 +01005254int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005255 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005256{
5257 int ret;
5258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005260
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005261 if( ssl->keep_current_message == 0 )
5262 {
5263 do {
Simon Butcher99000142016-10-13 17:21:01 +01005264
Hanno Becker26994592018-08-15 14:14:59 +01005265 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005266 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005267 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005268
Hanno Beckere74d5562018-08-15 14:26:08 +01005269 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005270 {
Hanno Becker40f50842018-08-15 14:48:01 +01005271#if defined(MBEDTLS_SSL_PROTO_DTLS)
5272 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005273
Hanno Becker40f50842018-08-15 14:48:01 +01005274 /* We only check for buffered messages if the
5275 * current datagram is fully consumed. */
5276 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005277 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005278 {
Hanno Becker40f50842018-08-15 14:48:01 +01005279 if( ssl_load_buffered_message( ssl ) == 0 )
5280 have_buffered = 1;
5281 }
5282
5283 if( have_buffered == 0 )
5284#endif /* MBEDTLS_SSL_PROTO_DTLS */
5285 {
5286 ret = ssl_get_next_record( ssl );
5287 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5288 continue;
5289
5290 if( ret != 0 )
5291 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005292 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005293 return( ret );
5294 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005295 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005296 }
5297
5298 ret = mbedtls_ssl_handle_message_type( ssl );
5299
Hanno Becker40f50842018-08-15 14:48:01 +01005300#if defined(MBEDTLS_SSL_PROTO_DTLS)
5301 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5302 {
5303 /* Buffer future message */
5304 ret = ssl_buffer_message( ssl );
5305 if( ret != 0 )
5306 return( ret );
5307
5308 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5309 }
5310#endif /* MBEDTLS_SSL_PROTO_DTLS */
5311
Hanno Becker90333da2017-10-10 11:27:13 +01005312 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5313 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005314
5315 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005316 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005317 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005318 return( ret );
5319 }
5320
Hanno Becker327c93b2018-08-15 13:56:18 +01005321 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005322 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005323 {
5324 mbedtls_ssl_update_handshake_status( ssl );
5325 }
Simon Butcher99000142016-10-13 17:21:01 +01005326 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005327 else
Simon Butcher99000142016-10-13 17:21:01 +01005328 {
Hanno Becker02f59072018-08-15 14:00:24 +01005329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005330 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005331 }
5332
5333 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5334
5335 return( 0 );
5336}
5337
Hanno Becker40f50842018-08-15 14:48:01 +01005338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005339static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005340{
Hanno Becker40f50842018-08-15 14:48:01 +01005341 if( ssl->in_left > ssl->next_record_offset )
5342 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005343
Hanno Becker40f50842018-08-15 14:48:01 +01005344 return( 0 );
5345}
5346
5347static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5348{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005349 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005350 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005351 int ret = 0;
5352
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005353 if( hs == NULL )
5354 return( -1 );
5355
Hanno Beckere00ae372018-08-20 09:39:42 +01005356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5357
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005358 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5359 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5360 {
5361 /* Check if we have seen a ChangeCipherSpec before.
5362 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005363 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005364 {
5365 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5366 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005367 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005368 }
5369
Hanno Becker39b8bc92018-08-28 17:17:13 +01005370 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005371 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5372 ssl->in_msglen = 1;
5373 ssl->in_msg[0] = 1;
5374
5375 /* As long as they are equal, the exact value doesn't matter. */
5376 ssl->in_left = 0;
5377 ssl->next_record_offset = 0;
5378
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005379 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005380 goto exit;
5381 }
Hanno Becker37f95322018-08-16 13:55:32 +01005382
Hanno Beckerb8f50142018-08-28 10:01:34 +01005383#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005384 /* Debug only */
5385 {
5386 unsigned offset;
5387 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5388 {
5389 hs_buf = &hs->buffering.hs[offset];
5390 if( hs_buf->is_valid == 1 )
5391 {
5392 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5393 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005394 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005395 }
5396 }
5397 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005398#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005399
5400 /* Check if we have buffered and/or fully reassembled the
5401 * next handshake message. */
5402 hs_buf = &hs->buffering.hs[0];
5403 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5404 {
5405 /* Synthesize a record containing the buffered HS message. */
5406 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5407 ( hs_buf->data[2] << 8 ) |
5408 hs_buf->data[3];
5409
5410 /* Double-check that we haven't accidentally buffered
5411 * a message that doesn't fit into the input buffer. */
5412 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5413 {
5414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5415 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5416 }
5417
5418 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5419 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5420 hs_buf->data, msg_len + 12 );
5421
5422 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5423 ssl->in_hslen = msg_len + 12;
5424 ssl->in_msglen = msg_len + 12;
5425 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5426
5427 ret = 0;
5428 goto exit;
5429 }
5430 else
5431 {
5432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5433 hs->in_msg_seq ) );
5434 }
5435
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005436 ret = -1;
5437
5438exit:
5439
5440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5441 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005442}
5443
Hanno Beckera02b0b42018-08-21 17:20:27 +01005444static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5445 size_t desired )
5446{
5447 int offset;
5448 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5450 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005451
Hanno Becker01315ea2018-08-21 17:22:17 +01005452 /* Get rid of future records epoch first, if such exist. */
5453 ssl_free_buffered_record( ssl );
5454
5455 /* Check if we have enough space available now. */
5456 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5457 hs->buffering.total_bytes_buffered ) )
5458 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005460 return( 0 );
5461 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005462
Hanno Becker4f432ad2018-08-28 10:02:32 +01005463 /* We don't have enough space to buffer the next expected handshake
5464 * message. Remove buffers used for future messages to gain space,
5465 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005466 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5467 offset >= 0; offset-- )
5468 {
5469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5470 offset ) );
5471
Hanno Beckerb309b922018-08-23 13:18:05 +01005472 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005473
5474 /* Check if we have enough space available now. */
5475 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5476 hs->buffering.total_bytes_buffered ) )
5477 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005479 return( 0 );
5480 }
5481 }
5482
5483 return( -1 );
5484}
5485
Hanno Becker40f50842018-08-15 14:48:01 +01005486static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5487{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005488 int ret = 0;
5489 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5490
5491 if( hs == NULL )
5492 return( 0 );
5493
5494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5495
5496 switch( ssl->in_msgtype )
5497 {
5498 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005500
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005501 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005502 break;
5503
5504 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005505 {
5506 unsigned recv_msg_seq_offset;
5507 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5508 mbedtls_ssl_hs_buffer *hs_buf;
5509 size_t msg_len = ssl->in_hslen - 12;
5510
5511 /* We should never receive an old handshake
5512 * message - double-check nonetheless. */
5513 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5514 {
5515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5516 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5517 }
5518
5519 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5520 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5521 {
5522 /* Silently ignore -- message too far in the future */
5523 MBEDTLS_SSL_DEBUG_MSG( 2,
5524 ( "Ignore future HS message with sequence number %u, "
5525 "buffering window %u - %u",
5526 recv_msg_seq, ssl->handshake->in_msg_seq,
5527 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5528
5529 goto exit;
5530 }
5531
5532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5533 recv_msg_seq, recv_msg_seq_offset ) );
5534
5535 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5536
5537 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005538 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005539 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005540 size_t reassembly_buf_sz;
5541
Hanno Becker37f95322018-08-16 13:55:32 +01005542 hs_buf->is_fragmented =
5543 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5544
5545 /* We copy the message back into the input buffer
5546 * after reassembly, so check that it's not too large.
5547 * This is an implementation-specific limitation
5548 * and not one from the standard, hence it is not
5549 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005550 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005551 {
5552 /* Ignore message */
5553 goto exit;
5554 }
5555
Hanno Beckere0b150f2018-08-21 15:51:03 +01005556 /* Check if we have enough space to buffer the message. */
5557 if( hs->buffering.total_bytes_buffered >
5558 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5559 {
5560 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5561 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5562 }
5563
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005564 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5565 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005566
5567 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5568 hs->buffering.total_bytes_buffered ) )
5569 {
5570 if( recv_msg_seq_offset > 0 )
5571 {
5572 /* If we can't buffer a future message because
5573 * of space limitations -- ignore. */
5574 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",
5575 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5576 (unsigned) hs->buffering.total_bytes_buffered ) );
5577 goto exit;
5578 }
Hanno Beckere1801392018-08-21 16:51:05 +01005579 else
5580 {
5581 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",
5582 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5583 (unsigned) hs->buffering.total_bytes_buffered ) );
5584 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005585
Hanno Beckera02b0b42018-08-21 17:20:27 +01005586 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005587 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005588 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",
5589 (unsigned) msg_len,
5590 (unsigned) reassembly_buf_sz,
5591 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005592 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005593 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5594 goto exit;
5595 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005596 }
5597
5598 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5599 msg_len ) );
5600
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005601 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5602 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005603 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005604 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005605 goto exit;
5606 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005607 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005608
5609 /* Prepare final header: copy msg_type, length and message_seq,
5610 * then add standardised fragment_offset and fragment_length */
5611 memcpy( hs_buf->data, ssl->in_msg, 6 );
5612 memset( hs_buf->data + 6, 0, 3 );
5613 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5614
5615 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005616
5617 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005618 }
5619 else
5620 {
5621 /* Make sure msg_type and length are consistent */
5622 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5623 {
5624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5625 /* Ignore */
5626 goto exit;
5627 }
5628 }
5629
Hanno Becker4422bbb2018-08-20 09:40:19 +01005630 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005631 {
5632 size_t frag_len, frag_off;
5633 unsigned char * const msg = hs_buf->data + 12;
5634
5635 /*
5636 * Check and copy current fragment
5637 */
5638
5639 /* Validation of header fields already done in
5640 * mbedtls_ssl_prepare_handshake_record(). */
5641 frag_off = ssl_get_hs_frag_off( ssl );
5642 frag_len = ssl_get_hs_frag_len( ssl );
5643
5644 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5645 frag_off, frag_len ) );
5646 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5647
5648 if( hs_buf->is_fragmented )
5649 {
5650 unsigned char * const bitmask = msg + msg_len;
5651 ssl_bitmask_set( bitmask, frag_off, frag_len );
5652 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5653 msg_len ) == 0 );
5654 }
5655 else
5656 {
5657 hs_buf->is_complete = 1;
5658 }
5659
5660 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5661 hs_buf->is_complete ? "" : "not yet " ) );
5662 }
5663
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005664 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005665 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005666
5667 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005668 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005669 break;
5670 }
5671
5672exit:
5673
5674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5675 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005676}
5677#endif /* MBEDTLS_SSL_PROTO_DTLS */
5678
Hanno Becker1097b342018-08-15 14:09:41 +01005679static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005680{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005681 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005682 * Consume last content-layer message and potentially
5683 * update in_msglen which keeps track of the contents'
5684 * consumption state.
5685 *
5686 * (1) Handshake messages:
5687 * Remove last handshake message, move content
5688 * and adapt in_msglen.
5689 *
5690 * (2) Alert messages:
5691 * Consume whole record content, in_msglen = 0.
5692 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005693 * (3) Change cipher spec:
5694 * Consume whole record content, in_msglen = 0.
5695 *
5696 * (4) Application data:
5697 * Don't do anything - the record layer provides
5698 * the application data as a stream transport
5699 * and consumes through mbedtls_ssl_read only.
5700 *
5701 */
5702
5703 /* Case (1): Handshake messages */
5704 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005705 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005706 /* Hard assertion to be sure that no application data
5707 * is in flight, as corrupting ssl->in_msglen during
5708 * ssl->in_offt != NULL is fatal. */
5709 if( ssl->in_offt != NULL )
5710 {
5711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5713 }
5714
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005715 /*
5716 * Get next Handshake message in the current record
5717 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005718
Hanno Becker4a810fb2017-05-24 16:27:30 +01005719 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005720 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005721 * current handshake content: If DTLS handshake
5722 * fragmentation is used, that's the fragment
5723 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005724 * size here is faulty and should be changed at
5725 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005726 * (2) While it doesn't seem to cause problems, one
5727 * has to be very careful not to assume that in_hslen
5728 * is always <= in_msglen in a sensible communication.
5729 * Again, it's wrong for DTLS handshake fragmentation.
5730 * The following check is therefore mandatory, and
5731 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005732 * Additionally, ssl->in_hslen might be arbitrarily out of
5733 * bounds after handling a DTLS message with an unexpected
5734 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005735 */
5736 if( ssl->in_hslen < ssl->in_msglen )
5737 {
5738 ssl->in_msglen -= ssl->in_hslen;
5739 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5740 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005741
Hanno Becker4a810fb2017-05-24 16:27:30 +01005742 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5743 ssl->in_msg, ssl->in_msglen );
5744 }
5745 else
5746 {
5747 ssl->in_msglen = 0;
5748 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005749
Hanno Becker4a810fb2017-05-24 16:27:30 +01005750 ssl->in_hslen = 0;
5751 }
5752 /* Case (4): Application data */
5753 else if( ssl->in_offt != NULL )
5754 {
5755 return( 0 );
5756 }
5757 /* Everything else (CCS & Alerts) */
5758 else
5759 {
5760 ssl->in_msglen = 0;
5761 }
5762
Hanno Becker1097b342018-08-15 14:09:41 +01005763 return( 0 );
5764}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005765
Hanno Beckere74d5562018-08-15 14:26:08 +01005766static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5767{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005768 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005769 return( 1 );
5770
5771 return( 0 );
5772}
5773
Hanno Becker5f066e72018-08-16 14:56:31 +01005774#if defined(MBEDTLS_SSL_PROTO_DTLS)
5775
5776static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5777{
5778 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5779 if( hs == NULL )
5780 return;
5781
Hanno Becker01315ea2018-08-21 17:22:17 +01005782 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005783 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005784 hs->buffering.total_bytes_buffered -=
5785 hs->buffering.future_record.len;
5786
5787 mbedtls_free( hs->buffering.future_record.data );
5788 hs->buffering.future_record.data = NULL;
5789 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005790}
5791
5792static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5793{
5794 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5795 unsigned char * rec;
5796 size_t rec_len;
5797 unsigned rec_epoch;
5798
5799 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5800 return( 0 );
5801
5802 if( hs == NULL )
5803 return( 0 );
5804
Hanno Becker5f066e72018-08-16 14:56:31 +01005805 rec = hs->buffering.future_record.data;
5806 rec_len = hs->buffering.future_record.len;
5807 rec_epoch = hs->buffering.future_record.epoch;
5808
5809 if( rec == NULL )
5810 return( 0 );
5811
Hanno Becker4cb782d2018-08-20 11:19:05 +01005812 /* Only consider loading future records if the
5813 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005814 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005815 return( 0 );
5816
Hanno Becker5f066e72018-08-16 14:56:31 +01005817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5818
5819 if( rec_epoch != ssl->in_epoch )
5820 {
5821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5822 goto exit;
5823 }
5824
5825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5826
5827 /* Double-check that the record is not too large */
5828 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5829 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5830 {
5831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5832 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5833 }
5834
5835 memcpy( ssl->in_hdr, rec, rec_len );
5836 ssl->in_left = rec_len;
5837 ssl->next_record_offset = 0;
5838
5839 ssl_free_buffered_record( ssl );
5840
5841exit:
5842 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5843 return( 0 );
5844}
5845
5846static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5847{
5848 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5849 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005850 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005851
5852 /* Don't buffer future records outside handshakes. */
5853 if( hs == NULL )
5854 return( 0 );
5855
5856 /* Only buffer handshake records (we are only interested
5857 * in Finished messages). */
5858 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5859 return( 0 );
5860
5861 /* Don't buffer more than one future epoch record. */
5862 if( hs->buffering.future_record.data != NULL )
5863 return( 0 );
5864
Hanno Becker01315ea2018-08-21 17:22:17 +01005865 /* Don't buffer record if there's not enough buffering space remaining. */
5866 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5867 hs->buffering.total_bytes_buffered ) )
5868 {
5869 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",
5870 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5871 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005872 return( 0 );
5873 }
5874
Hanno Becker5f066e72018-08-16 14:56:31 +01005875 /* Buffer record */
5876 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5877 ssl->in_epoch + 1 ) );
5878 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5879 rec_hdr_len + ssl->in_msglen );
5880
5881 /* ssl_parse_record_header() only considers records
5882 * of the next epoch as candidates for buffering. */
5883 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005884 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005885
5886 hs->buffering.future_record.data =
5887 mbedtls_calloc( 1, hs->buffering.future_record.len );
5888 if( hs->buffering.future_record.data == NULL )
5889 {
5890 /* If we run out of RAM trying to buffer a
5891 * record from the next epoch, just ignore. */
5892 return( 0 );
5893 }
5894
Hanno Becker01315ea2018-08-21 17:22:17 +01005895 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005896
Hanno Becker01315ea2018-08-21 17:22:17 +01005897 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005898 return( 0 );
5899}
5900
5901#endif /* MBEDTLS_SSL_PROTO_DTLS */
5902
Hanno Beckere74d5562018-08-15 14:26:08 +01005903static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005904{
5905 int ret;
5906
Hanno Becker5f066e72018-08-16 14:56:31 +01005907#if defined(MBEDTLS_SSL_PROTO_DTLS)
5908 /* We might have buffered a future record; if so,
5909 * and if the epoch matches now, load it.
5910 * On success, this call will set ssl->in_left to
5911 * the length of the buffered record, so that
5912 * the calls to ssl_fetch_input() below will
5913 * essentially be no-ops. */
5914 ret = ssl_load_buffered_record( ssl );
5915 if( ret != 0 )
5916 return( ret );
5917#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005918
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005919 /* Reset in pointers to default state for TLS/DTLS records,
5920 * assuming no CID and no offset between record content and
5921 * record plaintext. */
5922 ssl_update_in_pointers( ssl );
5923
5924 /* Ensure that we have enough space available for the default form
5925 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5926 * with no space for CIDs counted in). */
5927 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5928 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005930 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005931 return( ret );
5932 }
5933
5934 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005935 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005937 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5938 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005939 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005940 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5941 {
5942 ret = ssl_buffer_future_record( ssl );
5943 if( ret != 0 )
5944 return( ret );
5945
5946 /* Fall through to handling of unexpected records */
5947 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5948 }
5949
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005950 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5951 {
5952 /* Skip unexpected record (but not whole datagram) */
5953 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005954 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005955
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5957 "(header)" ) );
5958 }
5959 else
5960 {
5961 /* Skip invalid record and the rest of the datagram */
5962 ssl->next_record_offset = 0;
5963 ssl->in_left = 0;
5964
5965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5966 "(header)" ) );
5967 }
5968
5969 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005970 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005971 }
5972#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005973 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005974 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005975
5976 /*
5977 * Read and optionally decrypt the message contents
5978 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005980 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005981 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005982 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005983 return( ret );
5984 }
5985
5986 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005988 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005989 {
Hanno Becker5903de42019-05-03 14:46:38 +01005990 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005991 if( ssl->next_record_offset < ssl->in_left )
5992 {
5993 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5994 }
5995 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005996 else
5997#endif
5998 ssl->in_left = 0;
5999
6000 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006002#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006004 {
6005 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006006 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006007 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006008 /* Except when waiting for Finished as a bad mac here
6009 * probably means something went wrong in the handshake
6010 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6011 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6012 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6013 {
6014#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6015 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6016 {
6017 mbedtls_ssl_send_alert_message( ssl,
6018 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6019 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6020 }
6021#endif
6022 return( ret );
6023 }
6024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006026 if( ssl->conf->badmac_limit != 0 &&
6027 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6030 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006031 }
6032#endif
6033
Hanno Becker4a810fb2017-05-24 16:27:30 +01006034 /* As above, invalid records cause
6035 * dismissal of the whole datagram. */
6036
6037 ssl->next_record_offset = 0;
6038 ssl->in_left = 0;
6039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006040 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006041 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006042 }
6043
6044 return( ret );
6045 }
6046 else
6047#endif
6048 {
6049 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6051 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006052 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006053 mbedtls_ssl_send_alert_message( ssl,
6054 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6055 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006056 }
6057#endif
6058 return( ret );
6059 }
6060 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006061
Simon Butcher99000142016-10-13 17:21:01 +01006062 return( 0 );
6063}
6064
6065int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6066{
6067 int ret;
6068
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006069 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006070 * Handle particular types of records
6071 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006072 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006073 {
Simon Butcher99000142016-10-13 17:21:01 +01006074 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6075 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006076 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006077 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006078 }
6079
Hanno Beckere678eaa2018-08-21 14:57:46 +01006080 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006081 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006082 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006083 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6085 ssl->in_msglen ) );
6086 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006087 }
6088
Hanno Beckere678eaa2018-08-21 14:57:46 +01006089 if( ssl->in_msg[0] != 1 )
6090 {
6091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6092 ssl->in_msg[0] ) );
6093 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6094 }
6095
6096#if defined(MBEDTLS_SSL_PROTO_DTLS)
6097 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6098 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6099 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6100 {
6101 if( ssl->handshake == NULL )
6102 {
6103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6104 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6105 }
6106
6107 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6108 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6109 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006110#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006111 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006113 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006114 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006115 if( ssl->in_msglen != 2 )
6116 {
6117 /* Note: Standard allows for more than one 2 byte alert
6118 to be packed in a single message, but Mbed TLS doesn't
6119 currently support this. */
6120 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6121 ssl->in_msglen ) );
6122 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6123 }
6124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006125 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 ssl->in_msg[0], ssl->in_msg[1] ) );
6127
6128 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006129 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006134 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 }
6137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6139 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006141 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6142 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006143 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006144
6145#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6146 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6147 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6148 {
Hanno Becker90333da2017-10-10 11:27:13 +01006149 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006150 /* Will be handled when trying to parse ServerHello */
6151 return( 0 );
6152 }
6153#endif
6154
6155#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6156 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6157 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6158 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6159 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6160 {
6161 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6162 /* Will be handled in mbedtls_ssl_parse_certificate() */
6163 return( 0 );
6164 }
6165#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6166
6167 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006168 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006169 }
6170
Hanno Beckerc76c6192017-06-06 10:03:17 +01006171#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006172 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006173 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006174 /* Drop unexpected ApplicationData records,
6175 * except at the beginning of renegotiations */
6176 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6177 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6178#if defined(MBEDTLS_SSL_RENEGOTIATION)
6179 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6180 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006181#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006182 )
6183 {
6184 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6185 return( MBEDTLS_ERR_SSL_NON_FATAL );
6186 }
6187
6188 if( ssl->handshake != NULL &&
6189 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6190 {
6191 ssl_handshake_wrapup_free_hs_transform( ssl );
6192 }
6193 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006194#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006195
Paul Bakker5121ce52009-01-03 21:22:43 +00006196 return( 0 );
6197}
6198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006200{
6201 int ret;
6202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006203 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6204 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6205 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006206 {
6207 return( ret );
6208 }
6209
6210 return( 0 );
6211}
6212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006213int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006214 unsigned char level,
6215 unsigned char message )
6216{
6217 int ret;
6218
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006219 if( ssl == NULL || ssl->conf == NULL )
6220 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006223 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006226 ssl->out_msglen = 2;
6227 ssl->out_msg[0] = level;
6228 ssl->out_msg[1] = message;
6229
Hanno Becker67bc7c32018-08-06 11:33:50 +01006230 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006231 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006233 return( ret );
6234 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006235 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006236
6237 return( 0 );
6238}
6239
Hanno Beckerb9d44792019-02-08 07:19:04 +00006240#if defined(MBEDTLS_X509_CRT_PARSE_C)
6241static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6242{
6243#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6244 if( session->peer_cert != NULL )
6245 {
6246 mbedtls_x509_crt_free( session->peer_cert );
6247 mbedtls_free( session->peer_cert );
6248 session->peer_cert = NULL;
6249 }
6250#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6251 if( session->peer_cert_digest != NULL )
6252 {
6253 /* Zeroization is not necessary. */
6254 mbedtls_free( session->peer_cert_digest );
6255 session->peer_cert_digest = NULL;
6256 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6257 session->peer_cert_digest_len = 0;
6258 }
6259#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6260}
6261#endif /* MBEDTLS_X509_CRT_PARSE_C */
6262
Paul Bakker5121ce52009-01-03 21:22:43 +00006263/*
6264 * Handshake functions
6265 */
Hanno Becker21489932019-02-05 13:20:55 +00006266#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006267/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006268int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006269{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006270 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6271 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006274
Hanno Becker7177a882019-02-05 13:36:46 +00006275 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006276 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006277 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006278 ssl->state++;
6279 return( 0 );
6280 }
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6283 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006284}
6285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006286int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006287{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006288 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6289 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292
Hanno Becker7177a882019-02-05 13:36:46 +00006293 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006294 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006296 ssl->state++;
6297 return( 0 );
6298 }
6299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6301 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006302}
Gilles Peskinef9828522017-05-03 12:28:43 +02006303
Hanno Becker21489932019-02-05 13:20:55 +00006304#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006305/* Some certificate support -> implement write and parse */
6306
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006308{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006310 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006312 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6313 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006316
Hanno Becker7177a882019-02-05 13:36:46 +00006317 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006318 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006320 ssl->state++;
6321 return( 0 );
6322 }
6323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006324#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006325 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 {
6327 if( ssl->client_auth == 0 )
6328 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006330 ssl->state++;
6331 return( 0 );
6332 }
6333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006334#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006335 /*
6336 * If using SSLv3 and got no cert, send an Alert message
6337 * (otherwise an empty Certificate message will be sent).
6338 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6340 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 {
6342 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6344 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6345 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006346
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 goto write_msg;
6349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352#endif /* MBEDTLS_SSL_CLI_C */
6353#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006354 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006358 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6359 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006360 }
6361 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006362#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006364 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006365
6366 /*
6367 * 0 . 0 handshake type
6368 * 1 . 3 handshake length
6369 * 4 . 6 length of all certs
6370 * 7 . 9 length of cert. 1
6371 * 10 . n-1 peer certificate
6372 * n . n+2 length of cert. 2
6373 * n+3 . ... upper level cert, etc.
6374 */
6375 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006377
Paul Bakker29087132010-03-21 21:03:34 +00006378 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 {
6380 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006381 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006384 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006385 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006386 }
6387
6388 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6389 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6390 ssl->out_msg[i + 2] = (unsigned char)( n );
6391
6392 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6393 i += n; crt = crt->next;
6394 }
6395
6396 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6397 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6398 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6399
6400 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006401 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6402 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006403
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006404#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006405write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006406#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006407
6408 ssl->state++;
6409
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006410 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006412 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006413 return( ret );
6414 }
6415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006417
Paul Bakkered27a042013-04-18 22:46:23 +02006418 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419}
6420
Hanno Becker84879e32019-01-31 07:44:03 +00006421#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006422
6423#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006424static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6425 unsigned char *crt_buf,
6426 size_t crt_buf_len )
6427{
6428 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6429
6430 if( peer_crt == NULL )
6431 return( -1 );
6432
6433 if( peer_crt->raw.len != crt_buf_len )
6434 return( -1 );
6435
Hanno Becker46f34d02019-02-08 14:00:04 +00006436 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006437}
Hanno Becker177475a2019-02-05 17:02:46 +00006438#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6439static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6440 unsigned char *crt_buf,
6441 size_t crt_buf_len )
6442{
6443 int ret;
6444 unsigned char const * const peer_cert_digest =
6445 ssl->session->peer_cert_digest;
6446 mbedtls_md_type_t const peer_cert_digest_type =
6447 ssl->session->peer_cert_digest_type;
6448 mbedtls_md_info_t const * const digest_info =
6449 mbedtls_md_info_from_type( peer_cert_digest_type );
6450 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6451 size_t digest_len;
6452
6453 if( peer_cert_digest == NULL || digest_info == NULL )
6454 return( -1 );
6455
6456 digest_len = mbedtls_md_get_size( digest_info );
6457 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6458 return( -1 );
6459
6460 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6461 if( ret != 0 )
6462 return( -1 );
6463
6464 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6465}
6466#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006467#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006468
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006469/*
6470 * Once the certificate message is read, parse it into a cert chain and
6471 * perform basic checks, but leave actual verification to the caller
6472 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006473static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6474 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006475{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006476 int ret;
6477#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6478 int crt_cnt=0;
6479#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006480 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006481 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006482
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006484 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006486 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6487 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 }
6490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6492 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006493 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006495 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6496 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 }
6499
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006501
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006504 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006505 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006506
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006507 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006511 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6512 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006513 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006514 }
6515
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006516 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6517 i += 3;
6518
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006519 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006520 while( i < ssl->in_hslen )
6521 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006522 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006523 if ( i + 3 > ssl->in_hslen ) {
6524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006525 mbedtls_ssl_send_alert_message( ssl,
6526 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6527 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006528 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6529 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006530 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6531 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 if( ssl->in_msg[i] != 0 )
6533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006535 mbedtls_ssl_send_alert_message( ssl,
6536 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6537 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006538 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 }
6540
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006541 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006542 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6543 | (unsigned int) ssl->in_msg[i + 2];
6544 i += 3;
6545
6546 if( n < 128 || i + n > ssl->in_hslen )
6547 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006549 mbedtls_ssl_send_alert_message( ssl,
6550 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6551 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006552 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006553 }
6554
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006555 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006556#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6557 if( crt_cnt++ == 0 &&
6558 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6559 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006560 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006561 /* During client-side renegotiation, check that the server's
6562 * end-CRTs hasn't changed compared to the initial handshake,
6563 * mitigating the triple handshake attack. On success, reuse
6564 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006565 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6566 if( ssl_check_peer_crt_unchanged( ssl,
6567 &ssl->in_msg[i],
6568 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006569 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006570 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6571 mbedtls_ssl_send_alert_message( ssl,
6572 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6573 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6574 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006575 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006576
6577 /* Now we can safely free the original chain. */
6578 ssl_clear_peer_cert( ssl->session );
6579 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006580#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6581
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006582 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006583#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006584 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006585#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006586 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006587 * it in-place from the input buffer instead of making a copy. */
6588 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6589#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006590 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006591 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006592 case 0: /*ok*/
6593 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6594 /* Ignore certificate with an unknown algorithm: maybe a
6595 prior certificate was already trusted. */
6596 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006597
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006598 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6599 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6600 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006601
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006602 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6603 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6604 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006605
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006606 default:
6607 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6608 crt_parse_der_failed:
6609 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6610 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6611 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006612 }
6613
6614 i += n;
6615 }
6616
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006617 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006618 return( 0 );
6619}
6620
Hanno Becker4a55f632019-02-05 12:49:06 +00006621#if defined(MBEDTLS_SSL_SRV_C)
6622static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6623{
6624 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6625 return( -1 );
6626
6627#if defined(MBEDTLS_SSL_PROTO_SSL3)
6628 /*
6629 * Check if the client sent an empty certificate
6630 */
6631 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6632 {
6633 if( ssl->in_msglen == 2 &&
6634 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6635 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6636 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6637 {
6638 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6639 return( 0 );
6640 }
6641
6642 return( -1 );
6643 }
6644#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6645
6646#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6647 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6648 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6649 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6650 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6651 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6652 {
6653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6654 return( 0 );
6655 }
6656
6657 return( -1 );
6658#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6659 MBEDTLS_SSL_PROTO_TLS1_2 */
6660}
6661#endif /* MBEDTLS_SSL_SRV_C */
6662
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006663/* Check if a certificate message is expected.
6664 * Return either
6665 * - SSL_CERTIFICATE_EXPECTED, or
6666 * - SSL_CERTIFICATE_SKIP
6667 * indicating whether a Certificate message is expected or not.
6668 */
6669#define SSL_CERTIFICATE_EXPECTED 0
6670#define SSL_CERTIFICATE_SKIP 1
6671static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6672 int authmode )
6673{
6674 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006675 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006676
6677 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6678 return( SSL_CERTIFICATE_SKIP );
6679
6680#if defined(MBEDTLS_SSL_SRV_C)
6681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6682 {
6683 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6684 return( SSL_CERTIFICATE_SKIP );
6685
6686 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6687 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006688 ssl->session_negotiate->verify_result =
6689 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6690 return( SSL_CERTIFICATE_SKIP );
6691 }
6692 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006693#else
6694 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006695#endif /* MBEDTLS_SSL_SRV_C */
6696
6697 return( SSL_CERTIFICATE_EXPECTED );
6698}
6699
Hanno Becker68636192019-02-05 14:36:34 +00006700static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6701 int authmode,
6702 mbedtls_x509_crt *chain,
6703 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006704{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006705 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006706 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006707 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006708 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006709
Hanno Becker8927c832019-04-03 12:52:50 +01006710 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6711 void *p_vrfy;
6712
Hanno Becker68636192019-02-05 14:36:34 +00006713 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6714 return( 0 );
6715
Hanno Becker8927c832019-04-03 12:52:50 +01006716 if( ssl->f_vrfy != NULL )
6717 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006718 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006719 f_vrfy = ssl->f_vrfy;
6720 p_vrfy = ssl->p_vrfy;
6721 }
6722 else
6723 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006724 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006725 f_vrfy = ssl->conf->f_vrfy;
6726 p_vrfy = ssl->conf->p_vrfy;
6727 }
6728
Hanno Becker68636192019-02-05 14:36:34 +00006729 /*
6730 * Main check: verify certificate
6731 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006732#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6733 if( ssl->conf->f_ca_cb != NULL )
6734 {
6735 ((void) rs_ctx);
6736 have_ca_chain = 1;
6737
6738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006739 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006740 chain,
6741 ssl->conf->f_ca_cb,
6742 ssl->conf->p_ca_cb,
6743 ssl->conf->cert_profile,
6744 ssl->hostname,
6745 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006746 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006747 }
6748 else
6749#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6750 {
6751 mbedtls_x509_crt *ca_chain;
6752 mbedtls_x509_crl *ca_crl;
6753
6754#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6755 if( ssl->handshake->sni_ca_chain != NULL )
6756 {
6757 ca_chain = ssl->handshake->sni_ca_chain;
6758 ca_crl = ssl->handshake->sni_ca_crl;
6759 }
6760 else
6761#endif
6762 {
6763 ca_chain = ssl->conf->ca_chain;
6764 ca_crl = ssl->conf->ca_crl;
6765 }
6766
6767 if( ca_chain != NULL )
6768 have_ca_chain = 1;
6769
6770 ret = mbedtls_x509_crt_verify_restartable(
6771 chain,
6772 ca_chain, ca_crl,
6773 ssl->conf->cert_profile,
6774 ssl->hostname,
6775 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006776 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006777 }
Hanno Becker68636192019-02-05 14:36:34 +00006778
6779 if( ret != 0 )
6780 {
6781 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6782 }
6783
6784#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6785 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6786 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6787#endif
6788
6789 /*
6790 * Secondary checks: always done, but change 'ret' only if it was 0
6791 */
6792
6793#if defined(MBEDTLS_ECP_C)
6794 {
6795 const mbedtls_pk_context *pk = &chain->pk;
6796
6797 /* If certificate uses an EC key, make sure the curve is OK */
6798 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6799 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6800 {
6801 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6802
6803 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6804 if( ret == 0 )
6805 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6806 }
6807 }
6808#endif /* MBEDTLS_ECP_C */
6809
6810 if( mbedtls_ssl_check_cert_usage( chain,
6811 ciphersuite_info,
6812 ! ssl->conf->endpoint,
6813 &ssl->session_negotiate->verify_result ) != 0 )
6814 {
6815 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6816 if( ret == 0 )
6817 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6818 }
6819
6820 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6821 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6822 * with details encoded in the verification flags. All other kinds
6823 * of error codes, including those from the user provided f_vrfy
6824 * functions, are treated as fatal and lead to a failure of
6825 * ssl_parse_certificate even if verification was optional. */
6826 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6827 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6828 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6829 {
6830 ret = 0;
6831 }
6832
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006833 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006834 {
6835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6836 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6837 }
6838
6839 if( ret != 0 )
6840 {
6841 uint8_t alert;
6842
6843 /* The certificate may have been rejected for several reasons.
6844 Pick one and send the corresponding alert. Which alert to send
6845 may be a subject of debate in some cases. */
6846 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6847 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6849 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6850 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6851 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6852 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6853 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6854 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6855 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6856 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6857 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6858 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6859 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6860 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6861 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6862 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6863 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6864 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6865 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6866 else
6867 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6868 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6869 alert );
6870 }
6871
6872#if defined(MBEDTLS_DEBUG_C)
6873 if( ssl->session_negotiate->verify_result != 0 )
6874 {
6875 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6876 ssl->session_negotiate->verify_result ) );
6877 }
6878 else
6879 {
6880 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6881 }
6882#endif /* MBEDTLS_DEBUG_C */
6883
6884 return( ret );
6885}
6886
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006887#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6888static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6889 unsigned char *start, size_t len )
6890{
6891 int ret;
6892 /* Remember digest of the peer's end-CRT. */
6893 ssl->session_negotiate->peer_cert_digest =
6894 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6895 if( ssl->session_negotiate->peer_cert_digest == NULL )
6896 {
6897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6898 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6899 mbedtls_ssl_send_alert_message( ssl,
6900 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6901 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6902
6903 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6904 }
6905
6906 ret = mbedtls_md( mbedtls_md_info_from_type(
6907 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6908 start, len,
6909 ssl->session_negotiate->peer_cert_digest );
6910
6911 ssl->session_negotiate->peer_cert_digest_type =
6912 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6913 ssl->session_negotiate->peer_cert_digest_len =
6914 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6915
6916 return( ret );
6917}
6918
6919static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6920 unsigned char *start, size_t len )
6921{
6922 unsigned char *end = start + len;
6923 int ret;
6924
6925 /* Make a copy of the peer's raw public key. */
6926 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6927 ret = mbedtls_pk_parse_subpubkey( &start, end,
6928 &ssl->handshake->peer_pubkey );
6929 if( ret != 0 )
6930 {
6931 /* We should have parsed the public key before. */
6932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6933 }
6934
6935 return( 0 );
6936}
6937#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6938
Hanno Becker68636192019-02-05 14:36:34 +00006939int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6940{
6941 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006942 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006943#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6944 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6945 ? ssl->handshake->sni_authmode
6946 : ssl->conf->authmode;
6947#else
6948 const int authmode = ssl->conf->authmode;
6949#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006950 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006951 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006952
6953 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6954
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006955 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6956 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006957 {
6958 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006959 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006960 }
6961
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006962#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6963 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006964 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006965 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006966 chain = ssl->handshake->ecrs_peer_cert;
6967 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006968 goto crt_verify;
6969 }
6970#endif
6971
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006972 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006973 {
6974 /* mbedtls_ssl_read_record may have sent an alert already. We
6975 let it decide whether to alert. */
6976 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006977 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006978 }
6979
Hanno Becker4a55f632019-02-05 12:49:06 +00006980#if defined(MBEDTLS_SSL_SRV_C)
6981 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6982 {
6983 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006984
6985 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006986 ret = 0;
6987 else
6988 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006989
Hanno Becker6bdfab22019-02-05 13:11:17 +00006990 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006991 }
6992#endif /* MBEDTLS_SSL_SRV_C */
6993
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006994 /* Clear existing peer CRT structure in case we tried to
6995 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006996 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006997
6998 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6999 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007000 {
7001 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7002 sizeof( mbedtls_x509_crt ) ) );
7003 mbedtls_ssl_send_alert_message( ssl,
7004 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7005 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007006
Hanno Becker3dad3112019-02-05 17:19:52 +00007007 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7008 goto exit;
7009 }
7010 mbedtls_x509_crt_init( chain );
7011
7012 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007013 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007014 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007015
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007016#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7017 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007018 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007019
7020crt_verify:
7021 if( ssl->handshake->ecrs_enabled)
7022 rs_ctx = &ssl->handshake->ecrs_ctx;
7023#endif
7024
Hanno Becker68636192019-02-05 14:36:34 +00007025 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007026 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007027 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007028 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007029
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007030#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007031 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007032 unsigned char *crt_start, *pk_start;
7033 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007034
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007035 /* We parse the CRT chain without copying, so
7036 * these pointers point into the input buffer,
7037 * and are hence still valid after freeing the
7038 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007039
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007040 crt_start = chain->raw.p;
7041 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007042
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007043 pk_start = chain->pk_raw.p;
7044 pk_len = chain->pk_raw.len;
7045
7046 /* Free the CRT structures before computing
7047 * digest and copying the peer's public key. */
7048 mbedtls_x509_crt_free( chain );
7049 mbedtls_free( chain );
7050 chain = NULL;
7051
7052 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007053 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007054 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007055
7056 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7057 if( ret != 0 )
7058 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007059 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007060#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7061 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007062 ssl->session_negotiate->peer_cert = chain;
7063 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007064#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007066 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007067
Hanno Becker6bdfab22019-02-05 13:11:17 +00007068exit:
7069
Hanno Becker3dad3112019-02-05 17:19:52 +00007070 if( ret == 0 )
7071 ssl->state++;
7072
7073#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7074 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7075 {
7076 ssl->handshake->ecrs_peer_cert = chain;
7077 chain = NULL;
7078 }
7079#endif
7080
7081 if( chain != NULL )
7082 {
7083 mbedtls_x509_crt_free( chain );
7084 mbedtls_free( chain );
7085 }
7086
Paul Bakker5121ce52009-01-03 21:22:43 +00007087 return( ret );
7088}
Hanno Becker21489932019-02-05 13:20:55 +00007089#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007092{
7093 int ret;
7094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007096
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007097 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007098 ssl->out_msglen = 1;
7099 ssl->out_msg[0] = 1;
7100
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 ssl->state++;
7102
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007103 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007105 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106 return( ret );
7107 }
7108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007109 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007110
7111 return( 0 );
7112}
7113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007115{
7116 int ret;
7117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007119
Hanno Becker327c93b2018-08-15 13:56:18 +01007120 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 return( ret );
7124 }
7125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007127 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007129 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7130 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007132 }
7133
Hanno Beckere678eaa2018-08-21 14:57:46 +01007134 /* CCS records are only accepted if they have length 1 and content '1',
7135 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007136
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007137 /*
7138 * Switch to our negotiated transform and session parameters for inbound
7139 * data.
7140 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007142 ssl->transform_in = ssl->transform_negotiate;
7143 ssl->session_in = ssl->session_negotiate;
7144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007146 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007149 ssl_dtls_replay_reset( ssl );
7150#endif
7151
7152 /* Increment epoch */
7153 if( ++ssl->in_epoch == 0 )
7154 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007156 /* This is highly unlikely to happen for legitimate reasons, so
7157 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007159 }
7160 }
7161 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007162#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007163 memset( ssl->in_ctr, 0, 8 );
7164
Hanno Becker79594fd2019-05-08 09:38:41 +01007165 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7168 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007173 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7174 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007176 }
7177 }
7178#endif
7179
Paul Bakker5121ce52009-01-03 21:22:43 +00007180 ssl->state++;
7181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007183
7184 return( 0 );
7185}
7186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7188 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007189{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007190 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007192#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7193 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7194 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007195 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007196 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007197#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007198#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7199#if defined(MBEDTLS_SHA512_C)
7200 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007201 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7202 else
7203#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204#if defined(MBEDTLS_SHA256_C)
7205 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007206 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007207 else
7208#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007212 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007213 }
Paul Bakker380da532012-04-18 16:10:25 +00007214}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7219 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007220 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7221 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007222#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7224#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007225#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007226 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007227 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7228#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007229 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007230#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007231#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007232#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007233#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007234 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007235 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007237 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007238#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007239#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007241}
7242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007244 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007245{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007246#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7247 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007248 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7249 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007250#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007251#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7252#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007253#if defined(MBEDTLS_USE_PSA_CRYPTO)
7254 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7255#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007256 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007257#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007260#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007261 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007262#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007263 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007264#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007265#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007267}
7268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007269#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7270 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7271static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007272 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007273{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007274 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7275 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007276}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007277#endif
Paul Bakker380da532012-04-18 16:10:25 +00007278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7280#if defined(MBEDTLS_SHA256_C)
7281static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007282 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007283{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007284#if defined(MBEDTLS_USE_PSA_CRYPTO)
7285 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7286#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007287 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007288#endif
Paul Bakker380da532012-04-18 16:10:25 +00007289}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007290#endif
Paul Bakker380da532012-04-18 16:10:25 +00007291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007292#if defined(MBEDTLS_SHA512_C)
7293static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007294 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007295{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007296#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007297 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007298#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007299 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007300#endif
Paul Bakker380da532012-04-18 16:10:25 +00007301}
Paul Bakker769075d2012-11-24 11:26:46 +01007302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007306static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007307 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007308{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007309 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007310 mbedtls_md5_context md5;
7311 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007312
Paul Bakker5121ce52009-01-03 21:22:43 +00007313 unsigned char padbuf[48];
7314 unsigned char md5sum[16];
7315 unsigned char sha1sum[20];
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007318 if( !session )
7319 session = ssl->session;
7320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007321 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007322
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007323 mbedtls_md5_init( &md5 );
7324 mbedtls_sha1_init( &sha1 );
7325
7326 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7327 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007328
7329 /*
7330 * SSLv3:
7331 * hash =
7332 * MD5( master + pad2 +
7333 * MD5( handshake + sender + master + pad1 ) )
7334 * + SHA1( master + pad2 +
7335 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007336 */
7337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007338#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007339 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7340 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007341#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007343#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007344 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7345 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007346#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007348 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007349 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Paul Bakker1ef83d62012-04-11 12:09:53 +00007351 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007352
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007353 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7354 mbedtls_md5_update_ret( &md5, session->master, 48 );
7355 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7356 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007358 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7359 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7360 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7361 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Paul Bakker1ef83d62012-04-11 12:09:53 +00007363 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007364
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007365 mbedtls_md5_starts_ret( &md5 );
7366 mbedtls_md5_update_ret( &md5, session->master, 48 );
7367 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7368 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7369 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007370
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007371 mbedtls_sha1_starts_ret( &sha1 );
7372 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7373 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7374 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7375 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007376
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007377 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007378
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007379 mbedtls_md5_free( &md5 );
7380 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007382 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7383 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7384 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007387}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007392 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007393{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007395 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007396 mbedtls_md5_context md5;
7397 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007398 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007401 if( !session )
7402 session = ssl->session;
7403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007405
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007406 mbedtls_md5_init( &md5 );
7407 mbedtls_sha1_init( &sha1 );
7408
7409 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7410 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007411
Paul Bakker1ef83d62012-04-11 12:09:53 +00007412 /*
7413 * TLSv1:
7414 * hash = PRF( master, finished_label,
7415 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7416 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007418#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007419 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7420 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007421#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007423#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007424 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7425 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007426#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007428 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007429 ? "client finished"
7430 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007431
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007432 mbedtls_md5_finish_ret( &md5, padbuf );
7433 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007435 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007436 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007439
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007440 mbedtls_md5_free( &md5 );
7441 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007443 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007448
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7450#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007451static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007452 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007453{
7454 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007455 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007456 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007457#if defined(MBEDTLS_USE_PSA_CRYPTO)
7458 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007459 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007460 psa_status_t status;
7461#else
7462 mbedtls_sha256_context sha256;
7463#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007465 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007466 if( !session )
7467 session = ssl->session;
7468
Andrzej Kurekeb342242019-01-29 09:14:33 -05007469 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7470 ? "client finished"
7471 : "server finished";
7472
7473#if defined(MBEDTLS_USE_PSA_CRYPTO)
7474 sha256_psa = psa_hash_operation_init();
7475
7476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7477
7478 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7479 if( status != PSA_SUCCESS )
7480 {
7481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7482 return;
7483 }
7484
7485 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7486 if( status != PSA_SUCCESS )
7487 {
7488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7489 return;
7490 }
7491 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7492#else
7493
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007494 mbedtls_sha256_init( &sha256 );
7495
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007497
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007498 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007499
7500 /*
7501 * TLSv1.2:
7502 * hash = PRF( master, finished_label,
7503 * Hash( handshake ) )[0.11]
7504 */
7505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007506#if !defined(MBEDTLS_SHA256_ALT)
7507 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007508 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007509#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007510
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007511 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007512 mbedtls_sha256_free( &sha256 );
7513#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007514
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007515 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007516 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007519
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007520 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007525
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007527static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007528 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007529{
7530 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007531 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007532 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007533#if defined(MBEDTLS_USE_PSA_CRYPTO)
7534 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007535 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007536 psa_status_t status;
7537#else
7538 mbedtls_sha512_context sha512;
7539#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007540
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007541 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007542 if( !session )
7543 session = ssl->session;
7544
Andrzej Kurekeb342242019-01-29 09:14:33 -05007545 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7546 ? "client finished"
7547 : "server finished";
7548
7549#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007550 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551
7552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7553
Andrzej Kurek972fba52019-01-30 03:29:12 -05007554 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007555 if( status != PSA_SUCCESS )
7556 {
7557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7558 return;
7559 }
7560
Andrzej Kurek972fba52019-01-30 03:29:12 -05007561 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007562 if( status != PSA_SUCCESS )
7563 {
7564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7565 return;
7566 }
7567 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7568#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007569 mbedtls_sha512_init( &sha512 );
7570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007571 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007572
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007573 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007574
7575 /*
7576 * TLSv1.2:
7577 * hash = PRF( master, finished_label,
7578 * Hash( handshake ) )[0.11]
7579 */
7580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007581#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007582 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7583 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007584#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007585
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007586 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007587 mbedtls_sha512_free( &sha512 );
7588#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007589
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007590 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007591 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007594
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007595 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007598}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599#endif /* MBEDTLS_SHA512_C */
7600#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007603{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007605
7606 /*
7607 * Free our handshake params
7608 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007609 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007610 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007611 ssl->handshake = NULL;
7612
7613 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007614 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007615 */
7616 if( ssl->transform )
7617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 mbedtls_ssl_transform_free( ssl->transform );
7619 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007620 }
7621 ssl->transform = ssl->transform_negotiate;
7622 ssl->transform_negotiate = NULL;
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007625}
7626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007628{
7629 int resume = ssl->handshake->resume;
7630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633#if defined(MBEDTLS_SSL_RENEGOTIATION)
7634 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007635 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007636 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637 ssl->renego_records_seen = 0;
7638 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007639#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007640
7641 /*
7642 * Free the previous session and switch in the current one
7643 */
Paul Bakker0a597072012-09-25 21:55:46 +00007644 if( ssl->session )
7645 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007646#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007647 /* RFC 7366 3.1: keep the EtM state */
7648 ssl->session_negotiate->encrypt_then_mac =
7649 ssl->session->encrypt_then_mac;
7650#endif
7651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007652 mbedtls_ssl_session_free( ssl->session );
7653 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007654 }
7655 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007656 ssl->session_negotiate = NULL;
7657
Paul Bakker0a597072012-09-25 21:55:46 +00007658 /*
7659 * Add cache entry
7660 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007661 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007662 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007663 resume == 0 )
7664 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007665 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007667 }
Paul Bakker0a597072012-09-25 21:55:46 +00007668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007669#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007670 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007671 ssl->handshake->flight != NULL )
7672 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007673 /* Cancel handshake timer */
7674 ssl_set_timer( ssl, 0 );
7675
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676 /* Keep last flight around in case we need to resend it:
7677 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007678 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007679 }
7680 else
7681#endif
7682 ssl_handshake_wrapup_free_hs_transform( ssl );
7683
Paul Bakker48916f92012-09-16 19:57:18 +00007684 ssl->state++;
7685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007687}
7688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007691 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007694
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007695 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007696
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007697 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007698
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007699 /*
7700 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7701 * may define some other value. Currently (early 2016), no defined
7702 * ciphersuite does this (and this is unlikely to change as activity has
7703 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7704 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007707#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007708 ssl->verify_data_len = hash_len;
7709 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007710#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007711
Paul Bakker5121ce52009-01-03 21:22:43 +00007712 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007713 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7714 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007715
7716 /*
7717 * In case of session resuming, invert the client and server
7718 * ChangeCipherSpec messages order.
7719 */
Paul Bakker0a597072012-09-25 21:55:46 +00007720 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007721 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007723 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007725#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007727 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007729#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007730 }
7731 else
7732 ssl->state++;
7733
Paul Bakker48916f92012-09-16 19:57:18 +00007734 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007735 * Switch to our negotiated transform and session parameters for outbound
7736 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007737 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007742 {
7743 unsigned char i;
7744
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007745 /* Remember current epoch settings for resending */
7746 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007747 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007748
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007749 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007750 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007751
7752 /* Increment epoch */
7753 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007754 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007755 break;
7756
7757 /* The loop goes to its end iff the counter is wrapping */
7758 if( i == 0 )
7759 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007760 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7761 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007762 }
7763 }
7764 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007765#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007766 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007767
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007768 ssl->transform_out = ssl->transform_negotiate;
7769 ssl->session_out = ssl->session_negotiate;
7770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7772 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7777 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007778 }
7779 }
7780#endif
7781
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007783 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007784 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007785#endif
7786
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007787 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007789 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007790 return( ret );
7791 }
7792
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007793#if defined(MBEDTLS_SSL_PROTO_DTLS)
7794 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7795 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7796 {
7797 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7798 return( ret );
7799 }
7800#endif
7801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007803
7804 return( 0 );
7805}
7806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007807#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007808#define SSL_MAX_HASH_LEN 36
7809#else
7810#define SSL_MAX_HASH_LEN 12
7811#endif
7812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007813int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007814{
Paul Bakker23986e52011-04-24 08:57:21 +00007815 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007816 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007817 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007821 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007822
Hanno Becker327c93b2018-08-15 13:56:18 +01007823 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007826 return( ret );
7827 }
7828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007832 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7833 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007835 }
7836
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007837 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838#if defined(MBEDTLS_SSL_PROTO_SSL3)
7839 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007840 hash_len = 36;
7841 else
7842#endif
7843 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007844
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7846 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007849 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7850 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 }
7853
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007855 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007858 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7859 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007861 }
7862
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007864 ssl->verify_data_len = hash_len;
7865 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007866#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007867
Paul Bakker0a597072012-09-25 21:55:46 +00007868 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007869 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007873#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007875 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007876 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007877#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007878 }
7879 else
7880 ssl->state++;
7881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007885#endif
7886
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007888
7889 return( 0 );
7890}
7891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007893{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7897 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7898 mbedtls_md5_init( &handshake->fin_md5 );
7899 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007900 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7901 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007902#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7904#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007905#if defined(MBEDTLS_USE_PSA_CRYPTO)
7906 handshake->fin_sha256_psa = psa_hash_operation_init();
7907 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7908#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007910 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007911#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007912#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007913#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007914#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007915 handshake->fin_sha384_psa = psa_hash_operation_init();
7916 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007917#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007919 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007920#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007921#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007922#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007923
7924 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007925
7926#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7927 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7928 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7929#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007930
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#if defined(MBEDTLS_DHM_C)
7932 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007933#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007934#if defined(MBEDTLS_ECDH_C)
7935 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007936#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007937#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007938 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007939#if defined(MBEDTLS_SSL_CLI_C)
7940 handshake->ecjpake_cache = NULL;
7941 handshake->ecjpake_cache_len = 0;
7942#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007943#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007944
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007945#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007946 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007947#endif
7948
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007949#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7950 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7951#endif
Hanno Becker75173122019-02-06 16:18:31 +00007952
7953#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7954 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7955 mbedtls_pk_init( &handshake->peer_pubkey );
7956#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957}
7958
Hanno Beckera18d1322018-01-03 14:27:32 +00007959void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007960{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7964 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007965
Hanno Beckerd56ed242018-01-03 15:32:51 +00007966#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 mbedtls_md_init( &transform->md_ctx_enc );
7968 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007969#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007970}
7971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007973{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975}
7976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007978{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007979 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007980 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007983 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007984 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007985 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007986
7987 /*
7988 * Either the pointers are now NULL or cleared properly and can be freed.
7989 * Now allocate missing structures.
7990 */
7991 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007992 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007993 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007994 }
Paul Bakker48916f92012-09-16 19:57:18 +00007995
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007996 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007997 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007998 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007999 }
Paul Bakker48916f92012-09-16 19:57:18 +00008000
Paul Bakker82788fb2014-10-20 13:59:19 +02008001 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008002 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008003 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008004 }
Paul Bakker48916f92012-09-16 19:57:18 +00008005
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008006 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008007 if( ssl->handshake == NULL ||
8008 ssl->transform_negotiate == NULL ||
8009 ssl->session_negotiate == NULL )
8010 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008011 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008013 mbedtls_free( ssl->handshake );
8014 mbedtls_free( ssl->transform_negotiate );
8015 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008016
8017 ssl->handshake = NULL;
8018 ssl->transform_negotiate = NULL;
8019 ssl->session_negotiate = NULL;
8020
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008021 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008022 }
8023
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008024 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008026 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008027 ssl_handshake_params_init( ssl->handshake );
8028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008030 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8031 {
8032 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008033
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008034 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8035 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8036 else
8037 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008038
8039 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008040 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008041#endif
8042
Paul Bakker48916f92012-09-16 19:57:18 +00008043 return( 0 );
8044}
8045
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008046#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008047/* Dummy cookie callbacks for defaults */
8048static int ssl_cookie_write_dummy( void *ctx,
8049 unsigned char **p, unsigned char *end,
8050 const unsigned char *cli_id, size_t cli_id_len )
8051{
8052 ((void) ctx);
8053 ((void) p);
8054 ((void) end);
8055 ((void) cli_id);
8056 ((void) cli_id_len);
8057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008058 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008059}
8060
8061static int ssl_cookie_check_dummy( void *ctx,
8062 const unsigned char *cookie, size_t cookie_len,
8063 const unsigned char *cli_id, size_t cli_id_len )
8064{
8065 ((void) ctx);
8066 ((void) cookie);
8067 ((void) cookie_len);
8068 ((void) cli_id);
8069 ((void) cli_id_len);
8070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008071 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008072}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008073#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008074
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008075/* Once ssl->out_hdr as the address of the beginning of the
8076 * next outgoing record is set, deduce the other pointers.
8077 *
8078 * Note: For TLS, we save the implicit record sequence number
8079 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8080 * and the caller has to make sure there's space for this.
8081 */
8082
8083static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8084 mbedtls_ssl_transform *transform )
8085{
8086#if defined(MBEDTLS_SSL_PROTO_DTLS)
8087 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8088 {
8089 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008090#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008091 ssl->out_cid = ssl->out_ctr + 8;
8092 ssl->out_len = ssl->out_cid;
8093 if( transform != NULL )
8094 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008095#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008096 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008097#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008098 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008099 }
8100 else
8101#endif
8102 {
8103 ssl->out_ctr = ssl->out_hdr - 8;
8104 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008105#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008106 ssl->out_cid = ssl->out_len;
8107#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008108 ssl->out_iv = ssl->out_hdr + 5;
8109 }
8110
8111 /* Adjust out_msg to make space for explicit IV, if used. */
8112 if( transform != NULL &&
8113 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8114 {
8115 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8116 }
8117 else
8118 ssl->out_msg = ssl->out_iv;
8119}
8120
8121/* Once ssl->in_hdr as the address of the beginning of the
8122 * next incoming record is set, deduce the other pointers.
8123 *
8124 * Note: For TLS, we save the implicit record sequence number
8125 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8126 * and the caller has to make sure there's space for this.
8127 */
8128
Hanno Becker79594fd2019-05-08 09:38:41 +01008129static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008130{
Hanno Becker79594fd2019-05-08 09:38:41 +01008131 /* This function sets the pointers to match the case
8132 * of unprotected TLS/DTLS records, with both ssl->in_iv
8133 * and ssl->in_msg pointing to the beginning of the record
8134 * content.
8135 *
8136 * When decrypting a protected record, ssl->in_msg
8137 * will be shifted to point to the beginning of the
8138 * record plaintext.
8139 */
8140
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008141#if defined(MBEDTLS_SSL_PROTO_DTLS)
8142 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8143 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008144 /* This sets the header pointers to match records
8145 * without CID. When we receive a record containing
8146 * a CID, the fields are shifted accordingly in
8147 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008148 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008149#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008150 ssl->in_cid = ssl->in_ctr + 8;
8151 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008152#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008153 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008154#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008155 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008156 }
8157 else
8158#endif
8159 {
8160 ssl->in_ctr = ssl->in_hdr - 8;
8161 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008162#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008163 ssl->in_cid = ssl->in_len;
8164#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008165 ssl->in_iv = ssl->in_hdr + 5;
8166 }
8167
Hanno Becker79594fd2019-05-08 09:38:41 +01008168 /* This will be adjusted at record decryption time. */
8169 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008170}
8171
Paul Bakker5121ce52009-01-03 21:22:43 +00008172/*
8173 * Initialize an SSL context
8174 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008175void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8176{
8177 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8178}
8179
8180/*
8181 * Setup an SSL context
8182 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008183
8184static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8185{
8186 /* Set the incoming and outgoing record pointers. */
8187#if defined(MBEDTLS_SSL_PROTO_DTLS)
8188 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8189 {
8190 ssl->out_hdr = ssl->out_buf;
8191 ssl->in_hdr = ssl->in_buf;
8192 }
8193 else
8194#endif /* MBEDTLS_SSL_PROTO_DTLS */
8195 {
8196 ssl->out_hdr = ssl->out_buf + 8;
8197 ssl->in_hdr = ssl->in_buf + 8;
8198 }
8199
8200 /* Derive other internal pointers. */
8201 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008202 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008203}
8204
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008205int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008206 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008207{
Paul Bakker48916f92012-09-16 19:57:18 +00008208 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008209
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008210 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008211
8212 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008213 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008214 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008215
8216 /* Set to NULL in case of an error condition */
8217 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008218
Angus Grattond8213d02016-05-25 20:56:48 +10008219 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8220 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008221 {
Angus Grattond8213d02016-05-25 20:56:48 +10008222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008223 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008224 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008225 }
8226
8227 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8228 if( ssl->out_buf == NULL )
8229 {
8230 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008231 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008232 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008233 }
8234
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008235 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008236
Paul Bakker48916f92012-09-16 19:57:18 +00008237 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008238 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008239
8240 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008241
8242error:
8243 mbedtls_free( ssl->in_buf );
8244 mbedtls_free( ssl->out_buf );
8245
8246 ssl->conf = NULL;
8247
8248 ssl->in_buf = NULL;
8249 ssl->out_buf = NULL;
8250
8251 ssl->in_hdr = NULL;
8252 ssl->in_ctr = NULL;
8253 ssl->in_len = NULL;
8254 ssl->in_iv = NULL;
8255 ssl->in_msg = NULL;
8256
8257 ssl->out_hdr = NULL;
8258 ssl->out_ctr = NULL;
8259 ssl->out_len = NULL;
8260 ssl->out_iv = NULL;
8261 ssl->out_msg = NULL;
8262
k-stachowiak9f7798e2018-07-31 16:52:32 +02008263 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008264}
8265
8266/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008267 * Reset an initialized and used SSL context for re-use while retaining
8268 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008269 *
8270 * If partial is non-zero, keep data in the input buffer and client ID.
8271 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008272 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008273static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008274{
Paul Bakker48916f92012-09-16 19:57:18 +00008275 int ret;
8276
Hanno Becker7e772132018-08-10 12:38:21 +01008277#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8278 !defined(MBEDTLS_SSL_SRV_C)
8279 ((void) partial);
8280#endif
8281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008282 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008283
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008284 /* Cancel any possibly running timer */
8285 ssl_set_timer( ssl, 0 );
8286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008287#if defined(MBEDTLS_SSL_RENEGOTIATION)
8288 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008289 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008290
8291 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8293 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008294#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008296
Paul Bakker7eb013f2011-10-06 12:37:39 +00008297 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008298 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008299
8300 ssl->in_msgtype = 0;
8301 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008303 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008304 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008306#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008307 ssl_dtls_replay_reset( ssl );
8308#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008309
8310 ssl->in_hslen = 0;
8311 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008312
8313 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008314
8315 ssl->out_msgtype = 0;
8316 ssl->out_msglen = 0;
8317 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008318#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8319 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008320 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008321#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008322
Hanno Becker19859472018-08-06 09:40:20 +01008323 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8324
Paul Bakker48916f92012-09-16 19:57:18 +00008325 ssl->transform_in = NULL;
8326 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008327
Hanno Becker78640902018-08-13 16:35:15 +01008328 ssl->session_in = NULL;
8329 ssl->session_out = NULL;
8330
Angus Grattond8213d02016-05-25 20:56:48 +10008331 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008332
8333#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008334 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008335#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8336 {
8337 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008338 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008339 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8342 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8345 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008346 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008347 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8348 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008349 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008350 }
8351#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008352
Paul Bakker48916f92012-09-16 19:57:18 +00008353 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 mbedtls_ssl_transform_free( ssl->transform );
8356 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008357 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008358 }
Paul Bakker48916f92012-09-16 19:57:18 +00008359
Paul Bakkerc0463502013-02-14 11:19:38 +01008360 if( ssl->session )
8361 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008362 mbedtls_ssl_session_free( ssl->session );
8363 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008364 ssl->session = NULL;
8365 }
8366
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008367#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008368 ssl->alpn_chosen = NULL;
8369#endif
8370
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008371#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008372#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008373 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008374#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008375 {
8376 mbedtls_free( ssl->cli_id );
8377 ssl->cli_id = NULL;
8378 ssl->cli_id_len = 0;
8379 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008380#endif
8381
Paul Bakker48916f92012-09-16 19:57:18 +00008382 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8383 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008384
8385 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008386}
8387
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008388/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008389 * Reset an initialized and used SSL context for re-use while retaining
8390 * all application-set variables, function pointers and data.
8391 */
8392int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8393{
8394 return( ssl_session_reset_int( ssl, 0 ) );
8395}
8396
8397/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008398 * SSL set accessors
8399 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008400void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008401{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008402 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008403}
8404
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008405void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008406{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008407 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008408}
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008411void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008412{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008413 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008414}
8415#endif
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008418void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008419{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008420 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008421}
8422#endif
8423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008425
Hanno Becker1841b0a2018-08-24 11:13:57 +01008426void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8427 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008428{
8429 ssl->disable_datagram_packing = !allow_packing;
8430}
8431
8432void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8433 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008435 conf->hs_timeout_min = min;
8436 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008437}
8438#endif
8439
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008440void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008441{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008442 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008443}
8444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008445#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008446void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008447 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008448 void *p_vrfy )
8449{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008450 conf->f_vrfy = f_vrfy;
8451 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008453#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008454
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008455void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008456 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008457 void *p_rng )
8458{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008459 conf->f_rng = f_rng;
8460 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008461}
8462
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008463void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008464 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008465 void *p_dbg )
8466{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008467 conf->f_dbg = f_dbg;
8468 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008469}
8470
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008471void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008472 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008473 mbedtls_ssl_send_t *f_send,
8474 mbedtls_ssl_recv_t *f_recv,
8475 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008476{
8477 ssl->p_bio = p_bio;
8478 ssl->f_send = f_send;
8479 ssl->f_recv = f_recv;
8480 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008481}
8482
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008483#if defined(MBEDTLS_SSL_PROTO_DTLS)
8484void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8485{
8486 ssl->mtu = mtu;
8487}
8488#endif
8489
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008490void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008491{
8492 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008493}
8494
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008495void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8496 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008497 mbedtls_ssl_set_timer_t *f_set_timer,
8498 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008499{
8500 ssl->p_timer = p_timer;
8501 ssl->f_set_timer = f_set_timer;
8502 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008503
8504 /* Make sure we start with no timer running */
8505 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008506}
8507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008508#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008509void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008510 void *p_cache,
8511 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8512 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008513{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008514 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008515 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008516 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008520#if defined(MBEDTLS_SSL_CLI_C)
8521int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008522{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008523 int ret;
8524
8525 if( ssl == NULL ||
8526 session == NULL ||
8527 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008528 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008531 }
8532
Hanno Becker52055ae2019-02-06 14:30:46 +00008533 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8534 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008535 return( ret );
8536
Paul Bakker0a597072012-09-25 21:55:46 +00008537 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008538
8539 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008540}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008541#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008542
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008543void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008544 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008546 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8547 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8548 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8549 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008550}
8551
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008552void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008553 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008554 int major, int minor )
8555{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008557 return;
8558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008559 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008560 return;
8561
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008562 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008563}
8564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008565#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008566void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008567 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008568{
8569 conf->cert_profile = profile;
8570}
8571
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008572/* Append a new keycert entry to a (possibly empty) list */
8573static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8574 mbedtls_x509_crt *cert,
8575 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008576{
niisato8ee24222018-06-25 19:05:48 +09008577 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008578
niisato8ee24222018-06-25 19:05:48 +09008579 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8580 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008581 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008582
niisato8ee24222018-06-25 19:05:48 +09008583 new_cert->cert = cert;
8584 new_cert->key = key;
8585 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008586
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008587 /* Update head is the list was null, else add to the end */
8588 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008589 {
niisato8ee24222018-06-25 19:05:48 +09008590 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008591 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008592 else
8593 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008594 mbedtls_ssl_key_cert *cur = *head;
8595 while( cur->next != NULL )
8596 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008597 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008598 }
8599
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008600 return( 0 );
8601}
8602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008603int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008604 mbedtls_x509_crt *own_cert,
8605 mbedtls_pk_context *pk_key )
8606{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008607 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008608}
8609
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008610void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008611 mbedtls_x509_crt *ca_chain,
8612 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008613{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008614 conf->ca_chain = ca_chain;
8615 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008616
8617#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8618 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8619 * cannot be used together. */
8620 conf->f_ca_cb = NULL;
8621 conf->p_ca_cb = NULL;
8622#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008623}
Hanno Becker5adaad92019-03-27 16:54:37 +00008624
8625#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8626void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008627 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008628 void *p_ca_cb )
8629{
8630 conf->f_ca_cb = f_ca_cb;
8631 conf->p_ca_cb = p_ca_cb;
8632
8633 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8634 * cannot be used together. */
8635 conf->ca_chain = NULL;
8636 conf->ca_crl = NULL;
8637}
8638#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008639#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008640
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008641#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8642int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8643 mbedtls_x509_crt *own_cert,
8644 mbedtls_pk_context *pk_key )
8645{
8646 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8647 own_cert, pk_key ) );
8648}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008649
8650void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8651 mbedtls_x509_crt *ca_chain,
8652 mbedtls_x509_crl *ca_crl )
8653{
8654 ssl->handshake->sni_ca_chain = ca_chain;
8655 ssl->handshake->sni_ca_crl = ca_crl;
8656}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008657
8658void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8659 int authmode )
8660{
8661 ssl->handshake->sni_authmode = authmode;
8662}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008663#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8664
Hanno Becker8927c832019-04-03 12:52:50 +01008665#if defined(MBEDTLS_X509_CRT_PARSE_C)
8666void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8667 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8668 void *p_vrfy )
8669{
8670 ssl->f_vrfy = f_vrfy;
8671 ssl->p_vrfy = p_vrfy;
8672}
8673#endif
8674
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008675#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008676/*
8677 * Set EC J-PAKE password for current handshake
8678 */
8679int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8680 const unsigned char *pw,
8681 size_t pw_len )
8682{
8683 mbedtls_ecjpake_role role;
8684
Janos Follath8eb64132016-06-03 15:40:57 +01008685 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008686 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8687
8688 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8689 role = MBEDTLS_ECJPAKE_SERVER;
8690 else
8691 role = MBEDTLS_ECJPAKE_CLIENT;
8692
8693 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8694 role,
8695 MBEDTLS_MD_SHA256,
8696 MBEDTLS_ECP_DP_SECP256R1,
8697 pw, pw_len ) );
8698}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008699#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008701#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008702
8703static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8704{
8705 /* Remove reference to existing PSK, if any. */
8706#if defined(MBEDTLS_USE_PSA_CRYPTO)
8707 if( conf->psk_opaque != 0 )
8708 {
8709 /* The maintenance of the PSK key slot is the
8710 * user's responsibility. */
8711 conf->psk_opaque = 0;
8712 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008713 /* This and the following branch should never
8714 * be taken simultaenously as we maintain the
8715 * invariant that raw and opaque PSKs are never
8716 * configured simultaneously. As a safeguard,
8717 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008718#endif /* MBEDTLS_USE_PSA_CRYPTO */
8719 if( conf->psk != NULL )
8720 {
8721 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8722
8723 mbedtls_free( conf->psk );
8724 conf->psk = NULL;
8725 conf->psk_len = 0;
8726 }
8727
8728 /* Remove reference to PSK identity, if any. */
8729 if( conf->psk_identity != NULL )
8730 {
8731 mbedtls_free( conf->psk_identity );
8732 conf->psk_identity = NULL;
8733 conf->psk_identity_len = 0;
8734 }
8735}
8736
Hanno Becker7390c712018-11-15 13:33:04 +00008737/* This function assumes that PSK identity in the SSL config is unset.
8738 * It checks that the provided identity is well-formed and attempts
8739 * to make a copy of it in the SSL config.
8740 * On failure, the PSK identity in the config remains unset. */
8741static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8742 unsigned char const *psk_identity,
8743 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008744{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008745 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008746 if( psk_identity == NULL ||
8747 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008748 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008749 {
8750 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8751 }
8752
Hanno Becker7390c712018-11-15 13:33:04 +00008753 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8754 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008755 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008756
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008757 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008758 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008759
8760 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008761}
8762
Hanno Becker7390c712018-11-15 13:33:04 +00008763int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8764 const unsigned char *psk, size_t psk_len,
8765 const unsigned char *psk_identity, size_t psk_identity_len )
8766{
8767 int ret;
8768 /* Remove opaque/raw PSK + PSK Identity */
8769 ssl_conf_remove_psk( conf );
8770
8771 /* Check and set raw PSK */
8772 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8773 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8774 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8775 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8776 conf->psk_len = psk_len;
8777 memcpy( conf->psk, psk, conf->psk_len );
8778
8779 /* Check and set PSK Identity */
8780 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8781 if( ret != 0 )
8782 ssl_conf_remove_psk( conf );
8783
8784 return( ret );
8785}
8786
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008787static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8788{
8789#if defined(MBEDTLS_USE_PSA_CRYPTO)
8790 if( ssl->handshake->psk_opaque != 0 )
8791 {
8792 ssl->handshake->psk_opaque = 0;
8793 }
8794 else
8795#endif /* MBEDTLS_USE_PSA_CRYPTO */
8796 if( ssl->handshake->psk != NULL )
8797 {
8798 mbedtls_platform_zeroize( ssl->handshake->psk,
8799 ssl->handshake->psk_len );
8800 mbedtls_free( ssl->handshake->psk );
8801 ssl->handshake->psk_len = 0;
8802 }
8803}
8804
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008805int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8806 const unsigned char *psk, size_t psk_len )
8807{
8808 if( psk == NULL || ssl->handshake == NULL )
8809 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8810
8811 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8812 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8813
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008814 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008815
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008816 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008817 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008818
8819 ssl->handshake->psk_len = psk_len;
8820 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8821
8822 return( 0 );
8823}
8824
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008825#if defined(MBEDTLS_USE_PSA_CRYPTO)
8826int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008827 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008828 const unsigned char *psk_identity,
8829 size_t psk_identity_len )
8830{
Hanno Becker7390c712018-11-15 13:33:04 +00008831 int ret;
8832 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008833 ssl_conf_remove_psk( conf );
8834
Hanno Becker7390c712018-11-15 13:33:04 +00008835 /* Check and set opaque PSK */
8836 if( psk_slot == 0 )
8837 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008838 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008839
8840 /* Check and set PSK Identity */
8841 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8842 psk_identity_len );
8843 if( ret != 0 )
8844 ssl_conf_remove_psk( conf );
8845
8846 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008847}
8848
8849int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008850 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008851{
8852 if( psk_slot == 0 || ssl->handshake == NULL )
8853 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8854
8855 ssl_remove_psk( ssl );
8856 ssl->handshake->psk_opaque = psk_slot;
8857 return( 0 );
8858}
8859#endif /* MBEDTLS_USE_PSA_CRYPTO */
8860
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008861void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008863 size_t),
8864 void *p_psk )
8865{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008866 conf->f_psk = f_psk;
8867 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008868}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008869#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008870
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008871#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008872
8873#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008874int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008875{
8876 int ret;
8877
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008878 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8879 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8880 {
8881 mbedtls_mpi_free( &conf->dhm_P );
8882 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008883 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008884 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008885
8886 return( 0 );
8887}
Hanno Becker470a8c42017-10-04 15:28:46 +01008888#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008889
Hanno Beckera90658f2017-10-04 15:29:08 +01008890int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8891 const unsigned char *dhm_P, size_t P_len,
8892 const unsigned char *dhm_G, size_t G_len )
8893{
8894 int ret;
8895
8896 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8897 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8898 {
8899 mbedtls_mpi_free( &conf->dhm_P );
8900 mbedtls_mpi_free( &conf->dhm_G );
8901 return( ret );
8902 }
8903
8904 return( 0 );
8905}
Paul Bakker5121ce52009-01-03 21:22:43 +00008906
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008907int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008908{
8909 int ret;
8910
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008911 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8912 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8913 {
8914 mbedtls_mpi_free( &conf->dhm_P );
8915 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008916 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008917 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008918
8919 return( 0 );
8920}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008921#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008922
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008923#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8924/*
8925 * Set the minimum length for Diffie-Hellman parameters
8926 */
8927void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8928 unsigned int bitlen )
8929{
8930 conf->dhm_min_bitlen = bitlen;
8931}
8932#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8933
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008934#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008935/*
8936 * Set allowed/preferred hashes for handshake signatures
8937 */
8938void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8939 const int *hashes )
8940{
8941 conf->sig_hashes = hashes;
8942}
Hanno Becker947194e2017-04-07 13:25:49 +01008943#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008944
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008945#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008946/*
8947 * Set the allowed elliptic curves
8948 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008949void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008950 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008951{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008952 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008953}
Hanno Becker947194e2017-04-07 13:25:49 +01008954#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008955
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008956#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008957int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008958{
Hanno Becker947194e2017-04-07 13:25:49 +01008959 /* Initialize to suppress unnecessary compiler warning */
8960 size_t hostname_len = 0;
8961
8962 /* Check if new hostname is valid before
8963 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008964 if( hostname != NULL )
8965 {
8966 hostname_len = strlen( hostname );
8967
8968 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8969 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8970 }
8971
8972 /* Now it's clear that we will overwrite the old hostname,
8973 * so we can free it safely */
8974
8975 if( ssl->hostname != NULL )
8976 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008977 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008978 mbedtls_free( ssl->hostname );
8979 }
8980
8981 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008982
Paul Bakker5121ce52009-01-03 21:22:43 +00008983 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008984 {
8985 ssl->hostname = NULL;
8986 }
8987 else
8988 {
8989 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008990 if( ssl->hostname == NULL )
8991 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008992
Hanno Becker947194e2017-04-07 13:25:49 +01008993 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008994
Hanno Becker947194e2017-04-07 13:25:49 +01008995 ssl->hostname[hostname_len] = '\0';
8996 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008997
8998 return( 0 );
8999}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01009000#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00009001
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009002#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009003void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009005 const unsigned char *, size_t),
9006 void *p_sni )
9007{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009008 conf->f_sni = f_sni;
9009 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009010}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009012
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009013#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009014int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009015{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009016 size_t cur_len, tot_len;
9017 const char **p;
9018
9019 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009020 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9021 * MUST NOT be truncated."
9022 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009023 */
9024 tot_len = 0;
9025 for( p = protos; *p != NULL; p++ )
9026 {
9027 cur_len = strlen( *p );
9028 tot_len += cur_len;
9029
9030 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009031 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009032 }
9033
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009034 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009035
9036 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009037}
9038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009040{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009041 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009042}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009043#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009044
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009045void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009046{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009047 conf->max_major_ver = major;
9048 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009049}
9050
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009051void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009052{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009053 conf->min_major_ver = major;
9054 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009055}
9056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009057#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009058void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009059{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009060 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009061}
9062#endif
9063
Janos Follath088ce432017-04-10 12:42:31 +01009064#if defined(MBEDTLS_SSL_SRV_C)
9065void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9066 char cert_req_ca_list )
9067{
9068 conf->cert_req_ca_list = cert_req_ca_list;
9069}
9070#endif
9071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009073void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009075 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009076}
9077#endif
9078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009080void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009081{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009082 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009083}
9084#endif
9085
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009086#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009087void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009088{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009089 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009090}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009091#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009094int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009095{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009097 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009100 }
9101
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009102 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009103
9104 return( 0 );
9105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009109void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009110{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009111 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009112}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009115#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009116void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009117{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009118 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009119}
9120#endif
9121
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009122void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009123{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009124 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009125}
9126
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009127#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009128void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009129{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009130 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009131}
9132
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009133void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009134{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009135 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009136}
9137
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009138void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009139 const unsigned char period[8] )
9140{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009141 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009145#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009146#if defined(MBEDTLS_SSL_CLI_C)
9147void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009148{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009149 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009150}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009151#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009152
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009153#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009154void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9155 mbedtls_ssl_ticket_write_t *f_ticket_write,
9156 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9157 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009158{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009159 conf->f_ticket_write = f_ticket_write;
9160 conf->f_ticket_parse = f_ticket_parse;
9161 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009162}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009163#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009164#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009165
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009166#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9167void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9168 mbedtls_ssl_export_keys_t *f_export_keys,
9169 void *p_export_keys )
9170{
9171 conf->f_export_keys = f_export_keys;
9172 conf->p_export_keys = p_export_keys;
9173}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009174
9175void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9176 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9177 void *p_export_keys )
9178{
9179 conf->f_export_keys_ext = f_export_keys_ext;
9180 conf->p_export_keys = p_export_keys;
9181}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009182#endif
9183
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009184#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009185void mbedtls_ssl_conf_async_private_cb(
9186 mbedtls_ssl_config *conf,
9187 mbedtls_ssl_async_sign_t *f_async_sign,
9188 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9189 mbedtls_ssl_async_resume_t *f_async_resume,
9190 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009191 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009192{
9193 conf->f_async_sign_start = f_async_sign;
9194 conf->f_async_decrypt_start = f_async_decrypt;
9195 conf->f_async_resume = f_async_resume;
9196 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009197 conf->p_async_config_data = async_config_data;
9198}
9199
Gilles Peskine8f97af72018-04-26 11:46:10 +02009200void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9201{
9202 return( conf->p_async_config_data );
9203}
9204
Gilles Peskine1febfef2018-04-30 11:54:39 +02009205void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009206{
9207 if( ssl->handshake == NULL )
9208 return( NULL );
9209 else
9210 return( ssl->handshake->user_async_ctx );
9211}
9212
Gilles Peskine1febfef2018-04-30 11:54:39 +02009213void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009214 void *ctx )
9215{
9216 if( ssl->handshake != NULL )
9217 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009218}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009219#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009220
Paul Bakker5121ce52009-01-03 21:22:43 +00009221/*
9222 * SSL get accessors
9223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009224size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009225{
9226 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9227}
9228
Hanno Becker8b170a02017-10-10 11:51:19 +01009229int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9230{
9231 /*
9232 * Case A: We're currently holding back
9233 * a message for further processing.
9234 */
9235
9236 if( ssl->keep_current_message == 1 )
9237 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009238 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009239 return( 1 );
9240 }
9241
9242 /*
9243 * Case B: Further records are pending in the current datagram.
9244 */
9245
9246#if defined(MBEDTLS_SSL_PROTO_DTLS)
9247 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9248 ssl->in_left > ssl->next_record_offset )
9249 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009250 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009251 return( 1 );
9252 }
9253#endif /* MBEDTLS_SSL_PROTO_DTLS */
9254
9255 /*
9256 * Case C: A handshake message is being processed.
9257 */
9258
Hanno Becker8b170a02017-10-10 11:51:19 +01009259 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9260 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009261 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009262 return( 1 );
9263 }
9264
9265 /*
9266 * Case D: An application data message is being processed
9267 */
9268 if( ssl->in_offt != NULL )
9269 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009270 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009271 return( 1 );
9272 }
9273
9274 /*
9275 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009276 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009277 * we implement support for multiple alerts in single records.
9278 */
9279
9280 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9281 return( 0 );
9282}
9283
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009284uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009285{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009286 if( ssl->session != NULL )
9287 return( ssl->session->verify_result );
9288
9289 if( ssl->session_negotiate != NULL )
9290 return( ssl->session_negotiate->verify_result );
9291
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009292 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009293}
9294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009295const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009296{
Paul Bakker926c8e42013-03-06 10:23:34 +01009297 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009298 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009301}
9302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009307 {
9308 switch( ssl->minor_ver )
9309 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009311 return( "DTLSv1.0" );
9312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009313 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009314 return( "DTLSv1.2" );
9315
9316 default:
9317 return( "unknown (DTLS)" );
9318 }
9319 }
9320#endif
9321
Paul Bakker43ca69c2011-01-15 17:35:19 +00009322 switch( ssl->minor_ver )
9323 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009325 return( "SSLv3.0" );
9326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328 return( "TLSv1.0" );
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009331 return( "TLSv1.1" );
9332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009333 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009334 return( "TLSv1.2" );
9335
Paul Bakker43ca69c2011-01-15 17:35:19 +00009336 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009337 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009338 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009339}
9340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009342{
Hanno Becker3136ede2018-08-17 15:28:19 +01009343 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009344 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009345 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009346
Hanno Becker5903de42019-05-03 14:46:38 +01009347 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9348
Hanno Becker78640902018-08-13 16:35:15 +01009349 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009350 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352#if defined(MBEDTLS_ZLIB_SUPPORT)
9353 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9354 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009355#endif
9356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 case MBEDTLS_MODE_GCM:
9360 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009361 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009363 transform_expansion = transform->minlen;
9364 break;
9365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009366 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009367
9368 block_size = mbedtls_cipher_get_block_size(
9369 &transform->cipher_ctx_enc );
9370
Hanno Becker3136ede2018-08-17 15:28:19 +01009371 /* Expansion due to the addition of the MAC. */
9372 transform_expansion += transform->maclen;
9373
9374 /* Expansion due to the addition of CBC padding;
9375 * Theoretically up to 256 bytes, but we never use
9376 * more than the block size of the underlying cipher. */
9377 transform_expansion += block_size;
9378
9379 /* For TLS 1.1 or higher, an explicit IV is added
9380 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009381#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9382 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009383 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009384#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009385
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009386 break;
9387
9388 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009390 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009391 }
9392
Hanno Beckera0e20d02019-05-15 14:03:01 +01009393#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009394 if( transform->out_cid_len != 0 )
9395 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009396#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009397
Hanno Becker5903de42019-05-03 14:46:38 +01009398 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009399}
9400
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009401#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9402size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9403{
9404 size_t max_len;
9405
9406 /*
9407 * Assume mfl_code is correct since it was checked when set
9408 */
Angus Grattond8213d02016-05-25 20:56:48 +10009409 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009410
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009411 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009412 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009413 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009414 {
Angus Grattond8213d02016-05-25 20:56:48 +10009415 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009416 }
9417
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009418 /* During a handshake, use the value being negotiated */
9419 if( ssl->session_negotiate != NULL &&
9420 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9421 {
9422 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9423 }
9424
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009425 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009426}
9427#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9428
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009429#if defined(MBEDTLS_SSL_PROTO_DTLS)
9430static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9431{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009432 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9433 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9434 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9435 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9436 return ( 0 );
9437
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009438 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9439 return( ssl->mtu );
9440
9441 if( ssl->mtu == 0 )
9442 return( ssl->handshake->mtu );
9443
9444 return( ssl->mtu < ssl->handshake->mtu ?
9445 ssl->mtu : ssl->handshake->mtu );
9446}
9447#endif /* MBEDTLS_SSL_PROTO_DTLS */
9448
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009449int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9450{
9451 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9452
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009453#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9454 !defined(MBEDTLS_SSL_PROTO_DTLS)
9455 (void) ssl;
9456#endif
9457
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009458#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9459 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9460
9461 if( max_len > mfl )
9462 max_len = mfl;
9463#endif
9464
9465#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009466 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009467 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009468 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009469 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9470 const size_t overhead = (size_t) ret;
9471
9472 if( ret < 0 )
9473 return( ret );
9474
9475 if( mtu <= overhead )
9476 {
9477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9478 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9479 }
9480
9481 if( max_len > mtu - overhead )
9482 max_len = mtu - overhead;
9483 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009484#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009485
Hanno Becker0defedb2018-08-10 12:35:02 +01009486#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9487 !defined(MBEDTLS_SSL_PROTO_DTLS)
9488 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009489#endif
9490
9491 return( (int) max_len );
9492}
9493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009494#if defined(MBEDTLS_X509_CRT_PARSE_C)
9495const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009496{
9497 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009498 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009499
Hanno Beckere6824572019-02-07 13:18:46 +00009500#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009501 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009502#else
9503 return( NULL );
9504#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009505}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009508#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009509int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9510 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009511{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009512 if( ssl == NULL ||
9513 dst == NULL ||
9514 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009515 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009516 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009518 }
9519
Hanno Becker52055ae2019-02-06 14:30:46 +00009520 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009523
Paul Bakker5121ce52009-01-03 21:22:43 +00009524/*
Paul Bakker1961b702013-01-25 14:49:24 +01009525 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009526 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009528{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009530
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009531 if( ssl == NULL || ssl->conf == NULL )
9532 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009537#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009539 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009540 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009541#endif
9542
Paul Bakker1961b702013-01-25 14:49:24 +01009543 return( ret );
9544}
9545
9546/*
9547 * Perform the SSL handshake
9548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009550{
9551 int ret = 0;
9552
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009553 if( ssl == NULL || ssl->conf == NULL )
9554 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009560 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009561
9562 if( ret != 0 )
9563 break;
9564 }
9565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009567
9568 return( ret );
9569}
9570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009571#if defined(MBEDTLS_SSL_RENEGOTIATION)
9572#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009573/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009574 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577{
9578 int ret;
9579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009581
9582 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009583 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9584 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009585
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009586 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009587 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009588 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009589 return( ret );
9590 }
9591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009593
9594 return( 0 );
9595}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009597
9598/*
9599 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600 * - any side: calling mbedtls_ssl_renegotiate(),
9601 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9602 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009603 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009604 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009606 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009608{
9609 int ret;
9610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009612
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009613 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9614 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009615
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009616 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9617 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009619 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009621 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009623 ssl->handshake->out_msg_seq = 1;
9624 else
9625 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009626 }
9627#endif
9628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9630 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009635 return( ret );
9636 }
9637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009638 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009639
9640 return( 0 );
9641}
9642
9643/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009644 * Renegotiate current connection on client,
9645 * or request renegotiation on server
9646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009649 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009650
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009651 if( ssl == NULL || ssl->conf == NULL )
9652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009655 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009656 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009657 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9659 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009662
9663 /* Did we already try/start sending HelloRequest? */
9664 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009666
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009667 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009668 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009672 /*
9673 * On client, either start the renegotiation process or,
9674 * if already in progress, continue the handshake
9675 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680
9681 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9682 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009683 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009684 return( ret );
9685 }
9686 }
9687 else
9688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009692 return( ret );
9693 }
9694 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009696
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009697 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009698}
9699
9700/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009701 * Check record counters and renegotiate if they're above the limit.
9702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009703static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009704{
Andres AG2196c7f2016-12-15 17:01:16 +00009705 size_t ep_len = ssl_ep_len( ssl );
9706 int in_ctr_cmp;
9707 int out_ctr_cmp;
9708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009709 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9710 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009711 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009712 {
9713 return( 0 );
9714 }
9715
Andres AG2196c7f2016-12-15 17:01:16 +00009716 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9717 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009718 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009719 ssl->conf->renego_period + ep_len, 8 - ep_len );
9720
9721 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009722 {
9723 return( 0 );
9724 }
9725
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009726 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009728}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009729#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009730
9731/*
9732 * Receive application data decrypted from the SSL layer
9733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009734int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009735{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009736 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009737 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009738
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009739 if( ssl == NULL || ssl->conf == NULL )
9740 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009743
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009745 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009746 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009748 return( ret );
9749
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009750 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009751 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009752 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009753 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009754 return( ret );
9755 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009756 }
9757#endif
9758
Hanno Becker4a810fb2017-05-24 16:27:30 +01009759 /*
9760 * Check if renegotiation is necessary and/or handshake is
9761 * in process. If yes, perform/continue, and fall through
9762 * if an unexpected packet is received while the client
9763 * is waiting for the ServerHello.
9764 *
9765 * (There is no equivalent to the last condition on
9766 * the server-side as it is not treated as within
9767 * a handshake while waiting for the ClientHello
9768 * after a renegotiation request.)
9769 */
9770
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009772 ret = ssl_check_ctr_renegotiate( ssl );
9773 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9774 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009777 return( ret );
9778 }
9779#endif
9780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009784 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9785 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009786 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009788 return( ret );
9789 }
9790 }
9791
Hanno Beckere41158b2017-10-23 13:30:32 +01009792 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009793 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009794 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009795 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009796 if( ssl->f_get_timer != NULL &&
9797 ssl->f_get_timer( ssl->p_timer ) == -1 )
9798 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009799 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009800 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009801
Hanno Becker327c93b2018-08-15 13:56:18 +01009802 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009803 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009804 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9805 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009806
Hanno Becker4a810fb2017-05-24 16:27:30 +01009807 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9808 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009809 }
9810
9811 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009813 {
9814 /*
9815 * OpenSSL sends empty messages to randomize the IV
9816 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009817 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009820 return( 0 );
9821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009823 return( ret );
9824 }
9825 }
9826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009830
Hanno Becker4a810fb2017-05-24 16:27:30 +01009831 /*
9832 * - For client-side, expect SERVER_HELLO_REQUEST.
9833 * - For server-side, expect CLIENT_HELLO.
9834 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9835 */
9836
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009838 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009840 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009843
9844 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009846 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009847 {
9848 continue;
9849 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009850#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009851 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009852 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009853#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009854
Hanno Becker4a810fb2017-05-24 16:27:30 +01009855#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009856 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009860
9861 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009862#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009864 {
9865 continue;
9866 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009867#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009868 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009869 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009870#endif /* MBEDTLS_SSL_SRV_C */
9871
Hanno Becker21df7f92017-10-17 11:03:26 +01009872#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009873 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009874 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9875 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9876 ssl->conf->allow_legacy_renegotiation ==
9877 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9878 {
9879 /*
9880 * Accept renegotiation request
9881 */
Paul Bakker48916f92012-09-16 19:57:18 +00009882
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009883 /* DTLS clients need to know renego is server-initiated */
9884#if defined(MBEDTLS_SSL_PROTO_DTLS)
9885 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9886 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9887 {
9888 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9889 }
9890#endif
9891 ret = ssl_start_renegotiation( ssl );
9892 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9893 ret != 0 )
9894 {
9895 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9896 return( ret );
9897 }
9898 }
9899 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009900#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009901 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009902 /*
9903 * Refuse renegotiation
9904 */
9905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009907
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009908#if defined(MBEDTLS_SSL_PROTO_SSL3)
9909 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009910 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009911 /* SSLv3 does not have a "no_renegotiation" warning, so
9912 we send a fatal alert and abort the connection. */
9913 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9914 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9915 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009916 }
9917 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009918#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9919#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9920 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9921 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009923 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9924 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9925 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009926 {
9927 return( ret );
9928 }
Paul Bakker48916f92012-09-16 19:57:18 +00009929 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009930 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9932 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009934 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9935 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009936 }
Paul Bakker48916f92012-09-16 19:57:18 +00009937 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009938
Hanno Becker90333da2017-10-10 11:27:13 +01009939 /* At this point, we don't know whether the renegotiation has been
9940 * completed or not. The cases to consider are the following:
9941 * 1) The renegotiation is complete. In this case, no new record
9942 * has been read yet.
9943 * 2) The renegotiation is incomplete because the client received
9944 * an application data record while awaiting the ServerHello.
9945 * 3) The renegotiation is incomplete because the client received
9946 * a non-handshake, non-application data message while awaiting
9947 * the ServerHello.
9948 * In each of these case, looping will be the proper action:
9949 * - For 1), the next iteration will read a new record and check
9950 * if it's application data.
9951 * - For 2), the loop condition isn't satisfied as application data
9952 * is present, hence continue is the same as break
9953 * - For 3), the loop condition is satisfied and read_record
9954 * will re-deliver the message that was held back by the client
9955 * when expecting the ServerHello.
9956 */
9957 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009958 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009959#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009960 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009961 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009962 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009963 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009964 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009965 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009967 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009969 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009970 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009971 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9975 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009978 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009979 }
9980
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009982 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009983 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9984 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009985 }
9986
9987 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009988
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009989 /* We're going to return something now, cancel timer,
9990 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009991 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009992 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009993
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009995 /* If we requested renego but received AppData, resend HelloRequest.
9996 * Do it now, after setting in_offt, to avoid taking this branch
9997 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009999 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010001 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010002 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010005 return( ret );
10006 }
10007 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010008#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010009#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010010 }
10011
10012 n = ( len < ssl->in_msglen )
10013 ? len : ssl->in_msglen;
10014
10015 memcpy( buf, ssl->in_offt, n );
10016 ssl->in_msglen -= n;
10017
10018 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019 {
10020 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010021 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010022 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010023 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010025 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010026 /* more data available */
10027 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010028 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010031
Paul Bakker23986e52011-04-24 08:57:21 +000010032 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010033}
10034
10035/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010036 * Send application data to be encrypted by the SSL layer, taking care of max
10037 * fragment length and buffer size.
10038 *
10039 * According to RFC 5246 Section 6.2.1:
10040 *
10041 * Zero-length fragments of Application data MAY be sent as they are
10042 * potentially useful as a traffic analysis countermeasure.
10043 *
10044 * Therefore, it is possible that the input message length is 0 and the
10045 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010046 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010047static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010048 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010049{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010050 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10051 const size_t max_len = (size_t) ret;
10052
10053 if( ret < 0 )
10054 {
10055 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10056 return( ret );
10057 }
10058
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010059 if( len > max_len )
10060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010062 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010063 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010065 "maximum fragment length: %d > %d",
10066 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010067 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010068 }
10069 else
10070#endif
10071 len = max_len;
10072 }
Paul Bakker887bd502011-06-08 13:10:54 +000010073
Paul Bakker5121ce52009-01-03 21:22:43 +000010074 if( ssl->out_left != 0 )
10075 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010076 /*
10077 * The user has previously tried to send the data and
10078 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10079 * written. In this case, we expect the high-level write function
10080 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10081 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010083 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010084 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010085 return( ret );
10086 }
10087 }
Paul Bakker887bd502011-06-08 13:10:54 +000010088 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010089 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010090 /*
10091 * The user is trying to send a message the first time, so we need to
10092 * copy the data into the internal buffers and setup the data structure
10093 * to keep track of partial writes
10094 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010095 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010096 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010097 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010098
Hanno Becker67bc7c32018-08-06 11:33:50 +010010099 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010102 return( ret );
10103 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010104 }
10105
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010106 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010107}
10108
10109/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010110 * Write application data, doing 1/n-1 splitting if necessary.
10111 *
10112 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010113 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010114 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010116#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010117static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010118 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010119{
10120 int ret;
10121
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010122 if( ssl->conf->cbc_record_splitting ==
10123 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010124 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010125 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10126 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10127 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010128 {
10129 return( ssl_write_real( ssl, buf, len ) );
10130 }
10131
10132 if( ssl->split_done == 0 )
10133 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010134 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010135 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010136 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010137 }
10138
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010139 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10140 return( ret );
10141 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010142
10143 return( ret + 1 );
10144}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010145#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010146
10147/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010148 * Write application data (public-facing wrapper)
10149 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010150int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010151{
10152 int ret;
10153
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010155
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010156 if( ssl == NULL || ssl->conf == NULL )
10157 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10158
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010159#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010160 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10161 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010162 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010163 return( ret );
10164 }
10165#endif
10166
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010167 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010168 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010169 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010170 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010172 return( ret );
10173 }
10174 }
10175
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010176#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010177 ret = ssl_write_split( ssl, buf, len );
10178#else
10179 ret = ssl_write_real( ssl, buf, len );
10180#endif
10181
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010182 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010183
10184 return( ret );
10185}
10186
10187/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010188 * Notify the peer that the connection is being closed
10189 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010191{
10192 int ret;
10193
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010194 if( ssl == NULL || ssl->conf == NULL )
10195 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010198
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010199 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10205 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10206 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010208 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010209 return( ret );
10210 }
10211 }
10212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010213 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010214
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010215 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010216}
10217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010218void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010219{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010220 if( transform == NULL )
10221 return;
10222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010223#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010224 deflateEnd( &transform->ctx_deflate );
10225 inflateEnd( &transform->ctx_inflate );
10226#endif
10227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10229 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010230
Hanno Beckerd56ed242018-01-03 15:32:51 +000010231#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232 mbedtls_md_free( &transform->md_ctx_enc );
10233 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010234#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010235
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010236 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010237}
10238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239#if defined(MBEDTLS_X509_CRT_PARSE_C)
10240static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010242 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010243
10244 while( cur != NULL )
10245 {
10246 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010248 cur = next;
10249 }
10250}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010252
Hanno Becker0271f962018-08-16 13:23:47 +010010253#if defined(MBEDTLS_SSL_PROTO_DTLS)
10254
10255static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10256{
10257 unsigned offset;
10258 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10259
10260 if( hs == NULL )
10261 return;
10262
Hanno Becker283f5ef2018-08-24 09:34:47 +010010263 ssl_free_buffered_record( ssl );
10264
Hanno Becker0271f962018-08-16 13:23:47 +010010265 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010266 ssl_buffering_free_slot( ssl, offset );
10267}
10268
10269static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10270 uint8_t slot )
10271{
10272 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10273 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010274
10275 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10276 return;
10277
Hanno Beckere605b192018-08-21 15:59:07 +010010278 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010279 {
Hanno Beckere605b192018-08-21 15:59:07 +010010280 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010281 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010282 mbedtls_free( hs_buf->data );
10283 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010284 }
10285}
10286
10287#endif /* MBEDTLS_SSL_PROTO_DTLS */
10288
Gilles Peskine9b562d52018-04-25 20:32:43 +020010289void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010290{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010291 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10292
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010293 if( handshake == NULL )
10294 return;
10295
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010296#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10297 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10298 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010299 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010300 handshake->async_in_progress = 0;
10301 }
10302#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10303
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010304#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10305 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10306 mbedtls_md5_free( &handshake->fin_md5 );
10307 mbedtls_sha1_free( &handshake->fin_sha1 );
10308#endif
10309#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10310#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010311#if defined(MBEDTLS_USE_PSA_CRYPTO)
10312 psa_hash_abort( &handshake->fin_sha256_psa );
10313#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010314 mbedtls_sha256_free( &handshake->fin_sha256 );
10315#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010316#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010317#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010318#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010319 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010320#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010321 mbedtls_sha512_free( &handshake->fin_sha512 );
10322#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010323#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010324#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326#if defined(MBEDTLS_DHM_C)
10327 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010328#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010329#if defined(MBEDTLS_ECDH_C)
10330 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010331#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010332#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010333 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010334#if defined(MBEDTLS_SSL_CLI_C)
10335 mbedtls_free( handshake->ecjpake_cache );
10336 handshake->ecjpake_cache = NULL;
10337 handshake->ecjpake_cache_len = 0;
10338#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010339#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010340
Janos Follath4ae5c292016-02-10 11:27:43 +000010341#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10342 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010343 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010344 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010345#endif
10346
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010347#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10348 if( handshake->psk != NULL )
10349 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010350 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010351 mbedtls_free( handshake->psk );
10352 }
10353#endif
10354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010355#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10356 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010357 /*
10358 * Free only the linked list wrapper, not the keys themselves
10359 * since the belong to the SNI callback
10360 */
10361 if( handshake->sni_key_cert != NULL )
10362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010363 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010364
10365 while( cur != NULL )
10366 {
10367 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010369 cur = next;
10370 }
10371 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010372#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010373
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010374#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010375 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010376 if( handshake->ecrs_peer_cert != NULL )
10377 {
10378 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10379 mbedtls_free( handshake->ecrs_peer_cert );
10380 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010381#endif
10382
Hanno Becker75173122019-02-06 16:18:31 +000010383#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10384 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10385 mbedtls_pk_free( &handshake->peer_pubkey );
10386#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010388#if defined(MBEDTLS_SSL_PROTO_DTLS)
10389 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010390 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010391 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010392#endif
10393
Hanno Becker4a63ed42019-01-08 11:39:35 +000010394#if defined(MBEDTLS_ECDH_C) && \
10395 defined(MBEDTLS_USE_PSA_CRYPTO)
10396 psa_destroy_key( handshake->ecdh_psa_privkey );
10397#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10398
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010399 mbedtls_platform_zeroize( handshake,
10400 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010401}
10402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010403void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010404{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010405 if( session == NULL )
10406 return;
10407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010408#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010409 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010410#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010411
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010412#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010413 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010414#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010415
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010416 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010417}
10418
Paul Bakker5121ce52009-01-03 21:22:43 +000010419/*
10420 * Free an SSL context
10421 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010422void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010423{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010424 if( ssl == NULL )
10425 return;
10426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010428
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010429 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010430 {
Angus Grattond8213d02016-05-25 20:56:48 +100010431 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010432 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010433 }
10434
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010435 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010436 {
Angus Grattond8213d02016-05-25 20:56:48 +100010437 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010439 }
10440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010442 if( ssl->compress_buf != NULL )
10443 {
Angus Grattond8213d02016-05-25 20:56:48 +100010444 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010445 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010446 }
10447#endif
10448
Paul Bakker48916f92012-09-16 19:57:18 +000010449 if( ssl->transform )
10450 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 mbedtls_ssl_transform_free( ssl->transform );
10452 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010453 }
10454
10455 if( ssl->handshake )
10456 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010457 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10459 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 mbedtls_free( ssl->handshake );
10462 mbedtls_free( ssl->transform_negotiate );
10463 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010464 }
10465
Paul Bakkerc0463502013-02-14 11:19:38 +010010466 if( ssl->session )
10467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010468 mbedtls_ssl_session_free( ssl->session );
10469 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010470 }
10471
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010472#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010473 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010474 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010475 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010477 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010478#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10481 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10484 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010485 }
10486#endif
10487
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010488#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010490#endif
10491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010493
Paul Bakker86f04f42013-02-14 11:20:09 +010010494 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010495 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010496}
10497
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010498/*
10499 * Initialze mbedtls_ssl_config
10500 */
10501void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10502{
10503 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10504}
10505
Simon Butcherc97b6972015-12-27 23:48:17 +000010506#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010507static int ssl_preset_default_hashes[] = {
10508#if defined(MBEDTLS_SHA512_C)
10509 MBEDTLS_MD_SHA512,
10510 MBEDTLS_MD_SHA384,
10511#endif
10512#if defined(MBEDTLS_SHA256_C)
10513 MBEDTLS_MD_SHA256,
10514 MBEDTLS_MD_SHA224,
10515#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010516#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010517 MBEDTLS_MD_SHA1,
10518#endif
10519 MBEDTLS_MD_NONE
10520};
Simon Butcherc97b6972015-12-27 23:48:17 +000010521#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010522
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010523static int ssl_preset_suiteb_ciphersuites[] = {
10524 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10525 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10526 0
10527};
10528
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010529#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010530static int ssl_preset_suiteb_hashes[] = {
10531 MBEDTLS_MD_SHA256,
10532 MBEDTLS_MD_SHA384,
10533 MBEDTLS_MD_NONE
10534};
10535#endif
10536
10537#if defined(MBEDTLS_ECP_C)
10538static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10539 MBEDTLS_ECP_DP_SECP256R1,
10540 MBEDTLS_ECP_DP_SECP384R1,
10541 MBEDTLS_ECP_DP_NONE
10542};
10543#endif
10544
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010545/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010546 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010548int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010549 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010550{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010551#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010552 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010553#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010555 /* Use the functions here so that they are covered in tests,
10556 * but otherwise access member directly for efficiency */
10557 mbedtls_ssl_conf_endpoint( conf, endpoint );
10558 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010559
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010560 /*
10561 * Things that are common to all presets
10562 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010563#if defined(MBEDTLS_SSL_CLI_C)
10564 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10565 {
10566 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10567#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10568 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10569#endif
10570 }
10571#endif
10572
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010573#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010574 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010575#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010576
10577#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10578 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10579#endif
10580
10581#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10582 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10583#endif
10584
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010585#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10586 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10587#endif
10588
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010589#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010590 conf->f_cookie_write = ssl_cookie_write_dummy;
10591 conf->f_cookie_check = ssl_cookie_check_dummy;
10592#endif
10593
10594#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10595 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10596#endif
10597
Janos Follath088ce432017-04-10 12:42:31 +010010598#if defined(MBEDTLS_SSL_SRV_C)
10599 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10600#endif
10601
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010602#if defined(MBEDTLS_SSL_PROTO_DTLS)
10603 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10604 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10605#endif
10606
10607#if defined(MBEDTLS_SSL_RENEGOTIATION)
10608 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010609 memset( conf->renego_period, 0x00, 2 );
10610 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010611#endif
10612
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010613#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10614 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10615 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010616 const unsigned char dhm_p[] =
10617 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10618 const unsigned char dhm_g[] =
10619 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10620
Hanno Beckera90658f2017-10-04 15:29:08 +010010621 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10622 dhm_p, sizeof( dhm_p ),
10623 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010624 {
10625 return( ret );
10626 }
10627 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010628#endif
10629
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010630 /*
10631 * Preset-specific defaults
10632 */
10633 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010634 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010635 /*
10636 * NSA Suite B
10637 */
10638 case MBEDTLS_SSL_PRESET_SUITEB:
10639 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10640 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10641 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10642 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10643
10644 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10645 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10646 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10647 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10648 ssl_preset_suiteb_ciphersuites;
10649
10650#if defined(MBEDTLS_X509_CRT_PARSE_C)
10651 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010652#endif
10653
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010654#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010655 conf->sig_hashes = ssl_preset_suiteb_hashes;
10656#endif
10657
10658#if defined(MBEDTLS_ECP_C)
10659 conf->curve_list = ssl_preset_suiteb_curves;
10660#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010661 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010662
10663 /*
10664 * Default
10665 */
10666 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010667 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10668 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10669 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10670 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10671 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10672 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10673 MBEDTLS_SSL_MIN_MINOR_VERSION :
10674 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010675 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10676 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10677
10678#if defined(MBEDTLS_SSL_PROTO_DTLS)
10679 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10680 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10681#endif
10682
10683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10684 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10685 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10686 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10687 mbedtls_ssl_list_ciphersuites();
10688
10689#if defined(MBEDTLS_X509_CRT_PARSE_C)
10690 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10691#endif
10692
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010693#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010694 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010695#endif
10696
10697#if defined(MBEDTLS_ECP_C)
10698 conf->curve_list = mbedtls_ecp_grp_id_list();
10699#endif
10700
10701#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10702 conf->dhm_min_bitlen = 1024;
10703#endif
10704 }
10705
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010706 return( 0 );
10707}
10708
10709/*
10710 * Free mbedtls_ssl_config
10711 */
10712void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10713{
10714#if defined(MBEDTLS_DHM_C)
10715 mbedtls_mpi_free( &conf->dhm_P );
10716 mbedtls_mpi_free( &conf->dhm_G );
10717#endif
10718
10719#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10720 if( conf->psk != NULL )
10721 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010722 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010724 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010725 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010726 }
10727
10728 if( conf->psk_identity != NULL )
10729 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010730 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010731 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010732 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010733 conf->psk_identity_len = 0;
10734 }
10735#endif
10736
10737#if defined(MBEDTLS_X509_CRT_PARSE_C)
10738 ssl_key_cert_free( conf->key_cert );
10739#endif
10740
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010741 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010742}
10743
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010744#if defined(MBEDTLS_PK_C) && \
10745 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010746/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751#if defined(MBEDTLS_RSA_C)
10752 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10753 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755#if defined(MBEDTLS_ECDSA_C)
10756 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10757 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010760}
10761
Hanno Becker7e5437a2017-04-28 17:15:26 +010010762unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10763{
10764 switch( type ) {
10765 case MBEDTLS_PK_RSA:
10766 return( MBEDTLS_SSL_SIG_RSA );
10767 case MBEDTLS_PK_ECDSA:
10768 case MBEDTLS_PK_ECKEY:
10769 return( MBEDTLS_SSL_SIG_ECDSA );
10770 default:
10771 return( MBEDTLS_SSL_SIG_ANON );
10772 }
10773}
10774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010776{
10777 switch( sig )
10778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779#if defined(MBEDTLS_RSA_C)
10780 case MBEDTLS_SSL_SIG_RSA:
10781 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783#if defined(MBEDTLS_ECDSA_C)
10784 case MBEDTLS_SSL_SIG_ECDSA:
10785 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010786#endif
10787 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010789 }
10790}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010791#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010792
Hanno Becker7e5437a2017-04-28 17:15:26 +010010793#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10794 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10795
10796/* Find an entry in a signature-hash set matching a given hash algorithm. */
10797mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10798 mbedtls_pk_type_t sig_alg )
10799{
10800 switch( sig_alg )
10801 {
10802 case MBEDTLS_PK_RSA:
10803 return( set->rsa );
10804 case MBEDTLS_PK_ECDSA:
10805 return( set->ecdsa );
10806 default:
10807 return( MBEDTLS_MD_NONE );
10808 }
10809}
10810
10811/* Add a signature-hash-pair to a signature-hash set */
10812void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10813 mbedtls_pk_type_t sig_alg,
10814 mbedtls_md_type_t md_alg )
10815{
10816 switch( sig_alg )
10817 {
10818 case MBEDTLS_PK_RSA:
10819 if( set->rsa == MBEDTLS_MD_NONE )
10820 set->rsa = md_alg;
10821 break;
10822
10823 case MBEDTLS_PK_ECDSA:
10824 if( set->ecdsa == MBEDTLS_MD_NONE )
10825 set->ecdsa = md_alg;
10826 break;
10827
10828 default:
10829 break;
10830 }
10831}
10832
10833/* Allow exactly one hash algorithm for each signature. */
10834void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10835 mbedtls_md_type_t md_alg )
10836{
10837 set->rsa = md_alg;
10838 set->ecdsa = md_alg;
10839}
10840
10841#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10842 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10843
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010844/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010845 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010848{
10849 switch( hash )
10850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851#if defined(MBEDTLS_MD5_C)
10852 case MBEDTLS_SSL_HASH_MD5:
10853 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855#if defined(MBEDTLS_SHA1_C)
10856 case MBEDTLS_SSL_HASH_SHA1:
10857 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859#if defined(MBEDTLS_SHA256_C)
10860 case MBEDTLS_SSL_HASH_SHA224:
10861 return( MBEDTLS_MD_SHA224 );
10862 case MBEDTLS_SSL_HASH_SHA256:
10863 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865#if defined(MBEDTLS_SHA512_C)
10866 case MBEDTLS_SSL_HASH_SHA384:
10867 return( MBEDTLS_MD_SHA384 );
10868 case MBEDTLS_SSL_HASH_SHA512:
10869 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010870#endif
10871 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010873 }
10874}
10875
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010876/*
10877 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10878 */
10879unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10880{
10881 switch( md )
10882 {
10883#if defined(MBEDTLS_MD5_C)
10884 case MBEDTLS_MD_MD5:
10885 return( MBEDTLS_SSL_HASH_MD5 );
10886#endif
10887#if defined(MBEDTLS_SHA1_C)
10888 case MBEDTLS_MD_SHA1:
10889 return( MBEDTLS_SSL_HASH_SHA1 );
10890#endif
10891#if defined(MBEDTLS_SHA256_C)
10892 case MBEDTLS_MD_SHA224:
10893 return( MBEDTLS_SSL_HASH_SHA224 );
10894 case MBEDTLS_MD_SHA256:
10895 return( MBEDTLS_SSL_HASH_SHA256 );
10896#endif
10897#if defined(MBEDTLS_SHA512_C)
10898 case MBEDTLS_MD_SHA384:
10899 return( MBEDTLS_SSL_HASH_SHA384 );
10900 case MBEDTLS_MD_SHA512:
10901 return( MBEDTLS_SSL_HASH_SHA512 );
10902#endif
10903 default:
10904 return( MBEDTLS_SSL_HASH_NONE );
10905 }
10906}
10907
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010908#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010909/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010910 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010911 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010912 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010913int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010914{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010916
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010917 if( ssl->conf->curve_list == NULL )
10918 return( -1 );
10919
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010920 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010921 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010922 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010923
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010924 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010925}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010926#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010927
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010928#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010929/*
10930 * Check if a hash proposed by the peer is in our list.
10931 * Return 0 if we're willing to use it, -1 otherwise.
10932 */
10933int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10934 mbedtls_md_type_t md )
10935{
10936 const int *cur;
10937
10938 if( ssl->conf->sig_hashes == NULL )
10939 return( -1 );
10940
10941 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10942 if( *cur == (int) md )
10943 return( 0 );
10944
10945 return( -1 );
10946}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010947#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949#if defined(MBEDTLS_X509_CRT_PARSE_C)
10950int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10951 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010952 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010953 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010954{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010955 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010957 int usage = 0;
10958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010960 const char *ext_oid;
10961 size_t ext_len;
10962#endif
10963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10965 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010966 ((void) cert);
10967 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010968 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010969#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10972 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010973 {
10974 /* Server part of the key exchange */
10975 switch( ciphersuite->key_exchange )
10976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010977 case MBEDTLS_KEY_EXCHANGE_RSA:
10978 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010979 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010980 break;
10981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10983 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10984 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10985 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010986 break;
10987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010988 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10989 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010990 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010991 break;
10992
10993 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994 case MBEDTLS_KEY_EXCHANGE_NONE:
10995 case MBEDTLS_KEY_EXCHANGE_PSK:
10996 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10997 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010998 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010999 usage = 0;
11000 }
11001 }
11002 else
11003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11005 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011006 }
11007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011009 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011010 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011011 ret = -1;
11012 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011013#else
11014 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11018 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11021 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011022 }
11023 else
11024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11026 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011027 }
11028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011030 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011031 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011032 ret = -1;
11033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011035
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011036 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011039
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011040/*
11041 * Convert version numbers to/from wire format
11042 * and, for DTLS, to/from TLS equivalent.
11043 *
11044 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011045 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011046 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11047 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011050 unsigned char ver[2] )
11051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052#if defined(MBEDTLS_SSL_PROTO_DTLS)
11053 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011056 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11057
11058 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11059 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11060 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011061 else
11062#else
11063 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011064#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011065 {
11066 ver[0] = (unsigned char) major;
11067 ver[1] = (unsigned char) minor;
11068 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011069}
11070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011072 const unsigned char ver[2] )
11073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#if defined(MBEDTLS_SSL_PROTO_DTLS)
11075 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011076 {
11077 *major = 255 - ver[0] + 2;
11078 *minor = 255 - ver[1] + 1;
11079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011081 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11082 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011083 else
11084#else
11085 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011086#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011087 {
11088 *major = ver[0];
11089 *minor = ver[1];
11090 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011091}
11092
Simon Butcher99000142016-10-13 17:21:01 +010011093int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11094{
11095#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11096 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11097 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11098
11099 switch( md )
11100 {
11101#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11102#if defined(MBEDTLS_MD5_C)
11103 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011104 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011105#endif
11106#if defined(MBEDTLS_SHA1_C)
11107 case MBEDTLS_SSL_HASH_SHA1:
11108 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11109 break;
11110#endif
11111#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11112#if defined(MBEDTLS_SHA512_C)
11113 case MBEDTLS_SSL_HASH_SHA384:
11114 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11115 break;
11116#endif
11117#if defined(MBEDTLS_SHA256_C)
11118 case MBEDTLS_SSL_HASH_SHA256:
11119 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11120 break;
11121#endif
11122 default:
11123 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11124 }
11125
11126 return 0;
11127#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11128 (void) ssl;
11129 (void) md;
11130
11131 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11132#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11133}
11134
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11136 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11137int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11138 unsigned char *output,
11139 unsigned char *data, size_t data_len )
11140{
11141 int ret = 0;
11142 mbedtls_md5_context mbedtls_md5;
11143 mbedtls_sha1_context mbedtls_sha1;
11144
11145 mbedtls_md5_init( &mbedtls_md5 );
11146 mbedtls_sha1_init( &mbedtls_sha1 );
11147
11148 /*
11149 * digitally-signed struct {
11150 * opaque md5_hash[16];
11151 * opaque sha_hash[20];
11152 * };
11153 *
11154 * md5_hash
11155 * MD5(ClientHello.random + ServerHello.random
11156 * + ServerParams);
11157 * sha_hash
11158 * SHA(ClientHello.random + ServerHello.random
11159 * + ServerParams);
11160 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011161 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011162 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 goto exit;
11165 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011166 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011167 ssl->handshake->randbytes, 64 ) ) != 0 )
11168 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011170 goto exit;
11171 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011172 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011173 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 goto exit;
11176 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011177 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011178 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 goto exit;
11181 }
11182
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011183 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 goto exit;
11187 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 ssl->handshake->randbytes, 64 ) ) != 0 )
11190 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011192 goto exit;
11193 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011194 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011195 data_len ) ) != 0 )
11196 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011198 goto exit;
11199 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011200 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011201 output + 16 ) ) != 0 )
11202 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011204 goto exit;
11205 }
11206
11207exit:
11208 mbedtls_md5_free( &mbedtls_md5 );
11209 mbedtls_sha1_free( &mbedtls_sha1 );
11210
11211 if( ret != 0 )
11212 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11213 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11214
11215 return( ret );
11216
11217}
11218#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11219 MBEDTLS_SSL_PROTO_TLS1_1 */
11220
11221#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11222 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011223
11224#if defined(MBEDTLS_USE_PSA_CRYPTO)
11225int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11226 unsigned char *hash, size_t *hashlen,
11227 unsigned char *data, size_t data_len,
11228 mbedtls_md_type_t md_alg )
11229{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011230 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011231 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011232 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11233
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011235
11236 if( ( status = psa_hash_setup( &hash_operation,
11237 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011238 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011239 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011240 goto exit;
11241 }
11242
Andrzej Kurek814feff2019-01-14 04:35:19 -050011243 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11244 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011245 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011246 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 goto exit;
11248 }
11249
Andrzej Kurek814feff2019-01-14 04:35:19 -050011250 if( ( status = psa_hash_update( &hash_operation,
11251 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011252 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011253 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 goto exit;
11255 }
11256
Andrzej Kurek814feff2019-01-14 04:35:19 -050011257 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11258 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011259 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011260 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011261 goto exit;
11262 }
11263
11264exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011265 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011266 {
11267 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11268 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011269 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011270 {
11271 case PSA_ERROR_NOT_SUPPORTED:
11272 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011273 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011274 case PSA_ERROR_BUFFER_TOO_SMALL:
11275 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11276 case PSA_ERROR_INSUFFICIENT_MEMORY:
11277 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11278 default:
11279 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11280 }
11281 }
11282 return( 0 );
11283}
11284
11285#else
11286
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011287int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011288 unsigned char *hash, size_t *hashlen,
11289 unsigned char *data, size_t data_len,
11290 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011291{
11292 int ret = 0;
11293 mbedtls_md_context_t ctx;
11294 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011295 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011298
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011299 mbedtls_md_init( &ctx );
11300
11301 /*
11302 * digitally-signed struct {
11303 * opaque client_random[32];
11304 * opaque server_random[32];
11305 * ServerDHParams params;
11306 * };
11307 */
11308 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11309 {
11310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11311 goto exit;
11312 }
11313 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11314 {
11315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11316 goto exit;
11317 }
11318 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11319 {
11320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11321 goto exit;
11322 }
11323 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11324 {
11325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11326 goto exit;
11327 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011328 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 {
11330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11331 goto exit;
11332 }
11333
11334exit:
11335 mbedtls_md_free( &ctx );
11336
11337 if( ret != 0 )
11338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11339 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11340
11341 return( ret );
11342}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011343#endif /* MBEDTLS_USE_PSA_CRYPTO */
11344
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11346 MBEDTLS_SSL_PROTO_TLS1_2 */
11347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348#endif /* MBEDTLS_SSL_TLS_C */