blob: 22adfc50c597cd7c74f709712e06412c9e4334f2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100114
115#define SSL_DONT_FORCE_FLUSH 0
116#define SSL_FORCE_FLUSH 1
117
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100119
Hanno Beckera0e20d02019-05-15 14:03:01 +0100120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100121/* Top-level Connection ID API */
122
Hanno Becker8367ccc2019-05-14 11:30:10 +0100123int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
124 size_t len,
125 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100126{
127 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
128 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
129
Hanno Becker611ac772019-05-14 11:45:26 +0100130 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
131 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
132 {
133 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
134 }
135
136 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100137 conf->cid_len = len;
138 return( 0 );
139}
140
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100141int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
142 int enable,
143 unsigned char const *own_cid,
144 size_t own_cid_len )
145{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100146 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
147 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
148
Hanno Beckerca092242019-04-25 16:01:49 +0100149 ssl->negotiate_cid = enable;
150 if( enable == MBEDTLS_SSL_CID_DISABLED )
151 {
152 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
153 return( 0 );
154 }
155 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100156 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100157
Hanno Beckerad4a1372019-05-03 13:06:44 +0100158 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100159 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100160 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
161 (unsigned) own_cid_len,
162 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100163 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
164 }
165
166 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100167 /* Truncation is not an issue here because
168 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
169 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100170
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100171 return( 0 );
172}
173
174int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
175 int *enabled,
176 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
177 size_t *peer_cid_len )
178{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100179 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180
Hanno Becker76a79ab2019-05-03 14:38:32 +0100181 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
182 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
183 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100184 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100185 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100186
Hanno Beckerc5f24222019-05-03 12:54:52 +0100187 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
188 * were used, but client and server requested the empty CID.
189 * This is indistinguishable from not using the CID extension
190 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100191 if( ssl->transform_in->in_cid_len == 0 &&
192 ssl->transform_in->out_cid_len == 0 )
193 {
194 return( 0 );
195 }
196
Hanno Becker615ef172019-05-22 16:50:35 +0100197 if( peer_cid_len != NULL )
198 {
199 *peer_cid_len = ssl->transform_in->out_cid_len;
200 if( peer_cid != NULL )
201 {
202 memcpy( peer_cid, ssl->transform_in->out_cid,
203 ssl->transform_in->out_cid_len );
204 }
205 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100206
207 *enabled = MBEDTLS_SSL_CID_ENABLED;
208
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209 return( 0 );
210}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100211#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100212
Hanno Beckerd5847772018-08-28 10:09:23 +0100213/* Forward declarations for functions related to message buffering. */
214static void ssl_buffering_free( mbedtls_ssl_context *ssl );
215static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
216 uint8_t slot );
217static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
218static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
219static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
220static int ssl_buffer_message( mbedtls_ssl_context *ssl );
221static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100222static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100223
Hanno Beckera67dee22018-08-22 10:05:20 +0100224static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100225static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100226{
Hanno Becker11682cc2018-08-22 14:41:02 +0100227 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100228
229 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100230 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100231
232 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
233}
234
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
236{
Hanno Becker11682cc2018-08-22 14:41:02 +0100237 size_t const bytes_written = ssl->out_left;
238 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100239
240 /* Double-check that the write-index hasn't gone
241 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100242 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100243 {
244 /* Should never happen... */
245 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
246 }
247
248 return( (int) ( mtu - bytes_written ) );
249}
250
251static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
252{
253 int ret;
254 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400255 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100256
257#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
258 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
259
260 if( max_len > mfl )
261 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100262
263 /* By the standard (RFC 6066 Sect. 4), the MFL extension
264 * only limits the maximum record payload size, so in theory
265 * we would be allowed to pack multiple records of payload size
266 * MFL into a single datagram. However, this would mean that there's
267 * no way to explicitly communicate MTU restrictions to the peer.
268 *
269 * The following reduction of max_len makes sure that we never
270 * write datagrams larger than MFL + Record Expansion Overhead.
271 */
272 if( max_len <= ssl->out_left )
273 return( 0 );
274
275 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100276#endif
277
278 ret = ssl_get_remaining_space_in_datagram( ssl );
279 if( ret < 0 )
280 return( ret );
281 remaining = (size_t) ret;
282
283 ret = mbedtls_ssl_get_record_expansion( ssl );
284 if( ret < 0 )
285 return( ret );
286 expansion = (size_t) ret;
287
288 if( remaining <= expansion )
289 return( 0 );
290
291 remaining -= expansion;
292 if( remaining >= max_len )
293 remaining = max_len;
294
295 return( (int) remaining );
296}
297
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200298/*
299 * Double the retransmit timeout value, within the allowed range,
300 * returning -1 if the maximum value has already been reached.
301 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303{
304 uint32_t new_timeout;
305
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200306 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 return( -1 );
308
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200309 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
310 * in the following way: after the initial transmission and a first
311 * retransmission, back off to a temporary estimated MTU of 508 bytes.
312 * This value is guaranteed to be deliverable (if not guaranteed to be
313 * delivered) of any compliant IPv4 (and IPv6) network, and should work
314 * on most non-IP stacks too. */
315 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400316 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200317 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
319 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200320
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200321 new_timeout = 2 * ssl->handshake->retransmit_timeout;
322
323 /* Avoid arithmetic overflow and range overflow */
324 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200325 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200326 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200327 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200328 }
329
330 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200332 ssl->handshake->retransmit_timeout ) );
333
334 return( 0 );
335}
336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200338{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200339 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200341 ssl->handshake->retransmit_timeout ) );
342}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200345#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200346/*
347 * Convert max_fragment_length codes to length.
348 * RFC 6066 says:
349 * enum{
350 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
351 * } MaxFragmentLength;
352 * and we add 0 -> extension unused
353 */
Angus Grattond8213d02016-05-25 20:56:48 +1000354static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200355{
Angus Grattond8213d02016-05-25 20:56:48 +1000356 switch( mfl )
357 {
358 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
359 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
360 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
361 return 512;
362 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
363 return 1024;
364 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
365 return 2048;
366 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
367 return 4096;
368 default:
369 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
370 }
371}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200372#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200373
Hanno Becker52055ae2019-02-06 14:30:46 +0000374int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
375 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200377 mbedtls_ssl_session_free( dst );
378 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200380#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000381
382#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200383 if( src->peer_cert != NULL )
384 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200385 int ret;
386
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200387 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200388 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200389 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200394 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200397 dst->peer_cert = NULL;
398 return( ret );
399 }
400 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000401#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000402 if( src->peer_cert_digest != NULL )
403 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000404 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000405 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000406 if( dst->peer_cert_digest == NULL )
407 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
408
409 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
410 src->peer_cert_digest_len );
411 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000412 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000413 }
414#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200416#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200417
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200418#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200419 if( src->ticket != NULL )
420 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200421 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200422 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200423 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 memcpy( dst->ticket, src->ticket, src->ticket_len );
426 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200427#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200428
429 return( 0 );
430}
431
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
433int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200434 const unsigned char *key_enc, const unsigned char *key_dec,
435 size_t keylen,
436 const unsigned char *iv_enc, const unsigned char *iv_dec,
437 size_t ivlen,
438 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200439 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
441int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
442int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
443int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
444int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
445#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000446
Paul Bakker5121ce52009-01-03 21:22:43 +0000447/*
448 * Key material generation
449 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200451static int ssl3_prf( const unsigned char *secret, size_t slen,
452 const char *label,
453 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000454 unsigned char *dstbuf, size_t dlen )
455{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100456 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000457 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200458 mbedtls_md5_context md5;
459 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000460 unsigned char padding[16];
461 unsigned char sha1sum[20];
462 ((void)label);
463
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200464 mbedtls_md5_init( &md5 );
465 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200466
Paul Bakker5f70b252012-09-13 14:23:06 +0000467 /*
468 * SSLv3:
469 * block =
470 * MD5( secret + SHA1( 'A' + secret + random ) ) +
471 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
472 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
473 * ...
474 */
475 for( i = 0; i < dlen / 16; i++ )
476 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200477 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100480 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100481 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100485 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100486 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100487 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100488 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000489
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100494 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100495 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100496 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100497 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000498 }
499
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100500exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200501 mbedtls_md5_free( &md5 );
502 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000503
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500504 mbedtls_platform_zeroize( padding, sizeof( padding ) );
505 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100507 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000508}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200511#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200512static int tls1_prf( const unsigned char *secret, size_t slen,
513 const char *label,
514 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000515 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000516{
Paul Bakker23986e52011-04-24 08:57:21 +0000517 size_t nb, hs;
518 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200519 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300520 unsigned char *tmp;
521 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000522 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 const mbedtls_md_info_t *md_info;
524 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100525 int ret;
526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200527 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000528
Ron Eldor3b350852019-05-07 18:31:49 +0300529 tmp_len = 20 + strlen( label ) + rlen;
530 tmp = mbedtls_calloc( 1, tmp_len );
531 if( tmp == NULL )
532 {
533 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
534 goto exit;
535 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000536
537 hs = ( slen + 1 ) / 2;
538 S1 = secret;
539 S2 = secret + slen - hs;
540
541 nb = strlen( label );
542 memcpy( tmp + 20, label, nb );
543 memcpy( tmp + 20 + nb, random, rlen );
544 nb += rlen;
545
546 /*
547 * First compute P_md5(secret,label+random)[0..dlen]
548 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200549 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300550 {
551 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
552 goto exit;
553 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300556 {
557 goto exit;
558 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
561 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
562 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000563
564 for( i = 0; i < dlen; i += 16 )
565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 mbedtls_md_hmac_reset ( &md_ctx );
567 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
568 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200570 mbedtls_md_hmac_reset ( &md_ctx );
571 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
572 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000573
574 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
575
576 for( j = 0; j < k; j++ )
577 dstbuf[i + j] = h_i[j];
578 }
579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100581
Paul Bakker5121ce52009-01-03 21:22:43 +0000582 /*
583 * XOR out with P_sha1(secret,label+random)[0..dlen]
584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200585 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300586 {
587 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
588 goto exit;
589 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200591 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300592 {
593 goto exit;
594 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200596 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
597 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
598 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599
600 for( i = 0; i < dlen; i += 20 )
601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602 mbedtls_md_hmac_reset ( &md_ctx );
603 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
604 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200606 mbedtls_md_hmac_reset ( &md_ctx );
607 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
608 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
611
612 for( j = 0; j < k; j++ )
613 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
614 }
615
Ron Eldor3b350852019-05-07 18:31:49 +0300616exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200617 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100618
Ron Eldor3b350852019-05-07 18:31:49 +0300619 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500620 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Ron Eldor3b350852019-05-07 18:31:49 +0300622 mbedtls_free( tmp );
623 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000624}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200625#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500628#if defined(MBEDTLS_USE_PSA_CRYPTO)
629static int tls_prf_generic( mbedtls_md_type_t md_type,
630 const unsigned char *secret, size_t slen,
631 const char *label,
632 const unsigned char *random, size_t rlen,
633 unsigned char *dstbuf, size_t dlen )
634{
635 psa_status_t status;
636 psa_algorithm_t alg;
637 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500638 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500639 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
640
Andrzej Kurek2f760752019-01-28 08:08:15 -0500641 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500642 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500643
Andrzej Kurekc929a822019-01-14 03:51:11 -0500644 if( md_type == MBEDTLS_MD_SHA384 )
645 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
646 else
647 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
648
Andrzej Kurek2f760752019-01-28 08:08:15 -0500649 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500650 psa_key_policy_set_usage( &policy,
651 PSA_KEY_USAGE_DERIVE,
652 alg );
653 status = psa_set_key_policy( master_slot, &policy );
654 if( status != PSA_SUCCESS )
655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
656
657 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
658 if( status != PSA_SUCCESS )
659 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
660
661 status = psa_key_derivation( &generator,
662 master_slot, alg,
663 random, rlen,
664 (unsigned char const *) label,
665 (size_t) strlen( label ),
666 dlen );
667 if( status != PSA_SUCCESS )
668 {
669 psa_generator_abort( &generator );
670 psa_destroy_key( master_slot );
671 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
672 }
673
674 status = psa_generator_read( &generator, dstbuf, dlen );
675 if( status != PSA_SUCCESS )
676 {
677 psa_generator_abort( &generator );
678 psa_destroy_key( master_slot );
679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
680 }
681
682 status = psa_generator_abort( &generator );
683 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500684 {
685 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500686 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500687 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500688
689 status = psa_destroy_key( master_slot );
690 if( status != PSA_SUCCESS )
691 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
692
Andrzej Kurek33171262019-01-15 03:25:18 -0500693 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500694}
695
696#else /* MBEDTLS_USE_PSA_CRYPTO */
697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200698static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100699 const unsigned char *secret, size_t slen,
700 const char *label,
701 const unsigned char *random, size_t rlen,
702 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000703{
704 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100705 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300706 unsigned char *tmp;
707 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200708 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
709 const mbedtls_md_info_t *md_info;
710 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100711 int ret;
712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200713 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
716 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200718 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100719
Ron Eldor3b350852019-05-07 18:31:49 +0300720 tmp_len = md_len + strlen( label ) + rlen;
721 tmp = mbedtls_calloc( 1, tmp_len );
722 if( tmp == NULL )
723 {
724 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
725 goto exit;
726 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727
728 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100729 memcpy( tmp + md_len, label, nb );
730 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000731 nb += rlen;
732
733 /*
734 * Compute P_<hash>(secret, label + random)[0..dlen]
735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300737 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200739 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
740 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
741 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100742
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100743 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745 mbedtls_md_hmac_reset ( &md_ctx );
746 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
747 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200749 mbedtls_md_hmac_reset ( &md_ctx );
750 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
751 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000752
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100753 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000754
755 for( j = 0; j < k; j++ )
756 dstbuf[i + j] = h_i[j];
757 }
758
Ron Eldor3b350852019-05-07 18:31:49 +0300759exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200760 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100761
Ron Eldor3b350852019-05-07 18:31:49 +0300762 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500763 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764
Ron Eldor3b350852019-05-07 18:31:49 +0300765 mbedtls_free( tmp );
766
767 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000768}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500769#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100771static int tls_prf_sha256( const unsigned char *secret, size_t slen,
772 const char *label,
773 const unsigned char *random, size_t rlen,
774 unsigned char *dstbuf, size_t dlen )
775{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100777 label, random, rlen, dstbuf, dlen ) );
778}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200779#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200782static int tls_prf_sha384( const unsigned char *secret, size_t slen,
783 const char *label,
784 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785 unsigned char *dstbuf, size_t dlen )
786{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100788 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790#endif /* MBEDTLS_SHA512_C */
791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200793static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200795#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
796 defined(MBEDTLS_SSL_PROTO_TLS1_1)
797static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200798#endif
Paul Bakker380da532012-04-18 16:10:25 +0000799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200800#if defined(MBEDTLS_SSL_PROTO_SSL3)
801static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
802static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200803#endif
804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200805#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
806static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
807static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200808#endif
809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200810#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
811#if defined(MBEDTLS_SHA256_C)
812static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
813static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
814static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200815#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200817#if defined(MBEDTLS_SHA512_C)
818static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
819static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
820static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100821#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200822#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000823
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100824#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100825 defined(MBEDTLS_USE_PSA_CRYPTO)
826static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
827{
828 if( ssl->conf->f_psk != NULL )
829 {
830 /* If we've used a callback to select the PSK,
831 * the static configuration is irrelevant. */
832 if( ssl->handshake->psk_opaque != 0 )
833 return( 1 );
834
835 return( 0 );
836 }
837
838 if( ssl->conf->psk_opaque != 0 )
839 return( 1 );
840
841 return( 0 );
842}
843#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100844 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100845
Ron Eldorcf280092019-05-14 20:19:13 +0300846#if defined(MBEDTLS_SSL_EXPORT_KEYS)
847static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
848{
849#if defined(MBEDTLS_SSL_PROTO_SSL3)
850 if( tls_prf == ssl3_prf )
851 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300852 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300853 }
854 else
855#endif
856#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
857 if( tls_prf == tls1_prf )
858 {
859 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
860 }
861 else
862#endif
863#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
864#if defined(MBEDTLS_SHA512_C)
865 if( tls_prf == tls_prf_sha384 )
866 {
867 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
868 }
869 else
870#endif
871#if defined(MBEDTLS_SHA256_C)
872 if( tls_prf == tls_prf_sha256 )
873 {
874 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
875 }
876 else
877#endif
878#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
879 return( MBEDTLS_SSL_TLS_PRF_NONE );
880}
881#endif /* MBEDTLS_SSL_EXPORT_KEYS */
882
Ron Eldor51d3ab52019-05-12 14:54:30 +0300883int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
884 const unsigned char *secret, size_t slen,
885 const char *label,
886 const unsigned char *random, size_t rlen,
887 unsigned char *dstbuf, size_t dlen )
888{
889 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
890
891 switch( prf )
892 {
893#if defined(MBEDTLS_SSL_PROTO_SSL3)
894 case MBEDTLS_SSL_TLS_PRF_SSL3:
895 tls_prf = ssl3_prf;
896 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300897#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300898#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
899 case MBEDTLS_SSL_TLS_PRF_TLS1:
900 tls_prf = tls1_prf;
901 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300902#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
903
904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300905#if defined(MBEDTLS_SHA512_C)
906 case MBEDTLS_SSL_TLS_PRF_SHA384:
907 tls_prf = tls_prf_sha384;
908 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300909#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300910#if defined(MBEDTLS_SHA256_C)
911 case MBEDTLS_SSL_TLS_PRF_SHA256:
912 tls_prf = tls_prf_sha256;
913 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300914#endif /* MBEDTLS_SHA256_C */
915#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300916 default:
917 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
918 }
919
920 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
921}
922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200923int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000924{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200925 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000926#if defined(MBEDTLS_USE_PSA_CRYPTO)
927 int psa_fallthrough;
928#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000929 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000930 unsigned char keyblk[256];
931 unsigned char *key1;
932 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100933 unsigned char *mac_enc;
934 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000935 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200936 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000937 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000938 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200939 const mbedtls_cipher_info_t *cipher_info;
940 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100941
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000942 /* cf. RFC 5246, Section 8.1:
943 * "The master secret is always exactly 48 bytes in length." */
944 size_t const master_secret_len = 48;
945
Hanno Becker35b23c72018-10-23 12:10:41 +0100946#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
947 unsigned char session_hash[48];
948#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 mbedtls_ssl_session *session = ssl->session_negotiate;
951 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
952 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000955
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000956#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
957 transform->encrypt_then_mac = session->encrypt_then_mac;
958#endif
959 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000960
961 ciphersuite_info = handshake->ciphersuite_info;
962 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100963 if( cipher_info == NULL )
964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000966 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100968 }
969
Hanno Beckere694c3e2017-12-27 21:34:08 +0000970 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100971 if( md_info == NULL )
972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000974 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200975 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100976 }
977
Hanno Beckera0e20d02019-05-15 14:03:01 +0100978#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100979 /* Copy own and peer's CID if the use of the CID
980 * extension has been negotiated. */
981 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
982 {
983 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100984
Hanno Becker05154c32019-05-03 15:23:51 +0100985 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100986 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100987 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100988 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100989
990 transform->out_cid_len = ssl->handshake->peer_cid_len;
991 memcpy( transform->out_cid, ssl->handshake->peer_cid,
992 ssl->handshake->peer_cid_len );
993 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
994 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100995 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100996#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +0100997
Paul Bakker5121ce52009-01-03 21:22:43 +0000998 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000999 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +00001000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001001#if defined(MBEDTLS_SSL_PROTO_SSL3)
1002 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001003 {
Paul Bakker48916f92012-09-16 19:57:18 +00001004 handshake->tls_prf = ssl3_prf;
1005 handshake->calc_verify = ssl_calc_verify_ssl;
1006 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001007 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001008 else
1009#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001010#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1011 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001012 {
Paul Bakker48916f92012-09-16 19:57:18 +00001013 handshake->tls_prf = tls1_prf;
1014 handshake->calc_verify = ssl_calc_verify_tls;
1015 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001016 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001017 else
1018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1020#if defined(MBEDTLS_SHA512_C)
1021 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001022 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001023 {
Paul Bakker48916f92012-09-16 19:57:18 +00001024 handshake->tls_prf = tls_prf_sha384;
1025 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1026 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001027 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001028 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001030#if defined(MBEDTLS_SHA256_C)
1031 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001032 {
Paul Bakker48916f92012-09-16 19:57:18 +00001033 handshake->tls_prf = tls_prf_sha256;
1034 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1035 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001036 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001037 else
1038#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001040 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001041 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1042 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001043 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001044
1045 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001046 * SSLv3:
1047 * master =
1048 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1049 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1050 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001051 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001052 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 * master = PRF( premaster, "master secret", randbytes )[0..47]
1054 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001055 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001056 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1058 }
1059 else
1060 {
1061 /* The label for the KDF used for key expansion.
1062 * This is either "master secret" or "extended master secret"
1063 * depending on whether the Extended Master Secret extension
1064 * is used. */
1065 char const *lbl = "master secret";
1066
1067 /* The salt for the KDF used for key expansion.
1068 * - If the Extended Master Secret extension is not used,
1069 * this is ClientHello.Random + ServerHello.Random
1070 * (see Sect. 8.1 in RFC 5246).
1071 * - If the Extended Master Secret extension is used,
1072 * this is the transcript of the handshake so far.
1073 * (see Sect. 4 in RFC 7627). */
1074 unsigned char const *salt = handshake->randbytes;
1075 size_t salt_len = 64;
1076
1077#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001081
Hanno Becker35b23c72018-10-23 12:10:41 +01001082 lbl = "extended master secret";
1083 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001084 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1086 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001087 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001088#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001089 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1090 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001091 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001092 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001093 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001094#endif /* MBEDTLS_SHA512_C */
1095 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001096 }
1097 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001098#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001099 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100
Hanno Becker35b23c72018-10-23 12:10:41 +01001101 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001102 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001103#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1104
Hanno Becker7d0a5692018-10-23 15:26:22 +01001105#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1106 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1107 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1108 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1109 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001110 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001111 /* Perform PSK-to-MS expansion in a single step. */
1112 psa_status_t status;
1113 psa_algorithm_t alg;
1114 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001115 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001116
1117 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1118
1119 psk = ssl->conf->psk_opaque;
1120 if( ssl->handshake->psk_opaque != 0 )
1121 psk = ssl->handshake->psk_opaque;
1122
Hanno Becker22bf1452019-04-05 11:21:08 +01001123 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001124 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1125 else
1126 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1127
1128 status = psa_key_derivation( &generator, psk, alg,
1129 salt, salt_len,
1130 (unsigned char const *) lbl,
1131 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001132 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001133 if( status != PSA_SUCCESS )
1134 {
1135 psa_generator_abort( &generator );
1136 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1137 }
1138
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001139 status = psa_generator_read( &generator, session->master,
1140 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 if( status != PSA_SUCCESS )
1142 {
1143 psa_generator_abort( &generator );
1144 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1145 }
1146
1147 status = psa_generator_abort( &generator );
1148 if( status != PSA_SUCCESS )
1149 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001150 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001151 else
1152#endif
1153 {
1154 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1155 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001156 session->master,
1157 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001158 if( ret != 0 )
1159 {
1160 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1161 return( ret );
1162 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001163
Hanno Becker7d0a5692018-10-23 15:26:22 +01001164 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1165 handshake->premaster,
1166 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001167
Hanno Becker7d0a5692018-10-23 15:26:22 +01001168 mbedtls_platform_zeroize( handshake->premaster,
1169 sizeof(handshake->premaster) );
1170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001171 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001172
1173 /*
1174 * Swap the client and server random values.
1175 */
Paul Bakker48916f92012-09-16 19:57:18 +00001176 memcpy( tmp, handshake->randbytes, 64 );
1177 memcpy( handshake->randbytes, tmp + 32, 32 );
1178 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001179 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001180
1181 /*
1182 * SSLv3:
1183 * key block =
1184 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1185 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1186 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1187 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1188 * ...
1189 *
1190 * TLSv1:
1191 * key block = PRF( master, "key expansion", randbytes )
1192 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001193 ret = handshake->tls_prf( session->master, 48, "key expansion",
1194 handshake->randbytes, 64, keyblk, 256 );
1195 if( ret != 0 )
1196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001197 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001198 return( ret );
1199 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001201 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1202 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1203 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1204 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1205 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001206
Paul Bakker5121ce52009-01-03 21:22:43 +00001207 /*
1208 * Determine the appropriate key, IV and MAC length.
1209 */
Paul Bakker68884e32013-01-07 18:20:04 +01001210
Hanno Becker88aaf652017-12-27 08:17:40 +00001211 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001212
Hanno Becker8031d062018-01-03 15:32:31 +00001213#if defined(MBEDTLS_GCM_C) || \
1214 defined(MBEDTLS_CCM_C) || \
1215 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001216 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001217 cipher_info->mode == MBEDTLS_MODE_CCM ||
1218 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001219 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001220 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001221
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001222 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001223 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001224 transform->taglen =
1225 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001226
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001227 /* All modes haves 96-bit IVs;
1228 * GCM and CCM has 4 implicit and 8 explicit bytes
1229 * ChachaPoly has all 12 bytes implicit
1230 */
Paul Bakker68884e32013-01-07 18:20:04 +01001231 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001232 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1233 transform->fixed_ivlen = 12;
1234 else
1235 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001236
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001237 /* Minimum length of encrypted record */
1238 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001239 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001240 }
1241 else
Hanno Becker8031d062018-01-03 15:32:31 +00001242#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1243#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1244 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1245 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001246 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001247 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1249 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001252 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001253 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001254
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001255 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001256 mac_key_len = mbedtls_md_get_size( md_info );
1257 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001260 /*
1261 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1262 * (rfc 6066 page 13 or rfc 2104 section 4),
1263 * so we only need to adjust the length here.
1264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001265 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001267 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001268
1269#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1270 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001271 * HMAC implementation which also truncates the key
1272 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001273 mac_key_len = transform->maclen;
1274#endif
1275 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001277
1278 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001279 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001280
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001281 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001282 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001283 transform->minlen = transform->maclen;
1284 else
Paul Bakker68884e32013-01-07 18:20:04 +01001285 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001286 /*
1287 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001288 * 1. if EtM is in use: one block plus MAC
1289 * otherwise: * first multiple of blocklen greater than maclen
1290 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001292#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1293 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001294 {
1295 transform->minlen = transform->maclen
1296 + cipher_info->block_size;
1297 }
1298 else
1299#endif
1300 {
1301 transform->minlen = transform->maclen
1302 + cipher_info->block_size
1303 - transform->maclen % cipher_info->block_size;
1304 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1307 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1308 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001309 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001310 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001311#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001312#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1313 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1314 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001315 {
1316 transform->minlen += transform->ivlen;
1317 }
1318 else
1319#endif
1320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001321 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001322 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1323 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001324 }
Paul Bakker68884e32013-01-07 18:20:04 +01001325 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001326 }
Hanno Becker8031d062018-01-03 15:32:31 +00001327 else
1328#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1329 {
1330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1332 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
Hanno Becker88aaf652017-12-27 08:17:40 +00001334 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1335 (unsigned) keylen,
1336 (unsigned) transform->minlen,
1337 (unsigned) transform->ivlen,
1338 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001339
1340 /*
1341 * Finally setup the cipher contexts, IVs and MAC secrets.
1342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001343#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001344 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001345 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001346 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001347 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Paul Bakker68884e32013-01-07 18:20:04 +01001349 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001350 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001352 /*
1353 * This is not used in TLS v1.1.
1354 */
Paul Bakker48916f92012-09-16 19:57:18 +00001355 iv_copy_len = ( transform->fixed_ivlen ) ?
1356 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001357 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1358 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001359 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001360 }
1361 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001362#endif /* MBEDTLS_SSL_CLI_C */
1363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001364 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001365 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001366 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001367 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Hanno Becker81c7b182017-11-09 18:39:33 +00001369 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001370 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001371
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001372 /*
1373 * This is not used in TLS v1.1.
1374 */
Paul Bakker48916f92012-09-16 19:57:18 +00001375 iv_copy_len = ( transform->fixed_ivlen ) ?
1376 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001377 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1378 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001379 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001380 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001381 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001382#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001383 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001385 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1386 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001387 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001388
Hanno Beckerd56ed242018-01-03 15:32:51 +00001389#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001390#if defined(MBEDTLS_SSL_PROTO_SSL3)
1391 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001392 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001393 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001394 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001395 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001396 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1397 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001398 }
1399
Hanno Becker81c7b182017-11-09 18:39:33 +00001400 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1401 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001402 }
1403 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001404#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1405#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1406 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1407 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001408 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001409 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1410 For AEAD-based ciphersuites, there is nothing to do here. */
1411 if( mac_key_len != 0 )
1412 {
1413 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1414 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1415 }
Paul Bakker68884e32013-01-07 18:20:04 +01001416 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001417 else
1418#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001421 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1422 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001423 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001424#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001426#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1427 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001428 {
1429 int ret = 0;
1430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001431 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001432
Hanno Becker88aaf652017-12-27 08:17:40 +00001433 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001434 transform->iv_enc, transform->iv_dec,
1435 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001436 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001437 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001439 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001440 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1441 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001442 }
1443 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001444#else
1445 ((void) mac_dec);
1446 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001447#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001448
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001449#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1450 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001451 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001452 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1453 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001454 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001455 iv_copy_len );
1456 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001457
1458 if( ssl->conf->f_export_keys_ext != NULL )
1459 {
1460 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1461 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001462 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001463 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001464 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001465 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001466 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001467 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001468#endif
1469
Hanno Beckerf704bef2018-11-16 15:21:18 +00001470#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001471
1472 /* Only use PSA-based ciphers for TLS-1.2.
1473 * That's relevant at least for TLS-1.0, where
1474 * we assume that mbedtls_cipher_crypt() updates
1475 * the structure field for the IV, which the PSA-based
1476 * implementation currently doesn't. */
1477#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1478 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001479 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001481 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001482 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1483 {
1484 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001485 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001486 }
1487
1488 if( ret == 0 )
1489 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001491 psa_fallthrough = 0;
1492 }
1493 else
1494 {
1495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1496 psa_fallthrough = 1;
1497 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 psa_fallthrough = 1;
1501#else
1502 psa_fallthrough = 1;
1503#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001504
Hanno Beckercb1cc802018-11-17 22:27:38 +00001505 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001506#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001507 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001508 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001509 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001510 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001511 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001512 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001513
Hanno Beckerf704bef2018-11-16 15:21:18 +00001514#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001515 /* Only use PSA-based ciphers for TLS-1.2.
1516 * That's relevant at least for TLS-1.0, where
1517 * we assume that mbedtls_cipher_crypt() updates
1518 * the structure field for the IV, which the PSA-based
1519 * implementation currently doesn't. */
1520#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1521 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001522 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001523 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001524 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001525 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1526 {
1527 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001528 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001529 }
1530
1531 if( ret == 0 )
1532 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001533 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001534 psa_fallthrough = 0;
1535 }
1536 else
1537 {
1538 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1539 psa_fallthrough = 1;
1540 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001542 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001543 psa_fallthrough = 1;
1544#else
1545 psa_fallthrough = 1;
1546#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001547
Hanno Beckercb1cc802018-11-17 22:27:38 +00001548 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001549#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001550 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001551 cipher_info ) ) != 0 )
1552 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001553 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001554 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001555 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001557 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001558 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001562 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001563 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001566 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001569 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001570 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001571 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573#if defined(MBEDTLS_CIPHER_MODE_CBC)
1574 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1577 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001578 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001580 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001581 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1584 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001587 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001588 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001589 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Paul Bakker5121ce52009-01-03 21:22:43 +00001592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594 // Initialize compression
1595 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001597 {
Paul Bakker16770332013-10-11 09:59:44 +02001598 if( ssl->compress_buf == NULL )
1599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001601 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001602 if( ssl->compress_buf == NULL )
1603 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001604 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001605 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001606 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1607 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001608 }
1609 }
1610
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001611 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612
Paul Bakker48916f92012-09-16 19:57:18 +00001613 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1614 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001615
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001616 if( deflateInit( &transform->ctx_deflate,
1617 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001618 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001621 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1622 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001623 }
1624 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001627 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001628end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001629 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1630 mbedtls_platform_zeroize( handshake->randbytes,
1631 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001632 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001633}
1634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001635#if defined(MBEDTLS_SSL_PROTO_SSL3)
1636void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001637{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001638 mbedtls_md5_context md5;
1639 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001640 unsigned char pad_1[48];
1641 unsigned char pad_2[48];
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001645 mbedtls_md5_init( &md5 );
1646 mbedtls_sha1_init( &sha1 );
1647
1648 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1649 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Paul Bakker380da532012-04-18 16:10:25 +00001651 memset( pad_1, 0x36, 48 );
1652 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001654 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1655 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1656 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001658 mbedtls_md5_starts_ret( &md5 );
1659 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1660 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1661 mbedtls_md5_update_ret( &md5, hash, 16 );
1662 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001663
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001664 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1665 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1666 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001667
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001668 mbedtls_sha1_starts_ret( &sha1 );
1669 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1670 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1671 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1672 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001674 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1675 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001676
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001677 mbedtls_md5_free( &md5 );
1678 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001679
Paul Bakker380da532012-04-18 16:10:25 +00001680 return;
1681}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001682#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1685void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001686{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001687 mbedtls_md5_context md5;
1688 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001691
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001692 mbedtls_md5_init( &md5 );
1693 mbedtls_sha1_init( &sha1 );
1694
1695 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1696 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001697
Andrzej Kurekeb342242019-01-29 09:14:33 -05001698 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001699 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001701 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1702 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001703
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001704 mbedtls_md5_free( &md5 );
1705 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001706
Paul Bakker380da532012-04-18 16:10:25 +00001707 return;
1708}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001709#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001711#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1712#if defined(MBEDTLS_SHA256_C)
1713void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001714{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001715#if defined(MBEDTLS_USE_PSA_CRYPTO)
1716 size_t hash_size;
1717 psa_status_t status;
1718 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1719
1720 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1721 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1722 if( status != PSA_SUCCESS )
1723 {
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1725 return;
1726 }
1727
1728 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1729 if( status != PSA_SUCCESS )
1730 {
1731 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1732 return;
1733 }
1734 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1736#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001737 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001738
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001739 mbedtls_sha256_init( &sha256 );
1740
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001742
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001743 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001744 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001746 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1747 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001748
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001749 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001750#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001751 return;
1752}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001753#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001754
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#if defined(MBEDTLS_SHA512_C)
1756void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001757{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001758#if defined(MBEDTLS_USE_PSA_CRYPTO)
1759 size_t hash_size;
1760 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001761 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001762
1763 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001764 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001765 if( status != PSA_SUCCESS )
1766 {
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1768 return;
1769 }
1770
Andrzej Kurek972fba52019-01-30 03:29:12 -05001771 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001772 if( status != PSA_SUCCESS )
1773 {
1774 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1775 return;
1776 }
1777 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1779#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001780 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001781
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001782 mbedtls_sha512_init( &sha512 );
1783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001785
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001786 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001787 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001789 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1790 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001791
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001792 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001793#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001794 return;
1795}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#endif /* MBEDTLS_SHA512_C */
1797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001799#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1800int mbedtls_ssl_psk_derive_premaster( mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001801{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001802 unsigned char *p = ssl->handshake->premaster;
1803 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001804 const unsigned char *psk = ssl->conf->psk;
1805 size_t psk_len = ssl->conf->psk_len;
1806
1807 /* If the psk callback was called, use its result */
1808 if( ssl->handshake->psk != NULL )
1809 {
1810 psk = ssl->handshake->psk;
1811 psk_len = ssl->handshake->psk_len;
1812 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001813
1814 /*
1815 * PMS = struct {
1816 * opaque other_secret<0..2^16-1>;
1817 * opaque psk<0..2^16-1>;
1818 * };
1819 * with "other_secret" depending on the particular key exchange
1820 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001821#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1822 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001823 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001824 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001826
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001827 *(p++) = (unsigned char)( psk_len >> 8 );
1828 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001829
1830 if( end < p || (size_t)( end - p ) < psk_len )
1831 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1832
1833 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001834 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001835 }
1836 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001837#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1838#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1839 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001840 {
1841 /*
1842 * other_secret already set by the ClientKeyExchange message,
1843 * and is 48 bytes long
1844 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001845 if( end - p < 2 )
1846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1847
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001848 *p++ = 0;
1849 *p++ = 48;
1850 p += 48;
1851 }
1852 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001853#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1854#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1855 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001856 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001857 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001858 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001859
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001860 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001861 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001862 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001863 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001865 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001866 return( ret );
1867 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001868 *(p++) = (unsigned char)( len >> 8 );
1869 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 p += len;
1871
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 }
1874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1876#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1877 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001878 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001879 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001880 size_t zlen;
1881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001882 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001883 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001884 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001886 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001887 return( ret );
1888 }
1889
1890 *(p++) = (unsigned char)( zlen >> 8 );
1891 *(p++) = (unsigned char)( zlen );
1892 p += zlen;
1893
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001894 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1895 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896 }
1897 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1901 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001902 }
1903
1904 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001905 if( end - p < 2 )
1906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001907
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001908 *(p++) = (unsigned char)( psk_len >> 8 );
1909 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001910
1911 if( end < p || (size_t)( end - p ) < psk_len )
1912 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1913
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001914 memcpy( p, psk, psk_len );
1915 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001916
1917 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1918
1919 return( 0 );
1920}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001921#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001923#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001924/*
1925 * SSLv3.0 MAC functions
1926 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001927#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001928static void ssl_mac( mbedtls_md_context_t *md_ctx,
1929 const unsigned char *secret,
1930 const unsigned char *buf, size_t len,
1931 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001932 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001933{
1934 unsigned char header[11];
1935 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001936 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1938 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001939
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001940 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001941 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001942 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001943 else
Paul Bakker68884e32013-01-07 18:20:04 +01001944 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001945
1946 memcpy( header, ctr, 8 );
1947 header[ 8] = (unsigned char) type;
1948 header[ 9] = (unsigned char)( len >> 8 );
1949 header[10] = (unsigned char)( len );
1950
Paul Bakker68884e32013-01-07 18:20:04 +01001951 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001952 mbedtls_md_starts( md_ctx );
1953 mbedtls_md_update( md_ctx, secret, md_size );
1954 mbedtls_md_update( md_ctx, padding, padlen );
1955 mbedtls_md_update( md_ctx, header, 11 );
1956 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001957 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001958
Paul Bakker68884e32013-01-07 18:20:04 +01001959 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960 mbedtls_md_starts( md_ctx );
1961 mbedtls_md_update( md_ctx, secret, md_size );
1962 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001963 mbedtls_md_update( md_ctx, out, md_size );
1964 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001965}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001966#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001967
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001968/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001969 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001970#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001971 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1972 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1973 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1974/* This function makes sure every byte in the memory region is accessed
1975 * (in ascending addresses order) */
1976static void ssl_read_memory( unsigned char *p, size_t len )
1977{
1978 unsigned char acc = 0;
1979 volatile unsigned char force;
1980
1981 for( ; len != 0; p++, len-- )
1982 acc ^= *p;
1983
1984 force = acc;
1985 (void) force;
1986}
1987#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1988
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001989/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001990 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001991 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001992
Hanno Beckera0e20d02019-05-15 14:03:01 +01001993#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001994/* This functions transforms a DTLS plaintext fragment and a record content
1995 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001996 *
1997 * struct {
1998 * opaque content[DTLSPlaintext.length];
1999 * ContentType real_type;
2000 * uint8 zeros[length_of_padding];
2001 * } DTLSInnerPlaintext;
2002 *
2003 * Input:
2004 * - `content`: The beginning of the buffer holding the
2005 * plaintext to be wrapped.
2006 * - `*content_size`: The length of the plaintext in Bytes.
2007 * - `max_len`: The number of Bytes available starting from
2008 * `content`. This must be `>= *content_size`.
2009 * - `rec_type`: The desired record content type.
2010 *
2011 * Output:
2012 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2013 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2014 *
2015 * Returns:
2016 * - `0` on success.
2017 * - A negative error code if `max_len` didn't offer enough space
2018 * for the expansion.
2019 */
2020static int ssl_cid_build_inner_plaintext( unsigned char *content,
2021 size_t *content_size,
2022 size_t remaining,
2023 uint8_t rec_type )
2024{
2025 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002026 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2027 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2028 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002029
2030 /* Write real content type */
2031 if( remaining == 0 )
2032 return( -1 );
2033 content[ len ] = rec_type;
2034 len++;
2035 remaining--;
2036
2037 if( remaining < pad )
2038 return( -1 );
2039 memset( content + len, 0, pad );
2040 len += pad;
2041 remaining -= pad;
2042
2043 *content_size = len;
2044 return( 0 );
2045}
2046
Hanno Becker07dc97d2019-05-20 15:08:01 +01002047/* This function parses a DTLSInnerPlaintext structure.
2048 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002049static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2050 size_t *content_size,
2051 uint8_t *rec_type )
2052{
2053 size_t remaining = *content_size;
2054
2055 /* Determine length of padding by skipping zeroes from the back. */
2056 do
2057 {
2058 if( remaining == 0 )
2059 return( -1 );
2060 remaining--;
2061 } while( content[ remaining ] == 0 );
2062
2063 *content_size = remaining;
2064 *rec_type = content[ remaining ];
2065
2066 return( 0 );
2067}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002068#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002069
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002070/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002071 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002072static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002073 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002074 mbedtls_record *rec )
2075{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002076 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002077 *
2078 * additional_data = seq_num + TLSCompressed.type +
2079 * TLSCompressed.version + TLSCompressed.length;
2080 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002081 * For the CID extension, this is extended as follows
2082 * (quoting draft-ietf-tls-dtls-connection-id-05,
2083 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002084 *
2085 * additional_data = seq_num + DTLSPlaintext.type +
2086 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002087 * cid +
2088 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002089 * length_of_DTLSInnerPlaintext;
2090 */
2091
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002092 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2093 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002094 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002095
Hanno Beckera0e20d02019-05-15 14:03:01 +01002096#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002097 if( rec->cid_len != 0 )
2098 {
2099 memcpy( add_data + 11, rec->cid, rec->cid_len );
2100 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2101 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2102 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2103 *add_data_len = 13 + 1 + rec->cid_len;
2104 }
2105 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002106#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002107 {
2108 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2109 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2110 *add_data_len = 13;
2111 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002112}
2113
Hanno Beckera18d1322018-01-03 14:27:32 +00002114int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2115 mbedtls_ssl_transform *transform,
2116 mbedtls_record *rec,
2117 int (*f_rng)(void *, unsigned char *, size_t),
2118 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002119{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002121 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002123 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002124 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002125 size_t post_avail;
2126
2127 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002128#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002129 ((void) ssl);
2130#endif
2131
2132 /* The PRNG is used for dynamic IV generation that's used
2133 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2134#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2135 ( defined(MBEDTLS_AES_C) || \
2136 defined(MBEDTLS_ARIA_C) || \
2137 defined(MBEDTLS_CAMELLIA_C) ) && \
2138 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2139 ((void) f_rng);
2140 ((void) p_rng);
2141#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002143 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002144
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002145 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002146 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002147 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2148 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2149 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002150 if( rec == NULL
2151 || rec->buf == NULL
2152 || rec->buf_len < rec->data_offset
2153 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002154#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002155 || rec->cid_len != 0
2156#endif
2157 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002158 {
2159 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002160 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002161 }
2162
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002164 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002165 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002166 data, rec->data_len );
2167
2168 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2169
2170 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2171 {
2172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2173 (unsigned) rec->data_len,
2174 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2175 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2176 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002177
Hanno Beckera0e20d02019-05-15 14:03:01 +01002178#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002179 /*
2180 * Add CID information
2181 */
2182 rec->cid_len = transform->out_cid_len;
2183 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2184 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002185
2186 if( rec->cid_len != 0 )
2187 {
2188 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002189 * Wrap plaintext into DTLSInnerPlaintext structure.
2190 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002191 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002192 * Note that this changes `rec->data_len`, and hence
2193 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002194 */
2195 if( ssl_cid_build_inner_plaintext( data,
2196 &rec->data_len,
2197 post_avail,
2198 rec->type ) != 0 )
2199 {
2200 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2201 }
2202
2203 rec->type = MBEDTLS_SSL_MSG_CID;
2204 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002205#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002206
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002207 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2208
Paul Bakker5121ce52009-01-03 21:22:43 +00002209 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002210 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002211 */
Hanno Becker52344c22018-01-03 15:24:20 +00002212#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002213 if( mode == MBEDTLS_MODE_STREAM ||
2214 ( mode == MBEDTLS_MODE_CBC
2215#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002217#endif
2218 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002219 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002220 if( post_avail < transform->maclen )
2221 {
2222 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2223 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2224 }
2225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002226#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002228 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002229 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2231 data, rec->data_len, rec->ctr, rec->type, mac );
2232 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 }
2234 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002236#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2237 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002238 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002239 {
Hanno Becker992b6872017-11-09 18:57:39 +00002240 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2241
Hanno Beckercab87e62019-04-29 13:52:53 +01002242 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002243
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002244 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002245 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2247 data, rec->data_len );
2248 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2249 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2250
2251 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 }
2253 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002254#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002255 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002256 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2257 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002258 }
2259
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2261 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002262
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002263 rec->data_len += transform->maclen;
2264 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002265 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002266 }
Hanno Becker52344c22018-01-03 15:24:20 +00002267#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002268
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002269 /*
2270 * Encrypt
2271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2273 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002274 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002275 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002278 "including %d bytes of padding",
2279 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002280
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002281 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2282 transform->iv_enc, transform->ivlen,
2283 data, rec->data_len,
2284 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002287 return( ret );
2288 }
2289
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002290 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002292 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2293 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002294 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002295 }
Paul Bakker68884e32013-01-07 18:20:04 +01002296 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002297#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002298
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002299#if defined(MBEDTLS_GCM_C) || \
2300 defined(MBEDTLS_CCM_C) || \
2301 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002302 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002303 mode == MBEDTLS_MODE_CCM ||
2304 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002305 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002306 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002307 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002308 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002309
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002310 /* Check that there's space for both the authentication tag
2311 * and the explicit IV before and after the record content. */
2312 if( post_avail < transform->taglen ||
2313 rec->data_offset < explicit_iv_len )
2314 {
2315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2316 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2317 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002318
Paul Bakker68884e32013-01-07 18:20:04 +01002319 /*
2320 * Generate IV
2321 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2323 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002324 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002325 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002326 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2327 explicit_iv_len );
2328 /* Prefix record content with explicit IV. */
2329 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002330 }
2331 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2332 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002333 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 unsigned char i;
2335
2336 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2337
2338 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002340 }
2341 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002342 {
2343 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002344 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2345 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002346 }
2347
Hanno Beckercab87e62019-04-29 13:52:53 +01002348 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002349
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2351 iv, transform->ivlen );
2352 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353 data - explicit_iv_len, explicit_iv_len );
2354 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002355 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002356 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002357 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002358 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002359
Paul Bakker68884e32013-01-07 18:20:04 +01002360 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002361 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002362 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002363
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002364 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002365 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002366 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002367 data, rec->data_len, /* source */
2368 data, &rec->data_len, /* destination */
2369 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002371 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002372 return( ret );
2373 }
2374
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2376 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002377
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002378 rec->data_len += transform->taglen + explicit_iv_len;
2379 rec->data_offset -= explicit_iv_len;
2380 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002381 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002382 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2385#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002386 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002387 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002388 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002389 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 size_t padlen, i;
2391 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002392
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002393 /* Currently we're always using minimal padding
2394 * (up to 255 bytes would be allowed). */
2395 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2396 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002397 padlen = 0;
2398
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002399 /* Check there's enough space in the buffer for the padding. */
2400 if( post_avail < padlen + 1 )
2401 {
2402 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2403 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2404 }
2405
Paul Bakker5121ce52009-01-03 21:22:43 +00002406 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002407 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002408
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002409 rec->data_len += padlen + 1;
2410 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002412#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002414 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2415 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002416 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002417 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002418 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002419 if( f_rng == NULL )
2420 {
2421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2422 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2423 }
2424
2425 if( rec->data_offset < transform->ivlen )
2426 {
2427 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2428 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2429 }
2430
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 /*
2432 * Generate IV
2433 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002434 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002435 if( ret != 0 )
2436 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002437
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002438 memcpy( data - transform->ivlen, transform->iv_enc,
2439 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002440
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002442#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002444 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002445 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002447 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002448
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002449 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2450 transform->iv_enc,
2451 transform->ivlen,
2452 data, rec->data_len,
2453 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002456 return( ret );
2457 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002458
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002459 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002461 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2462 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002463 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002464
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002465#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002466 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002467 {
2468 /*
2469 * Save IV in SSL3 and TLS1
2470 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002471 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2472 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002473 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002474 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002475#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002476 {
2477 data -= transform->ivlen;
2478 rec->data_offset -= transform->ivlen;
2479 rec->data_len += transform->ivlen;
2480 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002482#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002483 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002484 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002485 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2486
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487 /*
2488 * MAC(MAC_write_key, seq_num +
2489 * TLSCipherText.type +
2490 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002491 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002492 * IV + // except for TLS 1.0
2493 * ENC(content + padding + padding_length));
2494 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002495
2496 if( post_avail < transform->maclen)
2497 {
2498 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2499 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2500 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002501
Hanno Beckercab87e62019-04-29 13:52:53 +01002502 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002506 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002507
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002508 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002509 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002510 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2511 data, rec->data_len );
2512 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2513 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002515 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002516
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002517 rec->data_len += transform->maclen;
2518 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002519 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002520 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002522 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002523 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002525 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2528 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002529 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002530
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002531 /* Make extra sure authentication was performed, exactly once */
2532 if( auth_done != 1 )
2533 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2535 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002536 }
2537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002538 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002539
2540 return( 0 );
2541}
2542
Hanno Beckera18d1322018-01-03 14:27:32 +00002543int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2544 mbedtls_ssl_transform *transform,
2545 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002546{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002547 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002548 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002549 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002550#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002551 size_t padlen = 0, correct = 1;
2552#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002554 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002555 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002556
Hanno Beckera18d1322018-01-03 14:27:32 +00002557#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002558 ((void) ssl);
2559#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002563 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002564 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2565 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2566 }
2567 if( rec == NULL ||
2568 rec->buf == NULL ||
2569 rec->buf_len < rec->data_offset ||
2570 rec->buf_len - rec->data_offset < rec->data_len )
2571 {
2572 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002573 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002574 }
2575
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002576 data = rec->buf + rec->data_offset;
2577 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002578
Hanno Beckera0e20d02019-05-15 14:03:01 +01002579#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002580 /*
2581 * Match record's CID with incoming CID.
2582 */
Hanno Becker938489a2019-05-08 13:02:22 +01002583 if( rec->cid_len != transform->in_cid_len ||
2584 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2585 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002586 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002587 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002588#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002590#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2591 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002592 {
2593 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002594 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2595 transform->iv_dec,
2596 transform->ivlen,
2597 data, rec->data_len,
2598 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002601 return( ret );
2602 }
2603
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002604 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002606 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2607 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002608 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002609 }
Paul Bakker68884e32013-01-07 18:20:04 +01002610 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002611#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612#if defined(MBEDTLS_GCM_C) || \
2613 defined(MBEDTLS_CCM_C) || \
2614 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002615 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002616 mode == MBEDTLS_MODE_CCM ||
2617 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002618 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002620 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002621
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002622 /*
2623 * Compute and update sizes
2624 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002628 "+ taglen (%d)", rec->data_len,
2629 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002630 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002631 }
Paul Bakker68884e32013-01-07 18:20:04 +01002632
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 /*
2634 * Prepare IV
2635 */
2636 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2637 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002638 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002640 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002641
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002642 }
2643 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2644 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002645 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002646 unsigned char i;
2647
2648 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2649
2650 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002651 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002652 }
2653 else
2654 {
2655 /* Reminder if we ever add an AEAD mode with a different size */
2656 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2657 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2658 }
2659
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660 data += explicit_iv_len;
2661 rec->data_offset += explicit_iv_len;
2662 rec->data_len -= explicit_iv_len + transform->taglen;
2663
Hanno Beckercab87e62019-04-29 13:52:53 +01002664 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002666 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002667
2668 memcpy( transform->iv_dec + transform->fixed_ivlen,
2669 data - explicit_iv_len, explicit_iv_len );
2670
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002671 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002673 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002674
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002675
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002676 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002677 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002678 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002679 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2680 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002681 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002682 data, rec->data_len,
2683 data, &olen,
2684 data + rec->data_len,
2685 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002689 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2690 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002691
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002692 return( ret );
2693 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002694 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002695
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002696 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002697 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002698 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2699 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002700 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002701 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002702 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2704#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002705 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002706 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002708 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002709
Paul Bakker5121ce52009-01-03 21:22:43 +00002710 /*
Paul Bakker45829992013-01-03 14:52:21 +01002711 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002712 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002713#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002714 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2715 {
2716 /* The ciphertext is prefixed with the CBC IV. */
2717 minlen += transform->ivlen;
2718 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002719#endif
Paul Bakker45829992013-01-03 14:52:21 +01002720
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002721 /* Size considerations:
2722 *
2723 * - The CBC cipher text must not be empty and hence
2724 * at least of size transform->ivlen.
2725 *
2726 * Together with the potential IV-prefix, this explains
2727 * the first of the two checks below.
2728 *
2729 * - The record must contain a MAC, either in plain or
2730 * encrypted, depending on whether Encrypt-then-MAC
2731 * is used or not.
2732 * - If it is, the message contains the IV-prefix,
2733 * the CBC ciphertext, and the MAC.
2734 * - If it is not, the padded plaintext, and hence
2735 * the CBC ciphertext, has at least length maclen + 1
2736 * because there is at least the padding length byte.
2737 *
2738 * As the CBC ciphertext is not empty, both cases give the
2739 * lower bound minlen + maclen + 1 on the record size, which
2740 * we test for in the second check below.
2741 */
2742 if( rec->data_len < minlen + transform->ivlen ||
2743 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002745 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002746 "+ 1 ) ( + expl IV )", rec->data_len,
2747 transform->ivlen,
2748 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002750 }
2751
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002752 /*
2753 * Authenticate before decrypt if enabled
2754 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002756 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002757 {
Hanno Becker992b6872017-11-09 18:57:39 +00002758 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002759
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002761
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002762 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2763 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002764
Hanno Beckercab87e62019-04-29 13:52:53 +01002765 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002766
Hanno Beckercab87e62019-04-29 13:52:53 +01002767 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2768 add_data_len );
2769 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2770 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002771 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2772 data, rec->data_len );
2773 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2774 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002775
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2777 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002778 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002779 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002781 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2782 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002785 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002786 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002787 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002788 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002789#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002790
2791 /*
2792 * Check length sanity
2793 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002795 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002797 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002799 }
2800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002801#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002802 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002803 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002804 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002805 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002806 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002807 /* This is safe because data_len >= minlen + maclen + 1 initially,
2808 * and at this point we have at most subtracted maclen (note that
2809 * minlen == transform->ivlen here). */
2810 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002812 data += transform->ivlen;
2813 rec->data_offset += transform->ivlen;
2814 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002815 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002817
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002818 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2819 transform->iv_dec, transform->ivlen,
2820 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002823 return( ret );
2824 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002825
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002826 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002830 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002832#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002833 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002834 {
2835 /*
2836 * Save IV in SSL3 and TLS1
2837 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002838 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2839 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002840 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002841#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002843 /* Safe since data_len >= minlen + maclen + 1, so after having
2844 * subtracted at most minlen and maclen up to this point,
2845 * data_len > 0. */
2846 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002847
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002848 if( auth_done == 1 )
2849 {
2850 correct *= ( rec->data_len >= padlen + 1 );
2851 padlen *= ( rec->data_len >= padlen + 1 );
2852 }
2853 else
Paul Bakker45829992013-01-03 14:52:21 +01002854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002855#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002856 if( rec->data_len < transform->maclen + padlen + 1 )
2857 {
2858 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2859 rec->data_len,
2860 transform->maclen,
2861 padlen + 1 ) );
2862 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002863#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002864
2865 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2866 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002867 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002868
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 padlen++;
2870
2871 /* Regardless of the validity of the padding,
2872 * we have data_len >= padlen here. */
2873
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002874#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002876 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002877 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002878 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002879#if defined(MBEDTLS_SSL_DEBUG_ALL)
2880 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002881 "should be no more than %d",
2882 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002883#endif
Paul Bakker45829992013-01-03 14:52:21 +01002884 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002885 }
2886 }
2887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2889#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2890 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002891 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002892 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002893 /* The padding check involves a series of up to 256
2894 * consecutive memory reads at the end of the record
2895 * plaintext buffer. In order to hide the length and
2896 * validity of the padding, always perform exactly
2897 * `min(256,plaintext_len)` reads (but take into account
2898 * only the last `padlen` bytes for the padding check). */
2899 size_t pad_count = 0;
2900 size_t real_count = 0;
2901 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002902
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002903 /* Index of first padding byte; it has been ensured above
2904 * that the subtraction is safe. */
2905 size_t const padding_idx = rec->data_len - padlen;
2906 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2907 size_t const start_idx = rec->data_len - num_checks;
2908 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002909
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002910 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002911 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002912 real_count |= ( idx >= padding_idx );
2913 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002914 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002915 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002918 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002920#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002921 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002922 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2925 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002929 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002930
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002931 /* If the padding was found to be invalid, padlen == 0
2932 * and the subtraction is safe. If the padding was found valid,
2933 * padlen hasn't been changed and the previous assertion
2934 * data_len >= padlen still holds. */
2935 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002936 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002937 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002939 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002940 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002941 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2942 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002943 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002944
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002945#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002946 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002947 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002948#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002949
2950 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002951 * Authenticate if not done yet.
2952 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002953 */
Hanno Becker52344c22018-01-03 15:24:20 +00002954#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002955 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002956 {
Hanno Becker992b6872017-11-09 18:57:39 +00002957 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002958
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002959 /* If the initial value of padlen was such that
2960 * data_len < maclen + padlen + 1, then padlen
2961 * got reset to 1, and the initial check
2962 * data_len >= minlen + maclen + 1
2963 * guarantees that at this point we still
2964 * have at least data_len >= maclen.
2965 *
2966 * If the initial value of padlen was such that
2967 * data_len >= maclen + padlen + 1, then we have
2968 * subtracted either padlen + 1 (if the padding was correct)
2969 * or 0 (if the padding was incorrect) since then,
2970 * hence data_len >= maclen in any case.
2971 */
2972 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Hanno Beckercab87e62019-04-29 13:52:53 +01002974 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002975
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002976#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002977 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002978 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002979 ssl_mac( &transform->md_ctx_dec,
2980 transform->mac_dec,
2981 data, rec->data_len,
2982 rec->ctr, rec->type,
2983 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002984 }
2985 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002986#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2987#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2988 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002989 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002990 {
2991 /*
2992 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002993 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002994 *
2995 * Known timing attacks:
2996 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2997 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002998 * To compensate for different timings for the MAC calculation
2999 * depending on how much padding was removed (which is determined
3000 * by padlen), process extra_run more blocks through the hash
3001 * function.
3002 *
3003 * The formula in the paper is
3004 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3005 * where L1 is the size of the header plus the decrypted message
3006 * plus CBC padding and L2 is the size of the header plus the
3007 * decrypted message. This is for an underlying hash function
3008 * with 64-byte blocks.
3009 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3010 * correctly. We round down instead of up, so -56 is the correct
3011 * value for our calculations instead of -55.
3012 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003013 * Repeat the formula rather than defining a block_size variable.
3014 * This avoids requiring division by a variable at runtime
3015 * (which would be marginally less efficient and would require
3016 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003017 */
3018 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003019 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003020
3021 /*
3022 * The next two sizes are the minimum and maximum values of
3023 * in_msglen over all padlen values.
3024 *
3025 * They're independent of padlen, since we previously did
3026 * in_msglen -= padlen.
3027 *
3028 * Note that max_len + maclen is never more than the buffer
3029 * length, as we previously did in_msglen -= maclen too.
3030 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003031 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003032 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3033
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003034 memset( tmp, 0, sizeof( tmp ) );
3035
3036 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003038#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3039 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 case MBEDTLS_MD_MD5:
3041 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003043 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003044 extra_run =
3045 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3046 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 break;
3048#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003049#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003050 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003051 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003052 extra_run =
3053 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3054 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003055 break;
3056#endif
3057 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003058 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003059 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3060 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003061
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003062 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003063
Hanno Beckercab87e62019-04-29 13:52:53 +01003064 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3065 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003066 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3067 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003068 /* Make sure we access everything even when padlen > 0. This
3069 * makes the synchronisation requirements for just-in-time
3070 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 ssl_read_memory( data + rec->data_len, padlen );
3072 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003073
3074 /* Call mbedtls_md_process at least once due to cache attacks
3075 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003076 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003077 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003078
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003080
3081 /* Make sure we access all the memory that could contain the MAC,
3082 * before we check it in the next code block. This makes the
3083 * synchronisation requirements for just-in-time Prime+Probe
3084 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003085 ssl_read_memory( data + min_len,
3086 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003087 }
3088 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3090 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3093 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003094 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003096#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003097 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3098 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003099#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003100
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003101 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3102 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003104#if defined(MBEDTLS_SSL_DEBUG_ALL)
3105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003106#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003107 correct = 0;
3108 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003109 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003110 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003111
3112 /*
3113 * Finally check the correct flag
3114 */
3115 if( correct == 0 )
3116 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003117#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003118
3119 /* Make extra sure authentication was performed, exactly once */
3120 if( auth_done != 1 )
3121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003122 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3123 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003124 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003125
Hanno Beckera0e20d02019-05-15 14:03:01 +01003126#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003127 if( rec->cid_len != 0 )
3128 {
3129 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3130 &rec->type );
3131 if( ret != 0 )
3132 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3133 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003134#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003136 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003137
3138 return( 0 );
3139}
3140
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003141#undef MAC_NONE
3142#undef MAC_PLAINTEXT
3143#undef MAC_CIPHERTEXT
3144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003145#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003146/*
3147 * Compression/decompression functions
3148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003149static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150{
3151 int ret;
3152 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003153 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003154 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003155 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003158
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003159 if( len_pre == 0 )
3160 return( 0 );
3161
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162 memcpy( msg_pre, ssl->out_msg, len_pre );
3163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003165 ssl->out_msglen ) );
3166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168 ssl->out_msg, ssl->out_msglen );
3169
Paul Bakker48916f92012-09-16 19:57:18 +00003170 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3171 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3172 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003173 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174
Paul Bakker48916f92012-09-16 19:57:18 +00003175 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003176 if( ret != Z_OK )
3177 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3179 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003180 }
3181
Angus Grattond8213d02016-05-25 20:56:48 +10003182 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003183 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186 ssl->out_msglen ) );
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189 ssl->out_msg, ssl->out_msglen );
3190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003191 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003192
3193 return( 0 );
3194}
3195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003196static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197{
3198 int ret;
3199 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003200 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003201 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003202 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003204 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003205
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003206 if( len_pre == 0 )
3207 return( 0 );
3208
Paul Bakker2770fbd2012-07-03 13:30:23 +00003209 memcpy( msg_pre, ssl->in_msg, len_pre );
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212 ssl->in_msglen ) );
3213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003214 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003215 ssl->in_msg, ssl->in_msglen );
3216
Paul Bakker48916f92012-09-16 19:57:18 +00003217 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3218 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3219 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003220 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003221 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222
Paul Bakker48916f92012-09-16 19:57:18 +00003223 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003224 if( ret != Z_OK )
3225 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3227 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228 }
3229
Angus Grattond8213d02016-05-25 20:56:48 +10003230 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003231 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003234 ssl->in_msglen ) );
3235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237 ssl->in_msg, ssl->in_msglen );
3238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003240
3241 return( 0 );
3242}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003243#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3246static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003248#if defined(MBEDTLS_SSL_PROTO_DTLS)
3249static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003250{
3251 /* If renegotiation is not enforced, retransmit until we would reach max
3252 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003253 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003254 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003255 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003256 unsigned char doublings = 1;
3257
3258 while( ratio != 0 )
3259 {
3260 ++doublings;
3261 ratio >>= 1;
3262 }
3263
3264 if( ++ssl->renego_records_seen > doublings )
3265 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003267 return( 0 );
3268 }
3269 }
3270
3271 return( ssl_write_hello_request( ssl ) );
3272}
3273#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003274#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003275
Paul Bakker5121ce52009-01-03 21:22:43 +00003276/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003277 * Fill the input message buffer by appending data to it.
3278 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003279 *
3280 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3281 * available (from this read and/or a previous one). Otherwise, an error code
3282 * is returned (possibly EOF or WANT_READ).
3283 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003284 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3285 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3286 * since we always read a whole datagram at once.
3287 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003288 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003289 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003291int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003292{
Paul Bakker23986e52011-04-24 08:57:21 +00003293 int ret;
3294 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003297
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003298 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3299 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003300 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003301 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003303 }
3304
Angus Grattond8213d02016-05-25 20:56:48 +10003305 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3308 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003309 }
3310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003311#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003312 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003313 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003314 uint32_t timeout;
3315
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003316 /* Just to be sure */
3317 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3318 {
3319 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3320 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3321 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3322 }
3323
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003324 /*
3325 * The point is, we need to always read a full datagram at once, so we
3326 * sometimes read more then requested, and handle the additional data.
3327 * It could be the rest of the current record (while fetching the
3328 * header) and/or some other records in the same datagram.
3329 */
3330
3331 /*
3332 * Move to the next record in the already read datagram if applicable
3333 */
3334 if( ssl->next_record_offset != 0 )
3335 {
3336 if( ssl->in_left < ssl->next_record_offset )
3337 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003340 }
3341
3342 ssl->in_left -= ssl->next_record_offset;
3343
3344 if( ssl->in_left != 0 )
3345 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003346 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003347 ssl->next_record_offset ) );
3348 memmove( ssl->in_hdr,
3349 ssl->in_hdr + ssl->next_record_offset,
3350 ssl->in_left );
3351 }
3352
3353 ssl->next_record_offset = 0;
3354 }
3355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003357 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358
3359 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003360 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003361 */
3362 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003363 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003364 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003365 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003366 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003367
3368 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003369 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003370 * are not at the beginning of a new record, the caller did something
3371 * wrong.
3372 */
3373 if( ssl->in_left != 0 )
3374 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003375 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3376 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003377 }
3378
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003379 /*
3380 * Don't even try to read if time's out already.
3381 * This avoids by-passing the timer when repeatedly receiving messages
3382 * that will end up being dropped.
3383 */
3384 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003385 {
3386 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003387 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003388 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003389 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003390 {
Angus Grattond8213d02016-05-25 20:56:48 +10003391 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003393 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003394 timeout = ssl->handshake->retransmit_timeout;
3395 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003396 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003399
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003400 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003401 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3402 timeout );
3403 else
3404 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407
3408 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003409 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003410 }
3411
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003412 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003415 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003417 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003418 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003419 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003422 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003423 }
3424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003426 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003428 return( ret );
3429 }
3430
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003431 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003432 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003434 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003435 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003436 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003437 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003440 return( ret );
3441 }
3442
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003443 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003444 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003445#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003446 }
3447
Paul Bakker5121ce52009-01-03 21:22:43 +00003448 if( ret < 0 )
3449 return( ret );
3450
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003451 ssl->in_left = ret;
3452 }
3453 else
3454#endif
3455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003457 ssl->in_left, nb_want ) );
3458
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003459 while( ssl->in_left < nb_want )
3460 {
3461 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003462
3463 if( ssl_check_timer( ssl ) != 0 )
3464 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3465 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003466 {
3467 if( ssl->f_recv_timeout != NULL )
3468 {
3469 ret = ssl->f_recv_timeout( ssl->p_bio,
3470 ssl->in_hdr + ssl->in_left, len,
3471 ssl->conf->read_timeout );
3472 }
3473 else
3474 {
3475 ret = ssl->f_recv( ssl->p_bio,
3476 ssl->in_hdr + ssl->in_left, len );
3477 }
3478 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003481 ssl->in_left, nb_want ) );
3482 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003483
3484 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003486
3487 if( ret < 0 )
3488 return( ret );
3489
mohammad160352aecb92018-03-28 23:41:40 -07003490 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003491 {
Darryl Green11999bb2018-03-13 15:22:58 +00003492 MBEDTLS_SSL_DEBUG_MSG( 1,
3493 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003494 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003495 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3496 }
3497
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003498 ssl->in_left += ret;
3499 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003500 }
3501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003503
3504 return( 0 );
3505}
3506
3507/*
3508 * Flush any data not yet written
3509 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003510int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003511{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003512 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003513 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003516
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003517 if( ssl->f_send == NULL )
3518 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003519 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003520 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003522 }
3523
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003524 /* Avoid incrementing counter if data is flushed */
3525 if( ssl->out_left == 0 )
3526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003528 return( 0 );
3529 }
3530
Paul Bakker5121ce52009-01-03 21:22:43 +00003531 while( ssl->out_left > 0 )
3532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003534 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003535
Hanno Becker2b1e3542018-08-06 11:19:13 +01003536 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003537 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003539 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003540
3541 if( ret <= 0 )
3542 return( ret );
3543
mohammad160352aecb92018-03-28 23:41:40 -07003544 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003545 {
Darryl Green11999bb2018-03-13 15:22:58 +00003546 MBEDTLS_SSL_DEBUG_MSG( 1,
3547 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003548 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003549 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3550 }
3551
Paul Bakker5121ce52009-01-03 21:22:43 +00003552 ssl->out_left -= ret;
3553 }
3554
Hanno Becker2b1e3542018-08-06 11:19:13 +01003555#if defined(MBEDTLS_SSL_PROTO_DTLS)
3556 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003557 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003558 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003559 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003560 else
3561#endif
3562 {
3563 ssl->out_hdr = ssl->out_buf + 8;
3564 }
3565 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003568
3569 return( 0 );
3570}
3571
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003572/*
3573 * Functions to handle the DTLS retransmission state machine
3574 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003576/*
3577 * Append current handshake message to current outgoing flight
3578 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003579static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003580{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003581 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003582 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3583 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3584 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585
3586 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003587 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003588 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003589 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003590 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003591 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 }
3593
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003594 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003595 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003596 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003597 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003598 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003599 }
3600
3601 /* Copy current handshake message with headers */
3602 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3603 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003604 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003605 msg->next = NULL;
3606
3607 /* Append to the current flight */
3608 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003609 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003610 else
3611 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003612 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003613 while( cur->next != NULL )
3614 cur = cur->next;
3615 cur->next = msg;
3616 }
3617
Hanno Becker3b235902018-08-06 09:54:53 +01003618 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003619 return( 0 );
3620}
3621
3622/*
3623 * Free the current flight of handshake messages
3624 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003625static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003627 mbedtls_ssl_flight_item *cur = flight;
3628 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003629
3630 while( cur != NULL )
3631 {
3632 next = cur->next;
3633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003634 mbedtls_free( cur->p );
3635 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003636
3637 cur = next;
3638 }
3639}
3640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003641#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3642static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003643#endif
3644
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003645/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003646 * Swap transform_out and out_ctr with the alternative ones
3647 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003648static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003649{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003650 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003651 unsigned char tmp_out_ctr[8];
3652
3653 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003655 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656 return;
3657 }
3658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003659 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003660
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003661 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003662 tmp_transform = ssl->transform_out;
3663 ssl->transform_out = ssl->handshake->alt_transform_out;
3664 ssl->handshake->alt_transform_out = tmp_transform;
3665
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003666 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003667 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3668 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003669 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003670
3671 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003672 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3675 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003677 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003679 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3680 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003681 }
3682 }
3683#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003684}
3685
3686/*
3687 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003688 */
3689int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3690{
3691 int ret = 0;
3692
3693 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3694
3695 ret = mbedtls_ssl_flight_transmit( ssl );
3696
3697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3698
3699 return( ret );
3700}
3701
3702/*
3703 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704 *
3705 * Need to remember the current message in case flush_output returns
3706 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003707 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003708 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003709int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003710{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003711 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003714 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003715 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003716 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003717
3718 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003719 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003720 ssl_swap_epochs( ssl );
3721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003722 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003723 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003724
3725 while( ssl->handshake->cur_msg != NULL )
3726 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003727 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003728 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003729
Hanno Beckere1dcb032018-08-17 16:47:58 +01003730 int const is_finished =
3731 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3732 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3733
Hanno Becker04da1892018-08-14 13:22:10 +01003734 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3735 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3736
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003737 /* Swap epochs before sending Finished: we can't do it after
3738 * sending ChangeCipherSpec, in case write returns WANT_READ.
3739 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003740 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003741 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003742 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003743 ssl_swap_epochs( ssl );
3744 }
3745
Hanno Becker67bc7c32018-08-06 11:33:50 +01003746 ret = ssl_get_remaining_payload_in_datagram( ssl );
3747 if( ret < 0 )
3748 return( ret );
3749 max_frag_len = (size_t) ret;
3750
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003751 /* CCS is copied as is, while HS messages may need fragmentation */
3752 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3753 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003754 if( max_frag_len == 0 )
3755 {
3756 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3757 return( ret );
3758
3759 continue;
3760 }
3761
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003762 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003763 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003764 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003765
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003766 /* Update position inside current message */
3767 ssl->handshake->cur_msg_p += cur->len;
3768 }
3769 else
3770 {
3771 const unsigned char * const p = ssl->handshake->cur_msg_p;
3772 const size_t hs_len = cur->len - 12;
3773 const size_t frag_off = p - ( cur->p + 12 );
3774 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003775 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003776
Hanno Beckere1dcb032018-08-17 16:47:58 +01003777 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003778 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003779 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003780 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003781
Hanno Becker67bc7c32018-08-06 11:33:50 +01003782 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3783 return( ret );
3784
3785 continue;
3786 }
3787 max_hs_frag_len = max_frag_len - 12;
3788
3789 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3790 max_hs_frag_len : rem_len;
3791
3792 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003793 {
3794 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003795 (unsigned) cur_hs_frag_len,
3796 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003797 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003798
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003799 /* Messages are stored with handshake headers as if not fragmented,
3800 * copy beginning of headers then fill fragmentation fields.
3801 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3802 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003803
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003804 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3805 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3806 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3807
Hanno Becker67bc7c32018-08-06 11:33:50 +01003808 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3809 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3810 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003811
3812 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3813
Hanno Becker3f7b9732018-08-28 09:53:25 +01003814 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003815 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3816 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003817 ssl->out_msgtype = cur->type;
3818
3819 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003820 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003821 }
3822
3823 /* If done with the current message move to the next one if any */
3824 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3825 {
3826 if( cur->next != NULL )
3827 {
3828 ssl->handshake->cur_msg = cur->next;
3829 ssl->handshake->cur_msg_p = cur->next->p + 12;
3830 }
3831 else
3832 {
3833 ssl->handshake->cur_msg = NULL;
3834 ssl->handshake->cur_msg_p = NULL;
3835 }
3836 }
3837
3838 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003839 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003842 return( ret );
3843 }
3844 }
3845
Hanno Becker67bc7c32018-08-06 11:33:50 +01003846 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3847 return( ret );
3848
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003849 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003850 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3851 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003852 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003854 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003855 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3856 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003857
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003858 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003859
3860 return( 0 );
3861}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003862
3863/*
3864 * To be called when the last message of an incoming flight is received.
3865 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003866void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003867{
3868 /* We won't need to resend that one any more */
3869 ssl_flight_free( ssl->handshake->flight );
3870 ssl->handshake->flight = NULL;
3871 ssl->handshake->cur_msg = NULL;
3872
3873 /* The next incoming flight will start with this msg_seq */
3874 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3875
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003876 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003877 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003878
Hanno Becker0271f962018-08-16 13:23:47 +01003879 /* Clear future message buffering structure. */
3880 ssl_buffering_free( ssl );
3881
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003882 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003883 ssl_set_timer( ssl, 0 );
3884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3886 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003889 }
3890 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003892}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003893
3894/*
3895 * To be called when the last message of an outgoing flight is send.
3896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003899 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003900 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3903 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003906 }
3907 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003908 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003909}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003911
Paul Bakker5121ce52009-01-03 21:22:43 +00003912/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003913 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003914 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003915
3916/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003917 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003918 *
3919 * - fill in handshake headers
3920 * - update handshake checksum
3921 * - DTLS: save message for resending
3922 * - then pass to the record layer
3923 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003924 * DTLS: except for HelloRequest, messages are only queued, and will only be
3925 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003926 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003927 * Inputs:
3928 * - ssl->out_msglen: 4 + actual handshake message len
3929 * (4 is the size of handshake headers for TLS)
3930 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3931 * - ssl->out_msg + 4: the handshake message body
3932 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003933 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003934 * - ssl->out_msglen: the length of the record contents
3935 * (including handshake headers but excluding record headers)
3936 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003937 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003938int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003939{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003940 int ret;
3941 const size_t hs_len = ssl->out_msglen - 4;
3942 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003943
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003944 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3945
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003946 /*
3947 * Sanity checks
3948 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003949 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003950 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3951 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003952 /* In SSLv3, the client might send a NoCertificate alert. */
3953#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3954 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3955 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3956 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3957#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3958 {
3959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3960 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3961 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003962 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003963
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003964 /* Whenever we send anything different from a
3965 * HelloRequest we should be in a handshake - double check. */
3966 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3967 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003968 ssl->handshake == NULL )
3969 {
3970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3971 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3972 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003975 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003976 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3980 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003982#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003983
Hanno Beckerb50a2532018-08-06 11:52:54 +01003984 /* Double-check that we did not exceed the bounds
3985 * of the outgoing record buffer.
3986 * This should never fail as the various message
3987 * writing functions must obey the bounds of the
3988 * outgoing record buffer, but better be safe.
3989 *
3990 * Note: We deliberately do not check for the MTU or MFL here.
3991 */
3992 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3993 {
3994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3995 "size %u, maximum %u",
3996 (unsigned) ssl->out_msglen,
3997 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3998 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3999 }
4000
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004001 /*
4002 * Fill handshake headers
4003 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004005 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004006 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4007 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4008 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004009
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004010 /*
4011 * DTLS has additional fields in the Handshake layer,
4012 * between the length field and the actual payload:
4013 * uint16 message_seq;
4014 * uint24 fragment_offset;
4015 * uint24 fragment_length;
4016 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004017#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004018 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004019 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004020 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004021 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004022 {
4023 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4024 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004025 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004026 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4028 }
4029
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004030 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004031 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004032
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004033 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004034 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004035 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004036 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4037 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4038 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004039 }
4040 else
4041 {
4042 ssl->out_msg[4] = 0;
4043 ssl->out_msg[5] = 0;
4044 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004045
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004046 /* Handshake hashes are computed without fragmentation,
4047 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004048 memset( ssl->out_msg + 6, 0x00, 3 );
4049 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004050 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004051#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004052
Hanno Becker0207e532018-08-28 10:28:28 +01004053 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004054 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4055 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004056 }
4057
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004058 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004061 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4062 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004063 {
4064 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004066 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004067 return( ret );
4068 }
4069 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004070 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004071#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004072 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004073 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004074 {
4075 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4076 return( ret );
4077 }
4078 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004079
4080 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4081
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004082 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004083}
4084
4085/*
4086 * Record layer functions
4087 */
4088
4089/*
4090 * Write current record.
4091 *
4092 * Uses:
4093 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4094 * - ssl->out_msglen: length of the record content (excl headers)
4095 * - ssl->out_msg: record content
4096 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004097int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004098{
4099 int ret, done = 0;
4100 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004101 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004102
4103 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004106 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004107 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004108 {
4109 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112 return( ret );
4113 }
4114
4115 len = ssl->out_msglen;
4116 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004117#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4120 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004121 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 ret = mbedtls_ssl_hw_record_write( ssl );
4125 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004127 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4128 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004129 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004130
4131 if( ret == 0 )
4132 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004133 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004134#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004135 if( !done )
4136 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004137 unsigned i;
4138 size_t protected_record_size;
4139
Hanno Becker6430faf2019-05-08 11:57:13 +01004140 /* Skip writing the record content type to after the encryption,
4141 * as it may change when using the CID extension. */
4142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004143 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004144 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004145
Hanno Becker19859472018-08-06 09:40:20 +01004146 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004147 ssl->out_len[0] = (unsigned char)( len >> 8 );
4148 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004149
Paul Bakker48916f92012-09-16 19:57:18 +00004150 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004151 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004152 mbedtls_record rec;
4153
4154 rec.buf = ssl->out_iv;
4155 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4156 ( ssl->out_iv - ssl->out_buf );
4157 rec.data_len = ssl->out_msglen;
4158 rec.data_offset = ssl->out_msg - rec.buf;
4159
4160 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4161 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4162 ssl->conf->transport, rec.ver );
4163 rec.type = ssl->out_msgtype;
4164
Hanno Beckera0e20d02019-05-15 14:03:01 +01004165#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004166 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004167 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004168#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004169
Hanno Beckera18d1322018-01-03 14:27:32 +00004170 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004171 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004174 return( ret );
4175 }
4176
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004177 if( rec.data_offset != 0 )
4178 {
4179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4180 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4181 }
4182
Hanno Becker6430faf2019-05-08 11:57:13 +01004183 /* Update the record content type and CID. */
4184 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004185#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004186 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004187#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004188 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004189 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4190 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004191 }
4192
Hanno Becker5903de42019-05-03 14:46:38 +01004193 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004194
4195#if defined(MBEDTLS_SSL_PROTO_DTLS)
4196 /* In case of DTLS, double-check that we don't exceed
4197 * the remaining space in the datagram. */
4198 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4199 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004200 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004201 if( ret < 0 )
4202 return( ret );
4203
4204 if( protected_record_size > (size_t) ret )
4205 {
4206 /* Should never happen */
4207 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4208 }
4209 }
4210#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004211
Hanno Becker6430faf2019-05-08 11:57:13 +01004212 /* Now write the potentially updated record content type. */
4213 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004215 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004216 "version = [%d:%d], msglen = %d",
4217 ssl->out_hdr[0], ssl->out_hdr[1],
4218 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004220 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004221 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004222
4223 ssl->out_left += protected_record_size;
4224 ssl->out_hdr += protected_record_size;
4225 ssl_update_out_pointers( ssl, ssl->transform_out );
4226
Hanno Becker04484622018-08-06 09:49:38 +01004227 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4228 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4229 break;
4230
4231 /* The loop goes to its end iff the counter is wrapping */
4232 if( i == ssl_ep_len( ssl ) )
4233 {
4234 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4235 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4236 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004237 }
4238
Hanno Becker67bc7c32018-08-06 11:33:50 +01004239#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4241 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004242 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004243 size_t remaining;
4244 ret = ssl_get_remaining_payload_in_datagram( ssl );
4245 if( ret < 0 )
4246 {
4247 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4248 ret );
4249 return( ret );
4250 }
4251
4252 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004253 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004254 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004255 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004256 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004257 else
4258 {
Hanno Becker513815a2018-08-20 11:56:09 +01004259 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004260 }
4261 }
4262#endif /* MBEDTLS_SSL_PROTO_DTLS */
4263
4264 if( ( flush == SSL_FORCE_FLUSH ) &&
4265 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004266 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004267 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004268 return( ret );
4269 }
4270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004272
4273 return( 0 );
4274}
4275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004276#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004277
4278static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4279{
4280 if( ssl->in_msglen < ssl->in_hslen ||
4281 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4282 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4283 {
4284 return( 1 );
4285 }
4286 return( 0 );
4287}
Hanno Becker44650b72018-08-16 12:51:11 +01004288
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004289static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004290{
4291 return( ( ssl->in_msg[9] << 16 ) |
4292 ( ssl->in_msg[10] << 8 ) |
4293 ssl->in_msg[11] );
4294}
4295
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004296static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004297{
4298 return( ( ssl->in_msg[6] << 16 ) |
4299 ( ssl->in_msg[7] << 8 ) |
4300 ssl->in_msg[8] );
4301}
4302
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004303static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004304{
4305 uint32_t msg_len, frag_off, frag_len;
4306
4307 msg_len = ssl_get_hs_total_len( ssl );
4308 frag_off = ssl_get_hs_frag_off( ssl );
4309 frag_len = ssl_get_hs_frag_len( ssl );
4310
4311 if( frag_off > msg_len )
4312 return( -1 );
4313
4314 if( frag_len > msg_len - frag_off )
4315 return( -1 );
4316
4317 if( frag_len + 12 > ssl->in_msglen )
4318 return( -1 );
4319
4320 return( 0 );
4321}
4322
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004323/*
4324 * Mark bits in bitmask (used for DTLS HS reassembly)
4325 */
4326static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4327{
4328 unsigned int start_bits, end_bits;
4329
4330 start_bits = 8 - ( offset % 8 );
4331 if( start_bits != 8 )
4332 {
4333 size_t first_byte_idx = offset / 8;
4334
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004335 /* Special case */
4336 if( len <= start_bits )
4337 {
4338 for( ; len != 0; len-- )
4339 mask[first_byte_idx] |= 1 << ( start_bits - len );
4340
4341 /* Avoid potential issues with offset or len becoming invalid */
4342 return;
4343 }
4344
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004345 offset += start_bits; /* Now offset % 8 == 0 */
4346 len -= start_bits;
4347
4348 for( ; start_bits != 0; start_bits-- )
4349 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4350 }
4351
4352 end_bits = len % 8;
4353 if( end_bits != 0 )
4354 {
4355 size_t last_byte_idx = ( offset + len ) / 8;
4356
4357 len -= end_bits; /* Now len % 8 == 0 */
4358
4359 for( ; end_bits != 0; end_bits-- )
4360 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4361 }
4362
4363 memset( mask + offset / 8, 0xFF, len / 8 );
4364}
4365
4366/*
4367 * Check that bitmask is full
4368 */
4369static int ssl_bitmask_check( unsigned char *mask, size_t len )
4370{
4371 size_t i;
4372
4373 for( i = 0; i < len / 8; i++ )
4374 if( mask[i] != 0xFF )
4375 return( -1 );
4376
4377 for( i = 0; i < len % 8; i++ )
4378 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4379 return( -1 );
4380
4381 return( 0 );
4382}
4383
Hanno Becker56e205e2018-08-16 09:06:12 +01004384/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004385static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004386 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004387{
Hanno Becker56e205e2018-08-16 09:06:12 +01004388 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004389
Hanno Becker56e205e2018-08-16 09:06:12 +01004390 alloc_len = 12; /* Handshake header */
4391 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004392
Hanno Beckerd07df862018-08-16 09:14:58 +01004393 if( add_bitmap )
4394 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004395
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004396 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004397}
Hanno Becker56e205e2018-08-16 09:06:12 +01004398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004399#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004400
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004401static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004402{
4403 return( ( ssl->in_msg[1] << 16 ) |
4404 ( ssl->in_msg[2] << 8 ) |
4405 ssl->in_msg[3] );
4406}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004407
Simon Butcher99000142016-10-13 17:21:01 +01004408int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004409{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004410 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004411 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004413 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004414 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004415 }
4416
Hanno Becker12555c62018-08-16 12:47:53 +01004417 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004419 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004420 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004421 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004423#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004424 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004425 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004426 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004427 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004428
Hanno Becker44650b72018-08-16 12:51:11 +01004429 if( ssl_check_hs_header( ssl ) != 0 )
4430 {
4431 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4432 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4433 }
4434
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004435 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004436 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4437 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4438 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4439 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004440 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004441 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4442 {
4443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4444 recv_msg_seq,
4445 ssl->handshake->in_msg_seq ) );
4446 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4447 }
4448
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004449 /* Retransmit only on last message from previous flight, to avoid
4450 * too many retransmissions.
4451 * Besides, No sane server ever retransmits HelloVerifyRequest */
4452 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004453 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004456 "message_seq = %d, start_of_flight = %d",
4457 recv_msg_seq,
4458 ssl->handshake->in_flight_start_seq ) );
4459
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004460 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004463 return( ret );
4464 }
4465 }
4466 else
4467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004469 "message_seq = %d, expected = %d",
4470 recv_msg_seq,
4471 ssl->handshake->in_msg_seq ) );
4472 }
4473
Hanno Becker90333da2017-10-10 11:27:13 +01004474 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004475 }
4476 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004477
Hanno Becker6d97ef52018-08-16 13:09:04 +01004478 /* Message reassembly is handled alongside buffering of future
4479 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004480 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004481 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004482 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004483 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004485 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004486 }
4487 }
4488 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004489#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004490 /* With TLS we don't handle fragmentation (for now) */
4491 if( ssl->in_msglen < ssl->in_hslen )
4492 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004493 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4494 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004495 }
4496
Simon Butcher99000142016-10-13 17:21:01 +01004497 return( 0 );
4498}
4499
4500void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4501{
Hanno Becker0271f962018-08-16 13:23:47 +01004502 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004503
Hanno Becker0271f962018-08-16 13:23:47 +01004504 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004505 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004506 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004507 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004508
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004509 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004510#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004511 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004512 ssl->handshake != NULL )
4513 {
Hanno Becker0271f962018-08-16 13:23:47 +01004514 unsigned offset;
4515 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004516
Hanno Becker0271f962018-08-16 13:23:47 +01004517 /* Increment handshake sequence number */
4518 hs->in_msg_seq++;
4519
4520 /*
4521 * Clear up handshake buffering and reassembly structure.
4522 */
4523
4524 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004525 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004526
4527 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004528 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4529 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004530 offset++, hs_buf++ )
4531 {
4532 *hs_buf = *(hs_buf + 1);
4533 }
4534
4535 /* Create a fresh last entry */
4536 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004537 }
4538#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004539}
4540
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004541/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004542 * DTLS anti-replay: RFC 6347 4.1.2.6
4543 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004544 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4545 * Bit n is set iff record number in_window_top - n has been seen.
4546 *
4547 * Usually, in_window_top is the last record number seen and the lsb of
4548 * in_window is set. The only exception is the initial state (record number 0
4549 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004551#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4552static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004553{
4554 ssl->in_window_top = 0;
4555 ssl->in_window = 0;
4556}
4557
4558static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4559{
4560 return( ( (uint64_t) buf[0] << 40 ) |
4561 ( (uint64_t) buf[1] << 32 ) |
4562 ( (uint64_t) buf[2] << 24 ) |
4563 ( (uint64_t) buf[3] << 16 ) |
4564 ( (uint64_t) buf[4] << 8 ) |
4565 ( (uint64_t) buf[5] ) );
4566}
4567
4568/*
4569 * Return 0 if sequence number is acceptable, -1 otherwise
4570 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004571int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004572{
4573 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4574 uint64_t bit;
4575
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004576 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004577 return( 0 );
4578
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004579 if( rec_seqnum > ssl->in_window_top )
4580 return( 0 );
4581
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004582 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004583
4584 if( bit >= 64 )
4585 return( -1 );
4586
4587 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4588 return( -1 );
4589
4590 return( 0 );
4591}
4592
4593/*
4594 * Update replay window on new validated record
4595 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004596void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004597{
4598 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4599
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004600 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004601 return;
4602
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004603 if( rec_seqnum > ssl->in_window_top )
4604 {
4605 /* Update window_top and the contents of the window */
4606 uint64_t shift = rec_seqnum - ssl->in_window_top;
4607
4608 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004609 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004610 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004611 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004613 ssl->in_window |= 1;
4614 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004615
4616 ssl->in_window_top = rec_seqnum;
4617 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004618 else
4619 {
4620 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004621 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004622
4623 if( bit < 64 ) /* Always true, but be extra sure */
4624 ssl->in_window |= (uint64_t) 1 << bit;
4625 }
4626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004627#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004628
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004629#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004630/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004631static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4632
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004633/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004634 * Without any SSL context, check if a datagram looks like a ClientHello with
4635 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004636 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004637 *
4638 * - if cookie is valid, return 0
4639 * - if ClientHello looks superficially valid but cookie is not,
4640 * fill obuf and set olen, then
4641 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4642 * - otherwise return a specific error code
4643 */
4644static int ssl_check_dtls_clihlo_cookie(
4645 mbedtls_ssl_cookie_write_t *f_cookie_write,
4646 mbedtls_ssl_cookie_check_t *f_cookie_check,
4647 void *p_cookie,
4648 const unsigned char *cli_id, size_t cli_id_len,
4649 const unsigned char *in, size_t in_len,
4650 unsigned char *obuf, size_t buf_len, size_t *olen )
4651{
4652 size_t sid_len, cookie_len;
4653 unsigned char *p;
4654
4655 if( f_cookie_write == NULL || f_cookie_check == NULL )
4656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4657
4658 /*
4659 * Structure of ClientHello with record and handshake headers,
4660 * and expected values. We don't need to check a lot, more checks will be
4661 * done when actually parsing the ClientHello - skipping those checks
4662 * avoids code duplication and does not make cookie forging any easier.
4663 *
4664 * 0-0 ContentType type; copied, must be handshake
4665 * 1-2 ProtocolVersion version; copied
4666 * 3-4 uint16 epoch; copied, must be 0
4667 * 5-10 uint48 sequence_number; copied
4668 * 11-12 uint16 length; (ignored)
4669 *
4670 * 13-13 HandshakeType msg_type; (ignored)
4671 * 14-16 uint24 length; (ignored)
4672 * 17-18 uint16 message_seq; copied
4673 * 19-21 uint24 fragment_offset; copied, must be 0
4674 * 22-24 uint24 fragment_length; (ignored)
4675 *
4676 * 25-26 ProtocolVersion client_version; (ignored)
4677 * 27-58 Random random; (ignored)
4678 * 59-xx SessionID session_id; 1 byte len + sid_len content
4679 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4680 * ...
4681 *
4682 * Minimum length is 61 bytes.
4683 */
4684 if( in_len < 61 ||
4685 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4686 in[3] != 0 || in[4] != 0 ||
4687 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4688 {
4689 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4690 }
4691
4692 sid_len = in[59];
4693 if( sid_len > in_len - 61 )
4694 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4695
4696 cookie_len = in[60 + sid_len];
4697 if( cookie_len > in_len - 60 )
4698 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4699
4700 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4701 cli_id, cli_id_len ) == 0 )
4702 {
4703 /* Valid cookie */
4704 return( 0 );
4705 }
4706
4707 /*
4708 * If we get here, we've got an invalid cookie, let's prepare HVR.
4709 *
4710 * 0-0 ContentType type; copied
4711 * 1-2 ProtocolVersion version; copied
4712 * 3-4 uint16 epoch; copied
4713 * 5-10 uint48 sequence_number; copied
4714 * 11-12 uint16 length; olen - 13
4715 *
4716 * 13-13 HandshakeType msg_type; hello_verify_request
4717 * 14-16 uint24 length; olen - 25
4718 * 17-18 uint16 message_seq; copied
4719 * 19-21 uint24 fragment_offset; copied
4720 * 22-24 uint24 fragment_length; olen - 25
4721 *
4722 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4723 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4724 *
4725 * Minimum length is 28.
4726 */
4727 if( buf_len < 28 )
4728 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4729
4730 /* Copy most fields and adapt others */
4731 memcpy( obuf, in, 25 );
4732 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4733 obuf[25] = 0xfe;
4734 obuf[26] = 0xff;
4735
4736 /* Generate and write actual cookie */
4737 p = obuf + 28;
4738 if( f_cookie_write( p_cookie,
4739 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4740 {
4741 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4742 }
4743
4744 *olen = p - obuf;
4745
4746 /* Go back and fill length fields */
4747 obuf[27] = (unsigned char)( *olen - 28 );
4748
4749 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4750 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4751 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4752
4753 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4754 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4755
4756 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4757}
4758
4759/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004760 * Handle possible client reconnect with the same UDP quadruplet
4761 * (RFC 6347 Section 4.2.8).
4762 *
4763 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4764 * that looks like a ClientHello.
4765 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 * - if the input looks like a ClientHello without cookies,
4767 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004768 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004769 * - if the input looks like a ClientHello with a valid cookie,
4770 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004771 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004772 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004773 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004774 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004775 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4776 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004777 */
4778static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4779{
4780 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004781 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004782
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004783 ret = ssl_check_dtls_clihlo_cookie(
4784 ssl->conf->f_cookie_write,
4785 ssl->conf->f_cookie_check,
4786 ssl->conf->p_cookie,
4787 ssl->cli_id, ssl->cli_id_len,
4788 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004789 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004790
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004791 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4792
4793 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004794 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004795 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004796 * If the error is permanent we'll catch it later,
4797 * if it's not, then hopefully it'll work next time. */
4798 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4799
4800 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004801 }
4802
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004803 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004804 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004805 /* Got a valid cookie, partially reset context */
4806 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4807 {
4808 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4809 return( ret );
4810 }
4811
4812 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004813 }
4814
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004815 return( ret );
4816}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004817#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004818
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004819static int ssl_check_record_type( uint8_t record_type )
4820{
4821 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4822 record_type != MBEDTLS_SSL_MSG_ALERT &&
4823 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4824 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4825 {
4826 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4827 }
4828
4829 return( 0 );
4830}
4831
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004832/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004833 * ContentType type;
4834 * ProtocolVersion version;
4835 * uint16 epoch; // DTLS only
4836 * uint48 sequence_number; // DTLS only
4837 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004838 *
4839 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004840 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004841 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4842 *
4843 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004844 * 1. proceed with the record if this function returns 0
4845 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4846 * 3. return CLIENT_RECONNECT if this function return that value
4847 * 4. drop the whole datagram if this function returns anything else.
4848 * Point 2 is needed when the peer is resending, and we have already received
4849 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004850 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004851static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004852{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004853 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004854 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004855
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004856 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004857
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004859 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004860
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004861 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004862#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004863 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4864 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4865 ssl->conf->cid_len != 0 )
4866 {
4867 /* Shift pointers to account for record header including CID
4868 * struct {
4869 * ContentType special_type = tls12_cid;
4870 * ProtocolVersion version;
4871 * uint16 epoch;
4872 * uint48 sequence_number;
4873 * opaque cid[cid_length]; // New field
4874 * uint16 length;
4875 * opaque enc_content[DTLSCiphertext.length];
4876 * } DTLSCiphertext;
4877 */
4878
4879 /* So far, we only support static CID lengths
4880 * fixed in the configuration. */
4881 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4882 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4883 }
4884 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004885#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004886 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004889
4890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004891 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4892 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004893 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4894#endif /* MBEDTLS_SSL_PROTO_DTLS */
4895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4896 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004899 }
4900
4901 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004902 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004906 }
4907
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004908 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4911 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 }
4913
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004914 /* Now that the total length of the record header is known, ensure
4915 * that the current datagram is large enough to hold it.
4916 * This would fail, for example, if we received a datagram of
4917 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4918 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4919 if( ret != 0 )
4920 {
4921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4922 return( ret );
4923 }
4924 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4925
4926 /* Parse and validate record length
4927 * This must happen after the CID parsing because
4928 * its position in the record header depends on
4929 * the presence of a CID. */
4930
4931 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004932 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004933 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4936 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004937 }
4938
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
4940 "version = [%d:%d], msglen = %d",
4941 ssl->in_msgtype,
4942 major_ver, minor_ver, ssl->in_msglen ) );
4943
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004944 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004945 * DTLS-related tests.
4946 * Check epoch before checking length constraint because
4947 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4948 * message gets duplicated before the corresponding Finished message,
4949 * the second ChangeCipherSpec should be discarded because it belongs
4950 * to an old epoch, but not because its length is shorter than
4951 * the minimum record length for packets using the new record transform.
4952 * Note that these two kinds of failures are handled differently,
4953 * as an unexpected record is silently skipped but an invalid
4954 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004955 */
4956#if defined(MBEDTLS_SSL_PROTO_DTLS)
4957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4958 {
4959 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4960
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004961 /* Check epoch (and sequence number) with DTLS */
4962 if( rec_epoch != ssl->in_epoch )
4963 {
4964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4965 "expected %d, received %d",
4966 ssl->in_epoch, rec_epoch ) );
4967
4968#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4969 /*
4970 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4971 * access the first byte of record content (handshake type), as we
4972 * have an active transform (possibly iv_len != 0), so use the
4973 * fact that the record header len is 13 instead.
4974 */
4975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4976 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4977 rec_epoch == 0 &&
4978 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4979 ssl->in_left > 13 &&
4980 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4981 {
4982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4983 "from the same port" ) );
4984 return( ssl_handle_possible_reconnect( ssl ) );
4985 }
4986 else
4987#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004988 {
4989 /* Consider buffering the record. */
4990 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4991 {
4992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4993 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4994 }
4995
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004996 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004997 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004998 }
4999
5000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5001 /* Replay detection only works for the current epoch */
5002 if( rec_epoch == ssl->in_epoch &&
5003 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5004 {
5005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5006 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5007 }
5008#endif
5009 }
5010#endif /* MBEDTLS_SSL_PROTO_DTLS */
5011
Hanno Becker52c6dc62017-05-26 16:07:36 +01005012
5013 /* Check length against bounds of the current transform and version */
5014 if( ssl->transform_in == NULL )
5015 {
5016 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005017 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5020 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5021 }
5022 }
5023 else
5024 {
5025 if( ssl->in_msglen < ssl->transform_in->minlen )
5026 {
5027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5028 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5029 }
5030
5031#if defined(MBEDTLS_SSL_PROTO_SSL3)
5032 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005033 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005034 {
5035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5037 }
5038#endif
5039#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5040 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5041 /*
5042 * TLS encrypted messages can have up to 256 bytes of padding
5043 */
5044 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5045 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005046 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005047 {
5048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5049 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5050 }
5051#endif
5052 }
5053
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005054 return( 0 );
5055}
Paul Bakker5121ce52009-01-03 21:22:43 +00005056
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005057/*
5058 * If applicable, decrypt (and decompress) record content
5059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061{
5062 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005065 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5068 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 ret = mbedtls_ssl_hw_record_read( ssl );
5073 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5076 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005077 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005078
5079 if( ret == 0 )
5080 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005083 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005084 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005085 mbedtls_record rec;
5086
5087 rec.buf = ssl->in_iv;
5088 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5089 - ( ssl->in_iv - ssl->in_buf );
5090 rec.data_len = ssl->in_msglen;
5091 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005092#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005093 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005094 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005096
5097 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5098 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5099 ssl->conf->transport, rec.ver );
5100 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005101 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5102 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005105
Hanno Beckera0e20d02019-05-15 14:03:01 +01005106#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005107 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5108 ssl->conf->ignore_unexpected_cid
5109 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5110 {
Hanno Becker16ded982019-05-08 13:02:55 +01005111 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005112 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005113#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005114
Paul Bakker5121ce52009-01-03 21:22:43 +00005115 return( ret );
5116 }
5117
Hanno Becker6430faf2019-05-08 11:57:13 +01005118 if( ssl->in_msgtype != rec.type )
5119 {
5120 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5121 ssl->in_msgtype, rec.type ) );
5122 }
5123
5124 /* The record content type may change during decryption,
5125 * so re-read it. */
5126 ssl->in_msgtype = rec.type;
5127 /* Also update the input buffer, because unfortunately
5128 * the server-side ssl_parse_client_hello() reparses the
5129 * record header when receiving a ClientHello initiating
5130 * a renegotiation. */
5131 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005132 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005133 ssl->in_msglen = rec.data_len;
5134 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5135 ssl->in_len[1] = (unsigned char)( rec.data_len );
5136
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005137 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5138 ssl->in_msg, ssl->in_msglen );
5139
Hanno Beckera0e20d02019-05-15 14:03:01 +01005140#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005141 /* We have already checked the record content type
5142 * in ssl_parse_record_header(), failing or silently
5143 * dropping the record in the case of an unknown type.
5144 *
5145 * Since with the use of CIDs, the record content type
5146 * might change during decryption, re-check the record
5147 * content type, but treat a failure as fatal this time. */
5148 if( ssl_check_record_type( ssl->in_msgtype ) )
5149 {
5150 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5151 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5152 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005153#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005154
Angus Grattond8213d02016-05-25 20:56:48 +10005155 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005157 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5158 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005159 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005160 else if( ssl->in_msglen == 0 )
5161 {
5162#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5163 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5164 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5165 {
5166 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5167 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5168 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5169 }
5170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5171
5172 ssl->nb_zero++;
5173
5174 /*
5175 * Three or more empty messages may be a DoS attack
5176 * (excessive CPU consumption).
5177 */
5178 if( ssl->nb_zero > 3 )
5179 {
5180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005181 "messages, possible DoS attack" ) );
5182 /* Treat the records as if they were not properly authenticated,
5183 * thereby failing the connection if we see more than allowed
5184 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005185 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5186 }
5187 }
5188 else
5189 ssl->nb_zero = 0;
5190
5191#if defined(MBEDTLS_SSL_PROTO_DTLS)
5192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5193 {
5194 ; /* in_ctr read from peer, not maintained internally */
5195 }
5196 else
5197#endif
5198 {
5199 unsigned i;
5200 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5201 if( ++ssl->in_ctr[i - 1] != 0 )
5202 break;
5203
5204 /* The loop goes to its end iff the counter is wrapping */
5205 if( i == ssl_ep_len( ssl ) )
5206 {
5207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5208 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5209 }
5210 }
5211
Paul Bakker5121ce52009-01-03 21:22:43 +00005212 }
5213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005214#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005215 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005217 {
5218 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005221 return( ret );
5222 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005223 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005224#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005226#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005227 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005229 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005230 }
5231#endif
5232
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005233 return( 0 );
5234}
5235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005236static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005237
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005238/*
5239 * Read a record.
5240 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005241 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5242 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5243 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005244 */
Hanno Becker1097b342018-08-15 14:09:41 +01005245
5246/* Helper functions for mbedtls_ssl_read_record(). */
5247static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005248static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5249static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005250
Hanno Becker327c93b2018-08-15 13:56:18 +01005251int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005252 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005253{
5254 int ret;
5255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005257
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005258 if( ssl->keep_current_message == 0 )
5259 {
5260 do {
Simon Butcher99000142016-10-13 17:21:01 +01005261
Hanno Becker26994592018-08-15 14:14:59 +01005262 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005263 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005264 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005265
Hanno Beckere74d5562018-08-15 14:26:08 +01005266 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005267 {
Hanno Becker40f50842018-08-15 14:48:01 +01005268#if defined(MBEDTLS_SSL_PROTO_DTLS)
5269 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005270
Hanno Becker40f50842018-08-15 14:48:01 +01005271 /* We only check for buffered messages if the
5272 * current datagram is fully consumed. */
5273 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005274 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005275 {
Hanno Becker40f50842018-08-15 14:48:01 +01005276 if( ssl_load_buffered_message( ssl ) == 0 )
5277 have_buffered = 1;
5278 }
5279
5280 if( have_buffered == 0 )
5281#endif /* MBEDTLS_SSL_PROTO_DTLS */
5282 {
5283 ret = ssl_get_next_record( ssl );
5284 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5285 continue;
5286
5287 if( ret != 0 )
5288 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005289 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005290 return( ret );
5291 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005292 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005293 }
5294
5295 ret = mbedtls_ssl_handle_message_type( ssl );
5296
Hanno Becker40f50842018-08-15 14:48:01 +01005297#if defined(MBEDTLS_SSL_PROTO_DTLS)
5298 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5299 {
5300 /* Buffer future message */
5301 ret = ssl_buffer_message( ssl );
5302 if( ret != 0 )
5303 return( ret );
5304
5305 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5306 }
5307#endif /* MBEDTLS_SSL_PROTO_DTLS */
5308
Hanno Becker90333da2017-10-10 11:27:13 +01005309 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5310 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005311
5312 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005313 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005314 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005315 return( ret );
5316 }
5317
Hanno Becker327c93b2018-08-15 13:56:18 +01005318 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005319 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005320 {
5321 mbedtls_ssl_update_handshake_status( ssl );
5322 }
Simon Butcher99000142016-10-13 17:21:01 +01005323 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005324 else
Simon Butcher99000142016-10-13 17:21:01 +01005325 {
Hanno Becker02f59072018-08-15 14:00:24 +01005326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005327 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005328 }
5329
5330 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5331
5332 return( 0 );
5333}
5334
Hanno Becker40f50842018-08-15 14:48:01 +01005335#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005336static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005337{
Hanno Becker40f50842018-08-15 14:48:01 +01005338 if( ssl->in_left > ssl->next_record_offset )
5339 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005340
Hanno Becker40f50842018-08-15 14:48:01 +01005341 return( 0 );
5342}
5343
5344static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5345{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005346 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005347 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005348 int ret = 0;
5349
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005350 if( hs == NULL )
5351 return( -1 );
5352
Hanno Beckere00ae372018-08-20 09:39:42 +01005353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5354
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005355 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5356 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5357 {
5358 /* Check if we have seen a ChangeCipherSpec before.
5359 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005360 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361 {
5362 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5363 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005364 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005365 }
5366
Hanno Becker39b8bc92018-08-28 17:17:13 +01005367 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005368 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5369 ssl->in_msglen = 1;
5370 ssl->in_msg[0] = 1;
5371
5372 /* As long as they are equal, the exact value doesn't matter. */
5373 ssl->in_left = 0;
5374 ssl->next_record_offset = 0;
5375
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005376 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005377 goto exit;
5378 }
Hanno Becker37f95322018-08-16 13:55:32 +01005379
Hanno Beckerb8f50142018-08-28 10:01:34 +01005380#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005381 /* Debug only */
5382 {
5383 unsigned offset;
5384 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5385 {
5386 hs_buf = &hs->buffering.hs[offset];
5387 if( hs_buf->is_valid == 1 )
5388 {
5389 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5390 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005391 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005392 }
5393 }
5394 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005395#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005396
5397 /* Check if we have buffered and/or fully reassembled the
5398 * next handshake message. */
5399 hs_buf = &hs->buffering.hs[0];
5400 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5401 {
5402 /* Synthesize a record containing the buffered HS message. */
5403 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5404 ( hs_buf->data[2] << 8 ) |
5405 hs_buf->data[3];
5406
5407 /* Double-check that we haven't accidentally buffered
5408 * a message that doesn't fit into the input buffer. */
5409 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5410 {
5411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5412 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5413 }
5414
5415 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5416 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5417 hs_buf->data, msg_len + 12 );
5418
5419 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5420 ssl->in_hslen = msg_len + 12;
5421 ssl->in_msglen = msg_len + 12;
5422 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5423
5424 ret = 0;
5425 goto exit;
5426 }
5427 else
5428 {
5429 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5430 hs->in_msg_seq ) );
5431 }
5432
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005433 ret = -1;
5434
5435exit:
5436
5437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5438 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005439}
5440
Hanno Beckera02b0b42018-08-21 17:20:27 +01005441static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5442 size_t desired )
5443{
5444 int offset;
5445 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005446 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5447 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005448
Hanno Becker01315ea2018-08-21 17:22:17 +01005449 /* Get rid of future records epoch first, if such exist. */
5450 ssl_free_buffered_record( ssl );
5451
5452 /* Check if we have enough space available now. */
5453 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5454 hs->buffering.total_bytes_buffered ) )
5455 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005456 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005457 return( 0 );
5458 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005459
Hanno Becker4f432ad2018-08-28 10:02:32 +01005460 /* We don't have enough space to buffer the next expected handshake
5461 * message. Remove buffers used for future messages to gain space,
5462 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005463 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5464 offset >= 0; offset-- )
5465 {
5466 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5467 offset ) );
5468
Hanno Beckerb309b922018-08-23 13:18:05 +01005469 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005470
5471 /* Check if we have enough space available now. */
5472 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5473 hs->buffering.total_bytes_buffered ) )
5474 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005475 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005476 return( 0 );
5477 }
5478 }
5479
5480 return( -1 );
5481}
5482
Hanno Becker40f50842018-08-15 14:48:01 +01005483static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5484{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005485 int ret = 0;
5486 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5487
5488 if( hs == NULL )
5489 return( 0 );
5490
5491 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5492
5493 switch( ssl->in_msgtype )
5494 {
5495 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005497
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005498 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005499 break;
5500
5501 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005502 {
5503 unsigned recv_msg_seq_offset;
5504 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5505 mbedtls_ssl_hs_buffer *hs_buf;
5506 size_t msg_len = ssl->in_hslen - 12;
5507
5508 /* We should never receive an old handshake
5509 * message - double-check nonetheless. */
5510 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5511 {
5512 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5513 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5514 }
5515
5516 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5517 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5518 {
5519 /* Silently ignore -- message too far in the future */
5520 MBEDTLS_SSL_DEBUG_MSG( 2,
5521 ( "Ignore future HS message with sequence number %u, "
5522 "buffering window %u - %u",
5523 recv_msg_seq, ssl->handshake->in_msg_seq,
5524 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5525
5526 goto exit;
5527 }
5528
5529 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5530 recv_msg_seq, recv_msg_seq_offset ) );
5531
5532 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5533
5534 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005535 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005536 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005537 size_t reassembly_buf_sz;
5538
Hanno Becker37f95322018-08-16 13:55:32 +01005539 hs_buf->is_fragmented =
5540 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5541
5542 /* We copy the message back into the input buffer
5543 * after reassembly, so check that it's not too large.
5544 * This is an implementation-specific limitation
5545 * and not one from the standard, hence it is not
5546 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005547 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005548 {
5549 /* Ignore message */
5550 goto exit;
5551 }
5552
Hanno Beckere0b150f2018-08-21 15:51:03 +01005553 /* Check if we have enough space to buffer the message. */
5554 if( hs->buffering.total_bytes_buffered >
5555 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5556 {
5557 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5558 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5559 }
5560
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005561 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5562 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005563
5564 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5565 hs->buffering.total_bytes_buffered ) )
5566 {
5567 if( recv_msg_seq_offset > 0 )
5568 {
5569 /* If we can't buffer a future message because
5570 * of space limitations -- ignore. */
5571 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",
5572 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5573 (unsigned) hs->buffering.total_bytes_buffered ) );
5574 goto exit;
5575 }
Hanno Beckere1801392018-08-21 16:51:05 +01005576 else
5577 {
5578 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",
5579 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5580 (unsigned) hs->buffering.total_bytes_buffered ) );
5581 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005582
Hanno Beckera02b0b42018-08-21 17:20:27 +01005583 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005584 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005585 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",
5586 (unsigned) msg_len,
5587 (unsigned) reassembly_buf_sz,
5588 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005589 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005590 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5591 goto exit;
5592 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005593 }
5594
5595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5596 msg_len ) );
5597
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005598 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5599 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005600 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005601 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005602 goto exit;
5603 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005604 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005605
5606 /* Prepare final header: copy msg_type, length and message_seq,
5607 * then add standardised fragment_offset and fragment_length */
5608 memcpy( hs_buf->data, ssl->in_msg, 6 );
5609 memset( hs_buf->data + 6, 0, 3 );
5610 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5611
5612 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005613
5614 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005615 }
5616 else
5617 {
5618 /* Make sure msg_type and length are consistent */
5619 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5620 {
5621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5622 /* Ignore */
5623 goto exit;
5624 }
5625 }
5626
Hanno Becker4422bbb2018-08-20 09:40:19 +01005627 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005628 {
5629 size_t frag_len, frag_off;
5630 unsigned char * const msg = hs_buf->data + 12;
5631
5632 /*
5633 * Check and copy current fragment
5634 */
5635
5636 /* Validation of header fields already done in
5637 * mbedtls_ssl_prepare_handshake_record(). */
5638 frag_off = ssl_get_hs_frag_off( ssl );
5639 frag_len = ssl_get_hs_frag_len( ssl );
5640
5641 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5642 frag_off, frag_len ) );
5643 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5644
5645 if( hs_buf->is_fragmented )
5646 {
5647 unsigned char * const bitmask = msg + msg_len;
5648 ssl_bitmask_set( bitmask, frag_off, frag_len );
5649 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5650 msg_len ) == 0 );
5651 }
5652 else
5653 {
5654 hs_buf->is_complete = 1;
5655 }
5656
5657 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5658 hs_buf->is_complete ? "" : "not yet " ) );
5659 }
5660
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005661 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005662 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005663
5664 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005665 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005666 break;
5667 }
5668
5669exit:
5670
5671 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5672 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005673}
5674#endif /* MBEDTLS_SSL_PROTO_DTLS */
5675
Hanno Becker1097b342018-08-15 14:09:41 +01005676static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005677{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005678 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005679 * Consume last content-layer message and potentially
5680 * update in_msglen which keeps track of the contents'
5681 * consumption state.
5682 *
5683 * (1) Handshake messages:
5684 * Remove last handshake message, move content
5685 * and adapt in_msglen.
5686 *
5687 * (2) Alert messages:
5688 * Consume whole record content, in_msglen = 0.
5689 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005690 * (3) Change cipher spec:
5691 * Consume whole record content, in_msglen = 0.
5692 *
5693 * (4) Application data:
5694 * Don't do anything - the record layer provides
5695 * the application data as a stream transport
5696 * and consumes through mbedtls_ssl_read only.
5697 *
5698 */
5699
5700 /* Case (1): Handshake messages */
5701 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005702 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005703 /* Hard assertion to be sure that no application data
5704 * is in flight, as corrupting ssl->in_msglen during
5705 * ssl->in_offt != NULL is fatal. */
5706 if( ssl->in_offt != NULL )
5707 {
5708 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5709 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5710 }
5711
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005712 /*
5713 * Get next Handshake message in the current record
5714 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005715
Hanno Becker4a810fb2017-05-24 16:27:30 +01005716 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005717 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005718 * current handshake content: If DTLS handshake
5719 * fragmentation is used, that's the fragment
5720 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005721 * size here is faulty and should be changed at
5722 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005723 * (2) While it doesn't seem to cause problems, one
5724 * has to be very careful not to assume that in_hslen
5725 * is always <= in_msglen in a sensible communication.
5726 * Again, it's wrong for DTLS handshake fragmentation.
5727 * The following check is therefore mandatory, and
5728 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005729 * Additionally, ssl->in_hslen might be arbitrarily out of
5730 * bounds after handling a DTLS message with an unexpected
5731 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005732 */
5733 if( ssl->in_hslen < ssl->in_msglen )
5734 {
5735 ssl->in_msglen -= ssl->in_hslen;
5736 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5737 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005738
Hanno Becker4a810fb2017-05-24 16:27:30 +01005739 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5740 ssl->in_msg, ssl->in_msglen );
5741 }
5742 else
5743 {
5744 ssl->in_msglen = 0;
5745 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005746
Hanno Becker4a810fb2017-05-24 16:27:30 +01005747 ssl->in_hslen = 0;
5748 }
5749 /* Case (4): Application data */
5750 else if( ssl->in_offt != NULL )
5751 {
5752 return( 0 );
5753 }
5754 /* Everything else (CCS & Alerts) */
5755 else
5756 {
5757 ssl->in_msglen = 0;
5758 }
5759
Hanno Becker1097b342018-08-15 14:09:41 +01005760 return( 0 );
5761}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005762
Hanno Beckere74d5562018-08-15 14:26:08 +01005763static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5764{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005765 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005766 return( 1 );
5767
5768 return( 0 );
5769}
5770
Hanno Becker5f066e72018-08-16 14:56:31 +01005771#if defined(MBEDTLS_SSL_PROTO_DTLS)
5772
5773static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5774{
5775 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5776 if( hs == NULL )
5777 return;
5778
Hanno Becker01315ea2018-08-21 17:22:17 +01005779 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005780 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005781 hs->buffering.total_bytes_buffered -=
5782 hs->buffering.future_record.len;
5783
5784 mbedtls_free( hs->buffering.future_record.data );
5785 hs->buffering.future_record.data = NULL;
5786 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005787}
5788
5789static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5790{
5791 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5792 unsigned char * rec;
5793 size_t rec_len;
5794 unsigned rec_epoch;
5795
5796 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5797 return( 0 );
5798
5799 if( hs == NULL )
5800 return( 0 );
5801
Hanno Becker5f066e72018-08-16 14:56:31 +01005802 rec = hs->buffering.future_record.data;
5803 rec_len = hs->buffering.future_record.len;
5804 rec_epoch = hs->buffering.future_record.epoch;
5805
5806 if( rec == NULL )
5807 return( 0 );
5808
Hanno Becker4cb782d2018-08-20 11:19:05 +01005809 /* Only consider loading future records if the
5810 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005811 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005812 return( 0 );
5813
Hanno Becker5f066e72018-08-16 14:56:31 +01005814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5815
5816 if( rec_epoch != ssl->in_epoch )
5817 {
5818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5819 goto exit;
5820 }
5821
5822 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5823
5824 /* Double-check that the record is not too large */
5825 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5826 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5827 {
5828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5829 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5830 }
5831
5832 memcpy( ssl->in_hdr, rec, rec_len );
5833 ssl->in_left = rec_len;
5834 ssl->next_record_offset = 0;
5835
5836 ssl_free_buffered_record( ssl );
5837
5838exit:
5839 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5840 return( 0 );
5841}
5842
5843static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5844{
5845 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5846 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005847 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005848
5849 /* Don't buffer future records outside handshakes. */
5850 if( hs == NULL )
5851 return( 0 );
5852
5853 /* Only buffer handshake records (we are only interested
5854 * in Finished messages). */
5855 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5856 return( 0 );
5857
5858 /* Don't buffer more than one future epoch record. */
5859 if( hs->buffering.future_record.data != NULL )
5860 return( 0 );
5861
Hanno Becker01315ea2018-08-21 17:22:17 +01005862 /* Don't buffer record if there's not enough buffering space remaining. */
5863 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5864 hs->buffering.total_bytes_buffered ) )
5865 {
5866 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",
5867 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5868 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005869 return( 0 );
5870 }
5871
Hanno Becker5f066e72018-08-16 14:56:31 +01005872 /* Buffer record */
5873 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5874 ssl->in_epoch + 1 ) );
5875 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5876 rec_hdr_len + ssl->in_msglen );
5877
5878 /* ssl_parse_record_header() only considers records
5879 * of the next epoch as candidates for buffering. */
5880 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005881 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005882
5883 hs->buffering.future_record.data =
5884 mbedtls_calloc( 1, hs->buffering.future_record.len );
5885 if( hs->buffering.future_record.data == NULL )
5886 {
5887 /* If we run out of RAM trying to buffer a
5888 * record from the next epoch, just ignore. */
5889 return( 0 );
5890 }
5891
Hanno Becker01315ea2018-08-21 17:22:17 +01005892 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005893
Hanno Becker01315ea2018-08-21 17:22:17 +01005894 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005895 return( 0 );
5896}
5897
5898#endif /* MBEDTLS_SSL_PROTO_DTLS */
5899
Hanno Beckere74d5562018-08-15 14:26:08 +01005900static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005901{
5902 int ret;
5903
Hanno Becker5f066e72018-08-16 14:56:31 +01005904#if defined(MBEDTLS_SSL_PROTO_DTLS)
5905 /* We might have buffered a future record; if so,
5906 * and if the epoch matches now, load it.
5907 * On success, this call will set ssl->in_left to
5908 * the length of the buffered record, so that
5909 * the calls to ssl_fetch_input() below will
5910 * essentially be no-ops. */
5911 ret = ssl_load_buffered_record( ssl );
5912 if( ret != 0 )
5913 return( ret );
5914#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005915
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005916 /* Reset in pointers to default state for TLS/DTLS records,
5917 * assuming no CID and no offset between record content and
5918 * record plaintext. */
5919 ssl_update_in_pointers( ssl );
5920
5921 /* Ensure that we have enough space available for the default form
5922 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5923 * with no space for CIDs counted in). */
5924 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5925 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005927 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005928 return( ret );
5929 }
5930
5931 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005933#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005934 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5935 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005936 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005937 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5938 {
5939 ret = ssl_buffer_future_record( ssl );
5940 if( ret != 0 )
5941 return( ret );
5942
5943 /* Fall through to handling of unexpected records */
5944 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5945 }
5946
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005947 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5948 {
5949 /* Skip unexpected record (but not whole datagram) */
5950 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005951 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005952
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5954 "(header)" ) );
5955 }
5956 else
5957 {
5958 /* Skip invalid record and the rest of the datagram */
5959 ssl->next_record_offset = 0;
5960 ssl->in_left = 0;
5961
5962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5963 "(header)" ) );
5964 }
5965
5966 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005967 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005968 }
5969#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005970 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005971 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005972
5973 /*
5974 * Read and optionally decrypt the message contents
5975 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005976 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005977 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005979 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005980 return( ret );
5981 }
5982
5983 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005984#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005985 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005986 {
Hanno Becker5903de42019-05-03 14:46:38 +01005987 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005988 if( ssl->next_record_offset < ssl->in_left )
5989 {
5990 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5991 }
5992 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005993 else
5994#endif
5995 ssl->in_left = 0;
5996
5997 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005998 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005999#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006000 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006001 {
6002 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006003 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006004 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006005 /* Except when waiting for Finished as a bad mac here
6006 * probably means something went wrong in the handshake
6007 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6008 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6009 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6010 {
6011#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6012 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6013 {
6014 mbedtls_ssl_send_alert_message( ssl,
6015 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6016 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6017 }
6018#endif
6019 return( ret );
6020 }
6021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006023 if( ssl->conf->badmac_limit != 0 &&
6024 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006026 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6027 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006028 }
6029#endif
6030
Hanno Becker4a810fb2017-05-24 16:27:30 +01006031 /* As above, invalid records cause
6032 * dismissal of the whole datagram. */
6033
6034 ssl->next_record_offset = 0;
6035 ssl->in_left = 0;
6036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006038 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006039 }
6040
6041 return( ret );
6042 }
6043 else
6044#endif
6045 {
6046 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006047#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6048 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006050 mbedtls_ssl_send_alert_message( ssl,
6051 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6052 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006053 }
6054#endif
6055 return( ret );
6056 }
6057 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006058
Simon Butcher99000142016-10-13 17:21:01 +01006059 return( 0 );
6060}
6061
6062int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6063{
6064 int ret;
6065
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006066 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006067 * Handle particular types of records
6068 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006069 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006070 {
Simon Butcher99000142016-10-13 17:21:01 +01006071 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6072 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006073 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006074 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006075 }
6076
Hanno Beckere678eaa2018-08-21 14:57:46 +01006077 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006078 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006079 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006080 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006081 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6082 ssl->in_msglen ) );
6083 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006084 }
6085
Hanno Beckere678eaa2018-08-21 14:57:46 +01006086 if( ssl->in_msg[0] != 1 )
6087 {
6088 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6089 ssl->in_msg[0] ) );
6090 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6091 }
6092
6093#if defined(MBEDTLS_SSL_PROTO_DTLS)
6094 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6095 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6096 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6097 {
6098 if( ssl->handshake == NULL )
6099 {
6100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6101 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6102 }
6103
6104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6105 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6106 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006107#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006108 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006110 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006111 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006112 if( ssl->in_msglen != 2 )
6113 {
6114 /* Note: Standard allows for more than one 2 byte alert
6115 to be packed in a single message, but Mbed TLS doesn't
6116 currently support this. */
6117 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6118 ssl->in_msglen ) );
6119 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6120 }
6121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006122 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 ssl->in_msg[0], ssl->in_msg[1] ) );
6124
6125 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006126 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006130 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006131 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006132 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 }
6134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006135 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6136 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006137 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006138 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6139 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006140 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006141
6142#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6143 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6144 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6145 {
Hanno Becker90333da2017-10-10 11:27:13 +01006146 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006147 /* Will be handled when trying to parse ServerHello */
6148 return( 0 );
6149 }
6150#endif
6151
6152#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6153 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6154 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6155 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6156 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6157 {
6158 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6159 /* Will be handled in mbedtls_ssl_parse_certificate() */
6160 return( 0 );
6161 }
6162#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6163
6164 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006165 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006166 }
6167
Hanno Beckerc76c6192017-06-06 10:03:17 +01006168#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006169 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006170 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006171 /* Drop unexpected ApplicationData records,
6172 * except at the beginning of renegotiations */
6173 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6174 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6175#if defined(MBEDTLS_SSL_RENEGOTIATION)
6176 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6177 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006178#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006179 )
6180 {
6181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6182 return( MBEDTLS_ERR_SSL_NON_FATAL );
6183 }
6184
6185 if( ssl->handshake != NULL &&
6186 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6187 {
6188 ssl_handshake_wrapup_free_hs_transform( ssl );
6189 }
6190 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006191#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006192
Paul Bakker5121ce52009-01-03 21:22:43 +00006193 return( 0 );
6194}
6195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006197{
6198 int ret;
6199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006200 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6201 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6202 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006203 {
6204 return( ret );
6205 }
6206
6207 return( 0 );
6208}
6209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006210int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006211 unsigned char level,
6212 unsigned char message )
6213{
6214 int ret;
6215
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006216 if( ssl == NULL || ssl->conf == NULL )
6217 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006220 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006222 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006223 ssl->out_msglen = 2;
6224 ssl->out_msg[0] = level;
6225 ssl->out_msg[1] = message;
6226
Hanno Becker67bc7c32018-08-06 11:33:50 +01006227 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006228 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006229 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006230 return( ret );
6231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006233
6234 return( 0 );
6235}
6236
Hanno Beckerb9d44792019-02-08 07:19:04 +00006237#if defined(MBEDTLS_X509_CRT_PARSE_C)
6238static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6239{
6240#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6241 if( session->peer_cert != NULL )
6242 {
6243 mbedtls_x509_crt_free( session->peer_cert );
6244 mbedtls_free( session->peer_cert );
6245 session->peer_cert = NULL;
6246 }
6247#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6248 if( session->peer_cert_digest != NULL )
6249 {
6250 /* Zeroization is not necessary. */
6251 mbedtls_free( session->peer_cert_digest );
6252 session->peer_cert_digest = NULL;
6253 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6254 session->peer_cert_digest_len = 0;
6255 }
6256#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6257}
6258#endif /* MBEDTLS_X509_CRT_PARSE_C */
6259
Paul Bakker5121ce52009-01-03 21:22:43 +00006260/*
6261 * Handshake functions
6262 */
Hanno Becker21489932019-02-05 13:20:55 +00006263#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006264/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006266{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006267 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6268 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006271
Hanno Becker7177a882019-02-05 13:36:46 +00006272 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006275 ssl->state++;
6276 return( 0 );
6277 }
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6280 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006281}
6282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006283int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006284{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006285 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6286 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006289
Hanno Becker7177a882019-02-05 13:36:46 +00006290 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006291 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006292 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006293 ssl->state++;
6294 return( 0 );
6295 }
6296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6298 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006299}
Gilles Peskinef9828522017-05-03 12:28:43 +02006300
Hanno Becker21489932019-02-05 13:20:55 +00006301#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006302/* Some certificate support -> implement write and parse */
6303
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006305{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006306 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006307 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006309 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6310 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006313
Hanno Becker7177a882019-02-05 13:36:46 +00006314 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006316 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006317 ssl->state++;
6318 return( 0 );
6319 }
6320
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006321#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006322 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006323 {
6324 if( ssl->client_auth == 0 )
6325 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006327 ssl->state++;
6328 return( 0 );
6329 }
6330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006331#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006332 /*
6333 * If using SSLv3 and got no cert, send an Alert message
6334 * (otherwise an empty Certificate message will be sent).
6335 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6337 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006338 {
6339 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6341 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6342 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006345 goto write_msg;
6346 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006347#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349#endif /* MBEDTLS_SSL_CLI_C */
6350#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006351 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006353 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006355 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6356 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006357 }
6358 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006359#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006360
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006361 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006362
6363 /*
6364 * 0 . 0 handshake type
6365 * 1 . 3 handshake length
6366 * 4 . 6 length of all certs
6367 * 7 . 9 length of cert. 1
6368 * 10 . n-1 peer certificate
6369 * n . n+2 length of cert. 2
6370 * n+3 . ... upper level cert, etc.
6371 */
6372 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006373 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006374
Paul Bakker29087132010-03-21 21:03:34 +00006375 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006376 {
6377 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006378 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006381 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006382 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006383 }
6384
6385 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6386 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6387 ssl->out_msg[i + 2] = (unsigned char)( n );
6388
6389 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6390 i += n; crt = crt->next;
6391 }
6392
6393 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6394 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6395 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6396
6397 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006398 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6399 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006401#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006402write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006403#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006404
6405 ssl->state++;
6406
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006407 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006408 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006409 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006410 return( ret );
6411 }
6412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006414
Paul Bakkered27a042013-04-18 22:46:23 +02006415 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006416}
6417
Hanno Becker84879e32019-01-31 07:44:03 +00006418#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006419
6420#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006421static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6422 unsigned char *crt_buf,
6423 size_t crt_buf_len )
6424{
6425 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6426
6427 if( peer_crt == NULL )
6428 return( -1 );
6429
6430 if( peer_crt->raw.len != crt_buf_len )
6431 return( -1 );
6432
Hanno Becker46f34d02019-02-08 14:00:04 +00006433 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006434}
Hanno Becker177475a2019-02-05 17:02:46 +00006435#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6436static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6437 unsigned char *crt_buf,
6438 size_t crt_buf_len )
6439{
6440 int ret;
6441 unsigned char const * const peer_cert_digest =
6442 ssl->session->peer_cert_digest;
6443 mbedtls_md_type_t const peer_cert_digest_type =
6444 ssl->session->peer_cert_digest_type;
6445 mbedtls_md_info_t const * const digest_info =
6446 mbedtls_md_info_from_type( peer_cert_digest_type );
6447 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6448 size_t digest_len;
6449
6450 if( peer_cert_digest == NULL || digest_info == NULL )
6451 return( -1 );
6452
6453 digest_len = mbedtls_md_get_size( digest_info );
6454 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6455 return( -1 );
6456
6457 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6458 if( ret != 0 )
6459 return( -1 );
6460
6461 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6462}
6463#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006464#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006465
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006466/*
6467 * Once the certificate message is read, parse it into a cert chain and
6468 * perform basic checks, but leave actual verification to the caller
6469 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006470static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6471 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006472{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006473 int ret;
6474#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6475 int crt_cnt=0;
6476#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006477 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006478 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006480 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006482 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006483 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6484 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006485 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 }
6487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006488 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6489 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006490 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006491 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006492 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6493 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006494 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 }
6496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006497 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006498
Paul Bakker5121ce52009-01-03 21:22:43 +00006499 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006500 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006501 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006502 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006503
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006504 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006505 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006506 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006507 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006508 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6509 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006510 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006511 }
6512
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006513 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6514 i += 3;
6515
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006516 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006517 while( i < ssl->in_hslen )
6518 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006519 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006520 if ( i + 3 > ssl->in_hslen ) {
6521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006522 mbedtls_ssl_send_alert_message( ssl,
6523 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6524 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006525 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6526 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006527 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6528 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006529 if( ssl->in_msg[i] != 0 )
6530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006532 mbedtls_ssl_send_alert_message( ssl,
6533 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6534 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006535 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006536 }
6537
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006538 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006539 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6540 | (unsigned int) ssl->in_msg[i + 2];
6541 i += 3;
6542
6543 if( n < 128 || i + n > ssl->in_hslen )
6544 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006546 mbedtls_ssl_send_alert_message( ssl,
6547 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6548 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006549 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006550 }
6551
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006552 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006553#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6554 if( crt_cnt++ == 0 &&
6555 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6556 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006557 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006558 /* During client-side renegotiation, check that the server's
6559 * end-CRTs hasn't changed compared to the initial handshake,
6560 * mitigating the triple handshake attack. On success, reuse
6561 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006562 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6563 if( ssl_check_peer_crt_unchanged( ssl,
6564 &ssl->in_msg[i],
6565 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006566 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006567 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6568 mbedtls_ssl_send_alert_message( ssl,
6569 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6570 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6571 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006572 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006573
6574 /* Now we can safely free the original chain. */
6575 ssl_clear_peer_cert( ssl->session );
6576 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006577#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6578
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006579 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006580#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006581 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006582#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006583 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006584 * it in-place from the input buffer instead of making a copy. */
6585 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6586#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006587 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006588 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006589 case 0: /*ok*/
6590 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6591 /* Ignore certificate with an unknown algorithm: maybe a
6592 prior certificate was already trusted. */
6593 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006594
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006595 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6596 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6597 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006598
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006599 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6600 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6601 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006602
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006603 default:
6604 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6605 crt_parse_der_failed:
6606 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6607 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6608 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006609 }
6610
6611 i += n;
6612 }
6613
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006614 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006615 return( 0 );
6616}
6617
Hanno Becker4a55f632019-02-05 12:49:06 +00006618#if defined(MBEDTLS_SSL_SRV_C)
6619static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6620{
6621 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6622 return( -1 );
6623
6624#if defined(MBEDTLS_SSL_PROTO_SSL3)
6625 /*
6626 * Check if the client sent an empty certificate
6627 */
6628 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6629 {
6630 if( ssl->in_msglen == 2 &&
6631 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6632 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6633 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6634 {
6635 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6636 return( 0 );
6637 }
6638
6639 return( -1 );
6640 }
6641#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6642
6643#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6644 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6645 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6646 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6647 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6648 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6649 {
6650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6651 return( 0 );
6652 }
6653
6654 return( -1 );
6655#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6656 MBEDTLS_SSL_PROTO_TLS1_2 */
6657}
6658#endif /* MBEDTLS_SSL_SRV_C */
6659
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006660/* Check if a certificate message is expected.
6661 * Return either
6662 * - SSL_CERTIFICATE_EXPECTED, or
6663 * - SSL_CERTIFICATE_SKIP
6664 * indicating whether a Certificate message is expected or not.
6665 */
6666#define SSL_CERTIFICATE_EXPECTED 0
6667#define SSL_CERTIFICATE_SKIP 1
6668static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6669 int authmode )
6670{
6671 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006672 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006673
6674 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6675 return( SSL_CERTIFICATE_SKIP );
6676
6677#if defined(MBEDTLS_SSL_SRV_C)
6678 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6679 {
6680 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6681 return( SSL_CERTIFICATE_SKIP );
6682
6683 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6684 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006685 ssl->session_negotiate->verify_result =
6686 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6687 return( SSL_CERTIFICATE_SKIP );
6688 }
6689 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006690#else
6691 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006692#endif /* MBEDTLS_SSL_SRV_C */
6693
6694 return( SSL_CERTIFICATE_EXPECTED );
6695}
6696
Hanno Becker68636192019-02-05 14:36:34 +00006697static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6698 int authmode,
6699 mbedtls_x509_crt *chain,
6700 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006701{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006702 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006703 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006704 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006705 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006706
Hanno Becker8927c832019-04-03 12:52:50 +01006707 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6708 void *p_vrfy;
6709
Hanno Becker68636192019-02-05 14:36:34 +00006710 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6711 return( 0 );
6712
Hanno Becker8927c832019-04-03 12:52:50 +01006713 if( ssl->f_vrfy != NULL )
6714 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006715 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006716 f_vrfy = ssl->f_vrfy;
6717 p_vrfy = ssl->p_vrfy;
6718 }
6719 else
6720 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006721 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006722 f_vrfy = ssl->conf->f_vrfy;
6723 p_vrfy = ssl->conf->p_vrfy;
6724 }
6725
Hanno Becker68636192019-02-05 14:36:34 +00006726 /*
6727 * Main check: verify certificate
6728 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006729#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6730 if( ssl->conf->f_ca_cb != NULL )
6731 {
6732 ((void) rs_ctx);
6733 have_ca_chain = 1;
6734
6735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006736 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006737 chain,
6738 ssl->conf->f_ca_cb,
6739 ssl->conf->p_ca_cb,
6740 ssl->conf->cert_profile,
6741 ssl->hostname,
6742 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006743 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006744 }
6745 else
6746#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6747 {
6748 mbedtls_x509_crt *ca_chain;
6749 mbedtls_x509_crl *ca_crl;
6750
6751#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6752 if( ssl->handshake->sni_ca_chain != NULL )
6753 {
6754 ca_chain = ssl->handshake->sni_ca_chain;
6755 ca_crl = ssl->handshake->sni_ca_crl;
6756 }
6757 else
6758#endif
6759 {
6760 ca_chain = ssl->conf->ca_chain;
6761 ca_crl = ssl->conf->ca_crl;
6762 }
6763
6764 if( ca_chain != NULL )
6765 have_ca_chain = 1;
6766
6767 ret = mbedtls_x509_crt_verify_restartable(
6768 chain,
6769 ca_chain, ca_crl,
6770 ssl->conf->cert_profile,
6771 ssl->hostname,
6772 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006773 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006774 }
Hanno Becker68636192019-02-05 14:36:34 +00006775
6776 if( ret != 0 )
6777 {
6778 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6779 }
6780
6781#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6782 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6783 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6784#endif
6785
6786 /*
6787 * Secondary checks: always done, but change 'ret' only if it was 0
6788 */
6789
6790#if defined(MBEDTLS_ECP_C)
6791 {
6792 const mbedtls_pk_context *pk = &chain->pk;
6793
6794 /* If certificate uses an EC key, make sure the curve is OK */
6795 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6796 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6797 {
6798 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6799
6800 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6801 if( ret == 0 )
6802 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6803 }
6804 }
6805#endif /* MBEDTLS_ECP_C */
6806
6807 if( mbedtls_ssl_check_cert_usage( chain,
6808 ciphersuite_info,
6809 ! ssl->conf->endpoint,
6810 &ssl->session_negotiate->verify_result ) != 0 )
6811 {
6812 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6813 if( ret == 0 )
6814 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6815 }
6816
6817 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6818 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6819 * with details encoded in the verification flags. All other kinds
6820 * of error codes, including those from the user provided f_vrfy
6821 * functions, are treated as fatal and lead to a failure of
6822 * ssl_parse_certificate even if verification was optional. */
6823 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6824 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6825 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6826 {
6827 ret = 0;
6828 }
6829
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006830 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006831 {
6832 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6833 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6834 }
6835
6836 if( ret != 0 )
6837 {
6838 uint8_t alert;
6839
6840 /* The certificate may have been rejected for several reasons.
6841 Pick one and send the corresponding alert. Which alert to send
6842 may be a subject of debate in some cases. */
6843 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6844 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6845 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6846 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6847 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6848 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6849 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6850 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6851 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6852 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6853 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6854 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6855 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6856 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6857 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6858 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6859 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6860 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6861 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6862 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6863 else
6864 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6865 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6866 alert );
6867 }
6868
6869#if defined(MBEDTLS_DEBUG_C)
6870 if( ssl->session_negotiate->verify_result != 0 )
6871 {
6872 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6873 ssl->session_negotiate->verify_result ) );
6874 }
6875 else
6876 {
6877 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6878 }
6879#endif /* MBEDTLS_DEBUG_C */
6880
6881 return( ret );
6882}
6883
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006884#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6885static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6886 unsigned char *start, size_t len )
6887{
6888 int ret;
6889 /* Remember digest of the peer's end-CRT. */
6890 ssl->session_negotiate->peer_cert_digest =
6891 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6892 if( ssl->session_negotiate->peer_cert_digest == NULL )
6893 {
6894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6895 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6896 mbedtls_ssl_send_alert_message( ssl,
6897 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6898 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6899
6900 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6901 }
6902
6903 ret = mbedtls_md( mbedtls_md_info_from_type(
6904 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6905 start, len,
6906 ssl->session_negotiate->peer_cert_digest );
6907
6908 ssl->session_negotiate->peer_cert_digest_type =
6909 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6910 ssl->session_negotiate->peer_cert_digest_len =
6911 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6912
6913 return( ret );
6914}
6915
6916static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6917 unsigned char *start, size_t len )
6918{
6919 unsigned char *end = start + len;
6920 int ret;
6921
6922 /* Make a copy of the peer's raw public key. */
6923 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6924 ret = mbedtls_pk_parse_subpubkey( &start, end,
6925 &ssl->handshake->peer_pubkey );
6926 if( ret != 0 )
6927 {
6928 /* We should have parsed the public key before. */
6929 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6930 }
6931
6932 return( 0 );
6933}
6934#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6935
Hanno Becker68636192019-02-05 14:36:34 +00006936int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6937{
6938 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006939 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006940#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6941 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6942 ? ssl->handshake->sni_authmode
6943 : ssl->conf->authmode;
6944#else
6945 const int authmode = ssl->conf->authmode;
6946#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006947 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006948 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006949
6950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6951
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006952 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6953 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006954 {
6955 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006956 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006957 }
6958
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006959#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6960 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006961 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006962 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006963 chain = ssl->handshake->ecrs_peer_cert;
6964 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006965 goto crt_verify;
6966 }
6967#endif
6968
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006969 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006970 {
6971 /* mbedtls_ssl_read_record may have sent an alert already. We
6972 let it decide whether to alert. */
6973 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006974 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006975 }
6976
Hanno Becker4a55f632019-02-05 12:49:06 +00006977#if defined(MBEDTLS_SSL_SRV_C)
6978 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6979 {
6980 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006981
6982 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006983 ret = 0;
6984 else
6985 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006986
Hanno Becker6bdfab22019-02-05 13:11:17 +00006987 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006988 }
6989#endif /* MBEDTLS_SSL_SRV_C */
6990
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006991 /* Clear existing peer CRT structure in case we tried to
6992 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006993 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006994
6995 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6996 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006997 {
6998 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6999 sizeof( mbedtls_x509_crt ) ) );
7000 mbedtls_ssl_send_alert_message( ssl,
7001 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7002 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007003
Hanno Becker3dad3112019-02-05 17:19:52 +00007004 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7005 goto exit;
7006 }
7007 mbedtls_x509_crt_init( chain );
7008
7009 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007010 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007011 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007012
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007013#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7014 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007015 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007016
7017crt_verify:
7018 if( ssl->handshake->ecrs_enabled)
7019 rs_ctx = &ssl->handshake->ecrs_ctx;
7020#endif
7021
Hanno Becker68636192019-02-05 14:36:34 +00007022 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007023 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007024 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007025 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007026
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007027#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007028 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007029 unsigned char *crt_start, *pk_start;
7030 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007031
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007032 /* We parse the CRT chain without copying, so
7033 * these pointers point into the input buffer,
7034 * and are hence still valid after freeing the
7035 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007036
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007037 crt_start = chain->raw.p;
7038 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007039
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007040 pk_start = chain->pk_raw.p;
7041 pk_len = chain->pk_raw.len;
7042
7043 /* Free the CRT structures before computing
7044 * digest and copying the peer's public key. */
7045 mbedtls_x509_crt_free( chain );
7046 mbedtls_free( chain );
7047 chain = NULL;
7048
7049 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007050 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007051 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007052
7053 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7054 if( ret != 0 )
7055 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007056 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007057#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7058 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007059 ssl->session_negotiate->peer_cert = chain;
7060 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007061#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007063 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007064
Hanno Becker6bdfab22019-02-05 13:11:17 +00007065exit:
7066
Hanno Becker3dad3112019-02-05 17:19:52 +00007067 if( ret == 0 )
7068 ssl->state++;
7069
7070#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7071 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7072 {
7073 ssl->handshake->ecrs_peer_cert = chain;
7074 chain = NULL;
7075 }
7076#endif
7077
7078 if( chain != NULL )
7079 {
7080 mbedtls_x509_crt_free( chain );
7081 mbedtls_free( chain );
7082 }
7083
Paul Bakker5121ce52009-01-03 21:22:43 +00007084 return( ret );
7085}
Hanno Becker21489932019-02-05 13:20:55 +00007086#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007089{
7090 int ret;
7091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007092 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007094 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007095 ssl->out_msglen = 1;
7096 ssl->out_msg[0] = 1;
7097
Paul Bakker5121ce52009-01-03 21:22:43 +00007098 ssl->state++;
7099
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007100 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007101 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007102 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007103 return( ret );
7104 }
7105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007106 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007107
7108 return( 0 );
7109}
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007112{
7113 int ret;
7114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007116
Hanno Becker327c93b2018-08-15 13:56:18 +01007117 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 return( ret );
7121 }
7122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007124 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007125 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007126 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7127 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007128 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007129 }
7130
Hanno Beckere678eaa2018-08-21 14:57:46 +01007131 /* CCS records are only accepted if they have length 1 and content '1',
7132 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007133
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007134 /*
7135 * Switch to our negotiated transform and session parameters for inbound
7136 * data.
7137 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007139 ssl->transform_in = ssl->transform_negotiate;
7140 ssl->session_in = ssl->session_negotiate;
7141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007143 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007145#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007146 ssl_dtls_replay_reset( ssl );
7147#endif
7148
7149 /* Increment epoch */
7150 if( ++ssl->in_epoch == 0 )
7151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007152 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007153 /* This is highly unlikely to happen for legitimate reasons, so
7154 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007156 }
7157 }
7158 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007160 memset( ssl->in_ctr, 0, 8 );
7161
Hanno Becker79594fd2019-05-08 09:38:41 +01007162 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007164#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7165 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007167 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007168 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007170 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7171 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007173 }
7174 }
7175#endif
7176
Paul Bakker5121ce52009-01-03 21:22:43 +00007177 ssl->state++;
7178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007180
7181 return( 0 );
7182}
7183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007184void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7185 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007186{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007187 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007189#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7190 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7191 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007192 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007193 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007194#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007195#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7196#if defined(MBEDTLS_SHA512_C)
7197 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007198 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7199 else
7200#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201#if defined(MBEDTLS_SHA256_C)
7202 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007203 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007204 else
7205#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007206#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007207 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007209 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007210 }
Paul Bakker380da532012-04-18 16:10:25 +00007211}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007213void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007214{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007215#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7216 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007217 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7218 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007220#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7221#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007223 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007224 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7225#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007226 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007227#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007228#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007229#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007230#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007231 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007232 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007233#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007234 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007235#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007236#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007237#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007238}
7239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007240static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007241 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007242{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007243#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7244 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007245 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7246 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007247#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007248#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7249#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007250#if defined(MBEDTLS_USE_PSA_CRYPTO)
7251 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7252#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007253 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007254#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007255#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007256#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007257#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007258 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007259#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007260 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007261#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007262#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007263#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007264}
7265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7267 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7268static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007269 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007270{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007271 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7272 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007273}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007274#endif
Paul Bakker380da532012-04-18 16:10:25 +00007275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007276#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7277#if defined(MBEDTLS_SHA256_C)
7278static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007279 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007280{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007281#if defined(MBEDTLS_USE_PSA_CRYPTO)
7282 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7283#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007284 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007285#endif
Paul Bakker380da532012-04-18 16:10:25 +00007286}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007287#endif
Paul Bakker380da532012-04-18 16:10:25 +00007288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007289#if defined(MBEDTLS_SHA512_C)
7290static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007291 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007292{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007293#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007294 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007295#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007296 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007297#endif
Paul Bakker380da532012-04-18 16:10:25 +00007298}
Paul Bakker769075d2012-11-24 11:26:46 +01007299#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007301
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007302#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007303static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007304 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007305{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007306 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007307 mbedtls_md5_context md5;
7308 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007309
Paul Bakker5121ce52009-01-03 21:22:43 +00007310 unsigned char padbuf[48];
7311 unsigned char md5sum[16];
7312 unsigned char sha1sum[20];
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007315 if( !session )
7316 session = ssl->session;
7317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007319
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007320 mbedtls_md5_init( &md5 );
7321 mbedtls_sha1_init( &sha1 );
7322
7323 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7324 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007325
7326 /*
7327 * SSLv3:
7328 * hash =
7329 * MD5( master + pad2 +
7330 * MD5( handshake + sender + master + pad1 ) )
7331 * + SHA1( master + pad2 +
7332 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007333 */
7334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007335#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007336 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7337 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007338#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007340#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007341 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7342 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007343#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007346 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007347
Paul Bakker1ef83d62012-04-11 12:09:53 +00007348 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007349
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007350 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7351 mbedtls_md5_update_ret( &md5, session->master, 48 );
7352 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7353 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007354
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007355 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7356 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7357 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7358 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007359
Paul Bakker1ef83d62012-04-11 12:09:53 +00007360 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007361
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007362 mbedtls_md5_starts_ret( &md5 );
7363 mbedtls_md5_update_ret( &md5, session->master, 48 );
7364 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7365 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7366 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007367
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007368 mbedtls_sha1_starts_ret( &sha1 );
7369 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7370 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7371 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7372 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007373
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007374 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007375
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007376 mbedtls_md5_free( &md5 );
7377 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007378
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007379 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7380 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7381 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007387#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007388static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007389 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007390{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007392 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007393 mbedtls_md5_context md5;
7394 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007395 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007398 if( !session )
7399 session = ssl->session;
7400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007401 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007402
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007403 mbedtls_md5_init( &md5 );
7404 mbedtls_sha1_init( &sha1 );
7405
7406 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7407 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007408
Paul Bakker1ef83d62012-04-11 12:09:53 +00007409 /*
7410 * TLSv1:
7411 * hash = PRF( master, finished_label,
7412 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7413 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007415#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007416 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7417 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007418#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007420#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007421 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7422 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007423#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007425 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007426 ? "client finished"
7427 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007428
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007429 mbedtls_md5_finish_ret( &md5, padbuf );
7430 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007431
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007432 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007433 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007434
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007435 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007436
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007437 mbedtls_md5_free( &md5 );
7438 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007439
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007440 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007444#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007445
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007446#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7447#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007448static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007449 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007450{
7451 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007452 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007453 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007454#if defined(MBEDTLS_USE_PSA_CRYPTO)
7455 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007456 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007457 psa_status_t status;
7458#else
7459 mbedtls_sha256_context sha256;
7460#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007462 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007463 if( !session )
7464 session = ssl->session;
7465
Andrzej Kurekeb342242019-01-29 09:14:33 -05007466 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7467 ? "client finished"
7468 : "server finished";
7469
7470#if defined(MBEDTLS_USE_PSA_CRYPTO)
7471 sha256_psa = psa_hash_operation_init();
7472
7473 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7474
7475 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7476 if( status != PSA_SUCCESS )
7477 {
7478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7479 return;
7480 }
7481
7482 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7483 if( status != PSA_SUCCESS )
7484 {
7485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7486 return;
7487 }
7488 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7489#else
7490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007491 mbedtls_sha256_init( &sha256 );
7492
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007493 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007494
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007495 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007496
7497 /*
7498 * TLSv1.2:
7499 * hash = PRF( master, finished_label,
7500 * Hash( handshake ) )[0.11]
7501 */
7502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007503#if !defined(MBEDTLS_SHA256_ALT)
7504 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007505 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007506#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007508 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007509 mbedtls_sha256_free( &sha256 );
7510#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007511
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007512 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007513 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007516
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007517 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007520}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007523#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007524static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007525 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007526{
7527 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007528 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007529 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007530#if defined(MBEDTLS_USE_PSA_CRYPTO)
7531 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007532 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007533 psa_status_t status;
7534#else
7535 mbedtls_sha512_context sha512;
7536#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007538 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007539 if( !session )
7540 session = ssl->session;
7541
Andrzej Kurekeb342242019-01-29 09:14:33 -05007542 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7543 ? "client finished"
7544 : "server finished";
7545
7546#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007547 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007548
7549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7550
Andrzej Kurek972fba52019-01-30 03:29:12 -05007551 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007552 if( status != PSA_SUCCESS )
7553 {
7554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7555 return;
7556 }
7557
Andrzej Kurek972fba52019-01-30 03:29:12 -05007558 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007559 if( status != PSA_SUCCESS )
7560 {
7561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7562 return;
7563 }
7564 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7565#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007566 mbedtls_sha512_init( &sha512 );
7567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007568 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007569
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007570 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007571
7572 /*
7573 * TLSv1.2:
7574 * hash = PRF( master, finished_label,
7575 * Hash( handshake ) )[0.11]
7576 */
7577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007578#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007579 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7580 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007581#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007582
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007583 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007584 mbedtls_sha512_free( &sha512 );
7585#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007586
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007587 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007588 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007591
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007592 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007595}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007596#endif /* MBEDTLS_SHA512_C */
7597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007598
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007599static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007600{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007601 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007602
7603 /*
7604 * Free our handshake params
7605 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007606 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007607 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007608 ssl->handshake = NULL;
7609
7610 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007611 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007612 */
7613 if( ssl->transform )
7614 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007615 mbedtls_ssl_transform_free( ssl->transform );
7616 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007617 }
7618 ssl->transform = ssl->transform_negotiate;
7619 ssl->transform_negotiate = NULL;
7620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007621 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007622}
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007625{
7626 int resume = ssl->handshake->resume;
7627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007628 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007630#if defined(MBEDTLS_SSL_RENEGOTIATION)
7631 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007632 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007633 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007634 ssl->renego_records_seen = 0;
7635 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007636#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007637
7638 /*
7639 * Free the previous session and switch in the current one
7640 */
Paul Bakker0a597072012-09-25 21:55:46 +00007641 if( ssl->session )
7642 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007643#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007644 /* RFC 7366 3.1: keep the EtM state */
7645 ssl->session_negotiate->encrypt_then_mac =
7646 ssl->session->encrypt_then_mac;
7647#endif
7648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007649 mbedtls_ssl_session_free( ssl->session );
7650 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007651 }
7652 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007653 ssl->session_negotiate = NULL;
7654
Paul Bakker0a597072012-09-25 21:55:46 +00007655 /*
7656 * Add cache entry
7657 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007658 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007659 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007660 resume == 0 )
7661 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007662 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007663 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007664 }
Paul Bakker0a597072012-09-25 21:55:46 +00007665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007667 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007668 ssl->handshake->flight != NULL )
7669 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007670 /* Cancel handshake timer */
7671 ssl_set_timer( ssl, 0 );
7672
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007673 /* Keep last flight around in case we need to resend it:
7674 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007675 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007676 }
7677 else
7678#endif
7679 ssl_handshake_wrapup_free_hs_transform( ssl );
7680
Paul Bakker48916f92012-09-16 19:57:18 +00007681 ssl->state++;
7682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007683 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007684}
7685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007687{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007688 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007691
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007692 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007693
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007694 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007695
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007696 /*
7697 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7698 * may define some other value. Currently (early 2016), no defined
7699 * ciphersuite does this (and this is unlikely to change as activity has
7700 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7701 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007702 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007704#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007705 ssl->verify_data_len = hash_len;
7706 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007707#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007708
Paul Bakker5121ce52009-01-03 21:22:43 +00007709 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007710 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7711 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007712
7713 /*
7714 * In case of session resuming, invert the client and server
7715 * ChangeCipherSpec messages order.
7716 */
Paul Bakker0a597072012-09-25 21:55:46 +00007717 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007718 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007722#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007723#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007724 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007726#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007727 }
7728 else
7729 ssl->state++;
7730
Paul Bakker48916f92012-09-16 19:57:18 +00007731 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007732 * Switch to our negotiated transform and session parameters for outbound
7733 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007734 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007735 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007739 {
7740 unsigned char i;
7741
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007742 /* Remember current epoch settings for resending */
7743 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007744 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007745
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007746 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007747 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007748
7749 /* Increment epoch */
7750 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007751 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007752 break;
7753
7754 /* The loop goes to its end iff the counter is wrapping */
7755 if( i == 0 )
7756 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007757 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7758 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007759 }
7760 }
7761 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007763 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007764
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007765 ssl->transform_out = ssl->transform_negotiate;
7766 ssl->session_out = ssl->session_negotiate;
7767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007768#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7769 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007771 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007773 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7774 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007775 }
7776 }
7777#endif
7778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007779#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007780 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007781 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007782#endif
7783
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007784 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007785 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007786 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007787 return( ret );
7788 }
7789
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007790#if defined(MBEDTLS_SSL_PROTO_DTLS)
7791 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7792 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7793 {
7794 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7795 return( ret );
7796 }
7797#endif
7798
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007799 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007800
7801 return( 0 );
7802}
7803
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007804#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007805#define SSL_MAX_HASH_LEN 36
7806#else
7807#define SSL_MAX_HASH_LEN 12
7808#endif
7809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007810int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007811{
Paul Bakker23986e52011-04-24 08:57:21 +00007812 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007813 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007814 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007815
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007816 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007817
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007818 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819
Hanno Becker327c93b2018-08-15 13:56:18 +01007820 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007823 return( ret );
7824 }
7825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007826 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007827 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007829 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7830 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007832 }
7833
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007834 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007835#if defined(MBEDTLS_SSL_PROTO_SSL3)
7836 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007837 hash_len = 36;
7838 else
7839#endif
7840 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007841
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007842 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7843 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007844 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007845 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007846 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7847 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007848 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 }
7850
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007851 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007852 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007855 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7856 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007858 }
7859
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007860#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007861 ssl->verify_data_len = hash_len;
7862 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007863#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007864
Paul Bakker0a597072012-09-25 21:55:46 +00007865 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007866 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007870#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007871#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007872 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007873 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007874#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007875 }
7876 else
7877 ssl->state++;
7878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007879#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007880 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007881 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007882#endif
7883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007884 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007885
7886 return( 0 );
7887}
7888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007890{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007891 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7894 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7895 mbedtls_md5_init( &handshake->fin_md5 );
7896 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007897 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7898 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007899#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007900#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7901#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007902#if defined(MBEDTLS_USE_PSA_CRYPTO)
7903 handshake->fin_sha256_psa = psa_hash_operation_init();
7904 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7905#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007907 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007908#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007909#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007910#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007911#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007912 handshake->fin_sha384_psa = psa_hash_operation_init();
7913 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007914#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007916 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007917#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007918#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007919#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007920
7921 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007922
7923#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7924 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7925 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7926#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007928#if defined(MBEDTLS_DHM_C)
7929 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007930#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007931#if defined(MBEDTLS_ECDH_C)
7932 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007933#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007934#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007935 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007936#if defined(MBEDTLS_SSL_CLI_C)
7937 handshake->ecjpake_cache = NULL;
7938 handshake->ecjpake_cache_len = 0;
7939#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007940#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007941
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007942#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007943 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007944#endif
7945
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007946#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7947 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7948#endif
Hanno Becker75173122019-02-06 16:18:31 +00007949
7950#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7951 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7952 mbedtls_pk_init( &handshake->peer_pubkey );
7953#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007954}
7955
Hanno Beckera18d1322018-01-03 14:27:32 +00007956void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007957{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007958 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007959
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7961 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007962
Hanno Beckerd56ed242018-01-03 15:32:51 +00007963#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007964 mbedtls_md_init( &transform->md_ctx_enc );
7965 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007966#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007967}
7968
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007969void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007970{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007971 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972}
7973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007975{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007976 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007977 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007978 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007979 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007980 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007981 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007982 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007983
7984 /*
7985 * Either the pointers are now NULL or cleared properly and can be freed.
7986 * Now allocate missing structures.
7987 */
7988 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007989 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007990 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007991 }
Paul Bakker48916f92012-09-16 19:57:18 +00007992
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007993 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007994 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007995 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007996 }
Paul Bakker48916f92012-09-16 19:57:18 +00007997
Paul Bakker82788fb2014-10-20 13:59:19 +02007998 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007999 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008000 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008001 }
Paul Bakker48916f92012-09-16 19:57:18 +00008002
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008003 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008004 if( ssl->handshake == NULL ||
8005 ssl->transform_negotiate == NULL ||
8006 ssl->session_negotiate == NULL )
8007 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008010 mbedtls_free( ssl->handshake );
8011 mbedtls_free( ssl->transform_negotiate );
8012 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008013
8014 ssl->handshake = NULL;
8015 ssl->transform_negotiate = NULL;
8016 ssl->session_negotiate = NULL;
8017
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008018 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008019 }
8020
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008021 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008023 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008024 ssl_handshake_params_init( ssl->handshake );
8025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008027 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8028 {
8029 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008030
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008031 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8032 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8033 else
8034 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008035
8036 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008037 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008038#endif
8039
Paul Bakker48916f92012-09-16 19:57:18 +00008040 return( 0 );
8041}
8042
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008043#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008044/* Dummy cookie callbacks for defaults */
8045static int ssl_cookie_write_dummy( void *ctx,
8046 unsigned char **p, unsigned char *end,
8047 const unsigned char *cli_id, size_t cli_id_len )
8048{
8049 ((void) ctx);
8050 ((void) p);
8051 ((void) end);
8052 ((void) cli_id);
8053 ((void) cli_id_len);
8054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008055 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008056}
8057
8058static int ssl_cookie_check_dummy( void *ctx,
8059 const unsigned char *cookie, size_t cookie_len,
8060 const unsigned char *cli_id, size_t cli_id_len )
8061{
8062 ((void) ctx);
8063 ((void) cookie);
8064 ((void) cookie_len);
8065 ((void) cli_id);
8066 ((void) cli_id_len);
8067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008068 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008069}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008070#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008071
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008072/* Once ssl->out_hdr as the address of the beginning of the
8073 * next outgoing record is set, deduce the other pointers.
8074 *
8075 * Note: For TLS, we save the implicit record sequence number
8076 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8077 * and the caller has to make sure there's space for this.
8078 */
8079
8080static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8081 mbedtls_ssl_transform *transform )
8082{
8083#if defined(MBEDTLS_SSL_PROTO_DTLS)
8084 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8085 {
8086 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008087#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008088 ssl->out_cid = ssl->out_ctr + 8;
8089 ssl->out_len = ssl->out_cid;
8090 if( transform != NULL )
8091 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008092#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008093 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008094#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008095 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008096 }
8097 else
8098#endif
8099 {
8100 ssl->out_ctr = ssl->out_hdr - 8;
8101 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008102#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008103 ssl->out_cid = ssl->out_len;
8104#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008105 ssl->out_iv = ssl->out_hdr + 5;
8106 }
8107
8108 /* Adjust out_msg to make space for explicit IV, if used. */
8109 if( transform != NULL &&
8110 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8111 {
8112 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8113 }
8114 else
8115 ssl->out_msg = ssl->out_iv;
8116}
8117
8118/* Once ssl->in_hdr as the address of the beginning of the
8119 * next incoming record is set, deduce the other pointers.
8120 *
8121 * Note: For TLS, we save the implicit record sequence number
8122 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8123 * and the caller has to make sure there's space for this.
8124 */
8125
Hanno Becker79594fd2019-05-08 09:38:41 +01008126static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008127{
Hanno Becker79594fd2019-05-08 09:38:41 +01008128 /* This function sets the pointers to match the case
8129 * of unprotected TLS/DTLS records, with both ssl->in_iv
8130 * and ssl->in_msg pointing to the beginning of the record
8131 * content.
8132 *
8133 * When decrypting a protected record, ssl->in_msg
8134 * will be shifted to point to the beginning of the
8135 * record plaintext.
8136 */
8137
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008138#if defined(MBEDTLS_SSL_PROTO_DTLS)
8139 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8140 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008141 /* This sets the header pointers to match records
8142 * without CID. When we receive a record containing
8143 * a CID, the fields are shifted accordingly in
8144 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008145 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008146#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008147 ssl->in_cid = ssl->in_ctr + 8;
8148 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008149#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008150 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008151#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008152 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008153 }
8154 else
8155#endif
8156 {
8157 ssl->in_ctr = ssl->in_hdr - 8;
8158 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008160 ssl->in_cid = ssl->in_len;
8161#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008162 ssl->in_iv = ssl->in_hdr + 5;
8163 }
8164
Hanno Becker79594fd2019-05-08 09:38:41 +01008165 /* This will be adjusted at record decryption time. */
8166 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008167}
8168
Paul Bakker5121ce52009-01-03 21:22:43 +00008169/*
8170 * Initialize an SSL context
8171 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008172void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8173{
8174 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8175}
8176
8177/*
8178 * Setup an SSL context
8179 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008180
8181static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8182{
8183 /* Set the incoming and outgoing record pointers. */
8184#if defined(MBEDTLS_SSL_PROTO_DTLS)
8185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8186 {
8187 ssl->out_hdr = ssl->out_buf;
8188 ssl->in_hdr = ssl->in_buf;
8189 }
8190 else
8191#endif /* MBEDTLS_SSL_PROTO_DTLS */
8192 {
8193 ssl->out_hdr = ssl->out_buf + 8;
8194 ssl->in_hdr = ssl->in_buf + 8;
8195 }
8196
8197 /* Derive other internal pointers. */
8198 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008199 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008200}
8201
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008202int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008203 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008204{
Paul Bakker48916f92012-09-16 19:57:18 +00008205 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008206
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008207 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008208
8209 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008210 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008211 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008212
8213 /* Set to NULL in case of an error condition */
8214 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008215
Angus Grattond8213d02016-05-25 20:56:48 +10008216 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8217 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008218 {
Angus Grattond8213d02016-05-25 20:56:48 +10008219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008220 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008221 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008222 }
8223
8224 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8225 if( ssl->out_buf == NULL )
8226 {
8227 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008228 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008229 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008230 }
8231
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008232 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008233
Paul Bakker48916f92012-09-16 19:57:18 +00008234 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008235 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008236
8237 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008238
8239error:
8240 mbedtls_free( ssl->in_buf );
8241 mbedtls_free( ssl->out_buf );
8242
8243 ssl->conf = NULL;
8244
8245 ssl->in_buf = NULL;
8246 ssl->out_buf = NULL;
8247
8248 ssl->in_hdr = NULL;
8249 ssl->in_ctr = NULL;
8250 ssl->in_len = NULL;
8251 ssl->in_iv = NULL;
8252 ssl->in_msg = NULL;
8253
8254 ssl->out_hdr = NULL;
8255 ssl->out_ctr = NULL;
8256 ssl->out_len = NULL;
8257 ssl->out_iv = NULL;
8258 ssl->out_msg = NULL;
8259
k-stachowiak9f7798e2018-07-31 16:52:32 +02008260 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008261}
8262
8263/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008264 * Reset an initialized and used SSL context for re-use while retaining
8265 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008266 *
8267 * If partial is non-zero, keep data in the input buffer and client ID.
8268 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008269 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008270static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008271{
Paul Bakker48916f92012-09-16 19:57:18 +00008272 int ret;
8273
Hanno Becker7e772132018-08-10 12:38:21 +01008274#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8275 !defined(MBEDTLS_SSL_SRV_C)
8276 ((void) partial);
8277#endif
8278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008280
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008281 /* Cancel any possibly running timer */
8282 ssl_set_timer( ssl, 0 );
8283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008284#if defined(MBEDTLS_SSL_RENEGOTIATION)
8285 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008286 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008287
8288 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008289 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8290 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008291#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008292 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008293
Paul Bakker7eb013f2011-10-06 12:37:39 +00008294 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008295 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008296
8297 ssl->in_msgtype = 0;
8298 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008300 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008301 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008302#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008303#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008304 ssl_dtls_replay_reset( ssl );
8305#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008306
8307 ssl->in_hslen = 0;
8308 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008309
8310 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008311
8312 ssl->out_msgtype = 0;
8313 ssl->out_msglen = 0;
8314 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008315#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8316 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008317 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008318#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008319
Hanno Becker19859472018-08-06 09:40:20 +01008320 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8321
Paul Bakker48916f92012-09-16 19:57:18 +00008322 ssl->transform_in = NULL;
8323 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008324
Hanno Becker78640902018-08-13 16:35:15 +01008325 ssl->session_in = NULL;
8326 ssl->session_out = NULL;
8327
Angus Grattond8213d02016-05-25 20:56:48 +10008328 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008329
8330#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008331 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008332#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8333 {
8334 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008335 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008336 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008338#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8339 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008340 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008341 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8342 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008343 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008344 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8345 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008346 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008347 }
8348#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008349
Paul Bakker48916f92012-09-16 19:57:18 +00008350 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008352 mbedtls_ssl_transform_free( ssl->transform );
8353 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008354 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008355 }
Paul Bakker48916f92012-09-16 19:57:18 +00008356
Paul Bakkerc0463502013-02-14 11:19:38 +01008357 if( ssl->session )
8358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008359 mbedtls_ssl_session_free( ssl->session );
8360 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008361 ssl->session = NULL;
8362 }
8363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008364#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008365 ssl->alpn_chosen = NULL;
8366#endif
8367
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008368#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008369#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008370 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008371#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008372 {
8373 mbedtls_free( ssl->cli_id );
8374 ssl->cli_id = NULL;
8375 ssl->cli_id_len = 0;
8376 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008377#endif
8378
Paul Bakker48916f92012-09-16 19:57:18 +00008379 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8380 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008381
8382 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008383}
8384
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008385/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008386 * Reset an initialized and used SSL context for re-use while retaining
8387 * all application-set variables, function pointers and data.
8388 */
8389int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8390{
8391 return( ssl_session_reset_int( ssl, 0 ) );
8392}
8393
8394/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008395 * SSL set accessors
8396 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008397void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008398{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008399 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008400}
8401
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008402void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008403{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008404 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008405}
8406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008407#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008408void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008409{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008410 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008411}
8412#endif
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008415void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008416{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008417 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008418}
8419#endif
8420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008421#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008422
Hanno Becker1841b0a2018-08-24 11:13:57 +01008423void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8424 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008425{
8426 ssl->disable_datagram_packing = !allow_packing;
8427}
8428
8429void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8430 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008431{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008432 conf->hs_timeout_min = min;
8433 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008434}
8435#endif
8436
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008437void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008438{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008439 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008440}
8441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008443void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008444 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008445 void *p_vrfy )
8446{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008447 conf->f_vrfy = f_vrfy;
8448 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008450#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008451
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008452void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008453 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008454 void *p_rng )
8455{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008456 conf->f_rng = f_rng;
8457 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008458}
8459
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008460void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008461 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008462 void *p_dbg )
8463{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008464 conf->f_dbg = f_dbg;
8465 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008466}
8467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008468void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008469 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008470 mbedtls_ssl_send_t *f_send,
8471 mbedtls_ssl_recv_t *f_recv,
8472 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008473{
8474 ssl->p_bio = p_bio;
8475 ssl->f_send = f_send;
8476 ssl->f_recv = f_recv;
8477 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008478}
8479
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008480#if defined(MBEDTLS_SSL_PROTO_DTLS)
8481void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8482{
8483 ssl->mtu = mtu;
8484}
8485#endif
8486
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008487void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008488{
8489 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008490}
8491
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008492void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8493 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008494 mbedtls_ssl_set_timer_t *f_set_timer,
8495 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008496{
8497 ssl->p_timer = p_timer;
8498 ssl->f_set_timer = f_set_timer;
8499 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008500
8501 /* Make sure we start with no timer running */
8502 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008503}
8504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008505#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008506void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008507 void *p_cache,
8508 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8509 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008510{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008511 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008512 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008513 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008515#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008517#if defined(MBEDTLS_SSL_CLI_C)
8518int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008519{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008520 int ret;
8521
8522 if( ssl == NULL ||
8523 session == NULL ||
8524 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008525 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008528 }
8529
Hanno Becker52055ae2019-02-06 14:30:46 +00008530 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8531 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008532 return( ret );
8533
Paul Bakker0a597072012-09-25 21:55:46 +00008534 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008535
8536 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008537}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008538#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008539
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008540void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008541 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008542{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008543 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8544 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8545 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8546 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008547}
8548
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008549void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008550 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008551 int major, int minor )
8552{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008553 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008554 return;
8555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008557 return;
8558
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008559 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008560}
8561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008562#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008563void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008564 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008565{
8566 conf->cert_profile = profile;
8567}
8568
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008569/* Append a new keycert entry to a (possibly empty) list */
8570static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8571 mbedtls_x509_crt *cert,
8572 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008573{
niisato8ee24222018-06-25 19:05:48 +09008574 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008575
niisato8ee24222018-06-25 19:05:48 +09008576 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8577 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008578 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008579
niisato8ee24222018-06-25 19:05:48 +09008580 new_cert->cert = cert;
8581 new_cert->key = key;
8582 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008583
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008584 /* Update head is the list was null, else add to the end */
8585 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008586 {
niisato8ee24222018-06-25 19:05:48 +09008587 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008588 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008589 else
8590 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008591 mbedtls_ssl_key_cert *cur = *head;
8592 while( cur->next != NULL )
8593 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008594 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008595 }
8596
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008597 return( 0 );
8598}
8599
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008600int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008601 mbedtls_x509_crt *own_cert,
8602 mbedtls_pk_context *pk_key )
8603{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008604 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008605}
8606
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008607void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008608 mbedtls_x509_crt *ca_chain,
8609 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008610{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008611 conf->ca_chain = ca_chain;
8612 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008613
8614#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8615 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8616 * cannot be used together. */
8617 conf->f_ca_cb = NULL;
8618 conf->p_ca_cb = NULL;
8619#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008620}
Hanno Becker5adaad92019-03-27 16:54:37 +00008621
8622#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8623void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008624 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008625 void *p_ca_cb )
8626{
8627 conf->f_ca_cb = f_ca_cb;
8628 conf->p_ca_cb = p_ca_cb;
8629
8630 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8631 * cannot be used together. */
8632 conf->ca_chain = NULL;
8633 conf->ca_crl = NULL;
8634}
8635#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008636#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008637
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008638#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8639int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8640 mbedtls_x509_crt *own_cert,
8641 mbedtls_pk_context *pk_key )
8642{
8643 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8644 own_cert, pk_key ) );
8645}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008646
8647void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8648 mbedtls_x509_crt *ca_chain,
8649 mbedtls_x509_crl *ca_crl )
8650{
8651 ssl->handshake->sni_ca_chain = ca_chain;
8652 ssl->handshake->sni_ca_crl = ca_crl;
8653}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008654
8655void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8656 int authmode )
8657{
8658 ssl->handshake->sni_authmode = authmode;
8659}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008660#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8661
Hanno Becker8927c832019-04-03 12:52:50 +01008662#if defined(MBEDTLS_X509_CRT_PARSE_C)
8663void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8664 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8665 void *p_vrfy )
8666{
8667 ssl->f_vrfy = f_vrfy;
8668 ssl->p_vrfy = p_vrfy;
8669}
8670#endif
8671
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008672#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008673/*
8674 * Set EC J-PAKE password for current handshake
8675 */
8676int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8677 const unsigned char *pw,
8678 size_t pw_len )
8679{
8680 mbedtls_ecjpake_role role;
8681
Janos Follath8eb64132016-06-03 15:40:57 +01008682 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008683 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8684
8685 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8686 role = MBEDTLS_ECJPAKE_SERVER;
8687 else
8688 role = MBEDTLS_ECJPAKE_CLIENT;
8689
8690 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8691 role,
8692 MBEDTLS_MD_SHA256,
8693 MBEDTLS_ECP_DP_SECP256R1,
8694 pw, pw_len ) );
8695}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008696#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008698#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008699
8700static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8701{
8702 /* Remove reference to existing PSK, if any. */
8703#if defined(MBEDTLS_USE_PSA_CRYPTO)
8704 if( conf->psk_opaque != 0 )
8705 {
8706 /* The maintenance of the PSK key slot is the
8707 * user's responsibility. */
8708 conf->psk_opaque = 0;
8709 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008710 /* This and the following branch should never
8711 * be taken simultaenously as we maintain the
8712 * invariant that raw and opaque PSKs are never
8713 * configured simultaneously. As a safeguard,
8714 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008715#endif /* MBEDTLS_USE_PSA_CRYPTO */
8716 if( conf->psk != NULL )
8717 {
8718 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8719
8720 mbedtls_free( conf->psk );
8721 conf->psk = NULL;
8722 conf->psk_len = 0;
8723 }
8724
8725 /* Remove reference to PSK identity, if any. */
8726 if( conf->psk_identity != NULL )
8727 {
8728 mbedtls_free( conf->psk_identity );
8729 conf->psk_identity = NULL;
8730 conf->psk_identity_len = 0;
8731 }
8732}
8733
Hanno Becker7390c712018-11-15 13:33:04 +00008734/* This function assumes that PSK identity in the SSL config is unset.
8735 * It checks that the provided identity is well-formed and attempts
8736 * to make a copy of it in the SSL config.
8737 * On failure, the PSK identity in the config remains unset. */
8738static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8739 unsigned char const *psk_identity,
8740 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008741{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008742 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008743 if( psk_identity == NULL ||
8744 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008745 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008746 {
8747 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8748 }
8749
Hanno Becker7390c712018-11-15 13:33:04 +00008750 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8751 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008752 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008753
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008754 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008755 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008756
8757 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008758}
8759
Hanno Becker7390c712018-11-15 13:33:04 +00008760int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8761 const unsigned char *psk, size_t psk_len,
8762 const unsigned char *psk_identity, size_t psk_identity_len )
8763{
8764 int ret;
8765 /* Remove opaque/raw PSK + PSK Identity */
8766 ssl_conf_remove_psk( conf );
8767
8768 /* Check and set raw PSK */
8769 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8770 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8771 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8772 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8773 conf->psk_len = psk_len;
8774 memcpy( conf->psk, psk, conf->psk_len );
8775
8776 /* Check and set PSK Identity */
8777 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8778 if( ret != 0 )
8779 ssl_conf_remove_psk( conf );
8780
8781 return( ret );
8782}
8783
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008784static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8785{
8786#if defined(MBEDTLS_USE_PSA_CRYPTO)
8787 if( ssl->handshake->psk_opaque != 0 )
8788 {
8789 ssl->handshake->psk_opaque = 0;
8790 }
8791 else
8792#endif /* MBEDTLS_USE_PSA_CRYPTO */
8793 if( ssl->handshake->psk != NULL )
8794 {
8795 mbedtls_platform_zeroize( ssl->handshake->psk,
8796 ssl->handshake->psk_len );
8797 mbedtls_free( ssl->handshake->psk );
8798 ssl->handshake->psk_len = 0;
8799 }
8800}
8801
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008802int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8803 const unsigned char *psk, size_t psk_len )
8804{
8805 if( psk == NULL || ssl->handshake == NULL )
8806 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8807
8808 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8809 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8810
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008811 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008812
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008813 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008814 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008815
8816 ssl->handshake->psk_len = psk_len;
8817 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8818
8819 return( 0 );
8820}
8821
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008822#if defined(MBEDTLS_USE_PSA_CRYPTO)
8823int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008824 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008825 const unsigned char *psk_identity,
8826 size_t psk_identity_len )
8827{
Hanno Becker7390c712018-11-15 13:33:04 +00008828 int ret;
8829 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008830 ssl_conf_remove_psk( conf );
8831
Hanno Becker7390c712018-11-15 13:33:04 +00008832 /* Check and set opaque PSK */
8833 if( psk_slot == 0 )
8834 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008835 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008836
8837 /* Check and set PSK Identity */
8838 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8839 psk_identity_len );
8840 if( ret != 0 )
8841 ssl_conf_remove_psk( conf );
8842
8843 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008844}
8845
8846int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008847 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008848{
8849 if( psk_slot == 0 || ssl->handshake == NULL )
8850 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8851
8852 ssl_remove_psk( ssl );
8853 ssl->handshake->psk_opaque = psk_slot;
8854 return( 0 );
8855}
8856#endif /* MBEDTLS_USE_PSA_CRYPTO */
8857
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008858void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008860 size_t),
8861 void *p_psk )
8862{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008863 conf->f_psk = f_psk;
8864 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008867
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008868#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008869
8870#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008871int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008872{
8873 int ret;
8874
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008875 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8876 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8877 {
8878 mbedtls_mpi_free( &conf->dhm_P );
8879 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008880 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008881 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008882
8883 return( 0 );
8884}
Hanno Becker470a8c42017-10-04 15:28:46 +01008885#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008886
Hanno Beckera90658f2017-10-04 15:29:08 +01008887int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8888 const unsigned char *dhm_P, size_t P_len,
8889 const unsigned char *dhm_G, size_t G_len )
8890{
8891 int ret;
8892
8893 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8894 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8895 {
8896 mbedtls_mpi_free( &conf->dhm_P );
8897 mbedtls_mpi_free( &conf->dhm_G );
8898 return( ret );
8899 }
8900
8901 return( 0 );
8902}
Paul Bakker5121ce52009-01-03 21:22:43 +00008903
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008904int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008905{
8906 int ret;
8907
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008908 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8909 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8910 {
8911 mbedtls_mpi_free( &conf->dhm_P );
8912 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008913 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008914 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008915
8916 return( 0 );
8917}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008918#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008919
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008920#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8921/*
8922 * Set the minimum length for Diffie-Hellman parameters
8923 */
8924void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8925 unsigned int bitlen )
8926{
8927 conf->dhm_min_bitlen = bitlen;
8928}
8929#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8930
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008931#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008932/*
8933 * Set allowed/preferred hashes for handshake signatures
8934 */
8935void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8936 const int *hashes )
8937{
8938 conf->sig_hashes = hashes;
8939}
Hanno Becker947194e2017-04-07 13:25:49 +01008940#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008941
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008942#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008943/*
8944 * Set the allowed elliptic curves
8945 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008946void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008947 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008948{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008949 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008950}
Hanno Becker947194e2017-04-07 13:25:49 +01008951#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008952
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008953#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008955{
Hanno Becker947194e2017-04-07 13:25:49 +01008956 /* Initialize to suppress unnecessary compiler warning */
8957 size_t hostname_len = 0;
8958
8959 /* Check if new hostname is valid before
8960 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008961 if( hostname != NULL )
8962 {
8963 hostname_len = strlen( hostname );
8964
8965 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8966 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8967 }
8968
8969 /* Now it's clear that we will overwrite the old hostname,
8970 * so we can free it safely */
8971
8972 if( ssl->hostname != NULL )
8973 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008974 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008975 mbedtls_free( ssl->hostname );
8976 }
8977
8978 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008979
Paul Bakker5121ce52009-01-03 21:22:43 +00008980 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008981 {
8982 ssl->hostname = NULL;
8983 }
8984 else
8985 {
8986 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008987 if( ssl->hostname == NULL )
8988 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008989
Hanno Becker947194e2017-04-07 13:25:49 +01008990 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008991
Hanno Becker947194e2017-04-07 13:25:49 +01008992 ssl->hostname[hostname_len] = '\0';
8993 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008994
8995 return( 0 );
8996}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008997#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008998
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008999#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009000void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009001 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009002 const unsigned char *, size_t),
9003 void *p_sni )
9004{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009005 conf->f_sni = f_sni;
9006 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009007}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009008#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009010#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009011int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009012{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009013 size_t cur_len, tot_len;
9014 const char **p;
9015
9016 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009017 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9018 * MUST NOT be truncated."
9019 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009020 */
9021 tot_len = 0;
9022 for( p = protos; *p != NULL; p++ )
9023 {
9024 cur_len = strlen( *p );
9025 tot_len += cur_len;
9026
9027 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009028 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009029 }
9030
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009031 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009032
9033 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009034}
9035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009037{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009038 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009039}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009040#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009041
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009042void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009043{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009044 conf->max_major_ver = major;
9045 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009046}
9047
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009048void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009049{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009050 conf->min_major_ver = major;
9051 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009052}
9053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009054#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009055void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009056{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009057 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009058}
9059#endif
9060
Janos Follath088ce432017-04-10 12:42:31 +01009061#if defined(MBEDTLS_SSL_SRV_C)
9062void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9063 char cert_req_ca_list )
9064{
9065 conf->cert_req_ca_list = cert_req_ca_list;
9066}
9067#endif
9068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009069#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009070void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009071{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009072 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009073}
9074#endif
9075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009076#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009077void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009078{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009079 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009080}
9081#endif
9082
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009083#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009084void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009085{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009086 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009087}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009088#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009090#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009091int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009092{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009093 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009094 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009095 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009096 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009097 }
9098
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009099 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009100
9101 return( 0 );
9102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009103#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009105#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009106void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009107{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009108 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009109}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009110#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009112#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009113void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009114{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009115 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009116}
9117#endif
9118
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009119void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009120{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009121 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009122}
9123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009124#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009125void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009127 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009128}
9129
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009130void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009131{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009132 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009133}
9134
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009135void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009136 const unsigned char period[8] )
9137{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009138 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009139}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009140#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009142#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009143#if defined(MBEDTLS_SSL_CLI_C)
9144void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009145{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009146 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009147}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009148#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009149
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009150#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009151void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9152 mbedtls_ssl_ticket_write_t *f_ticket_write,
9153 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9154 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009155{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009156 conf->f_ticket_write = f_ticket_write;
9157 conf->f_ticket_parse = f_ticket_parse;
9158 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009159}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009161#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009162
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009163#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9164void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9165 mbedtls_ssl_export_keys_t *f_export_keys,
9166 void *p_export_keys )
9167{
9168 conf->f_export_keys = f_export_keys;
9169 conf->p_export_keys = p_export_keys;
9170}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009171
9172void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9173 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9174 void *p_export_keys )
9175{
9176 conf->f_export_keys_ext = f_export_keys_ext;
9177 conf->p_export_keys = p_export_keys;
9178}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009179#endif
9180
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009181#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009182void mbedtls_ssl_conf_async_private_cb(
9183 mbedtls_ssl_config *conf,
9184 mbedtls_ssl_async_sign_t *f_async_sign,
9185 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9186 mbedtls_ssl_async_resume_t *f_async_resume,
9187 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009188 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009189{
9190 conf->f_async_sign_start = f_async_sign;
9191 conf->f_async_decrypt_start = f_async_decrypt;
9192 conf->f_async_resume = f_async_resume;
9193 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009194 conf->p_async_config_data = async_config_data;
9195}
9196
Gilles Peskine8f97af72018-04-26 11:46:10 +02009197void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9198{
9199 return( conf->p_async_config_data );
9200}
9201
Gilles Peskine1febfef2018-04-30 11:54:39 +02009202void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009203{
9204 if( ssl->handshake == NULL )
9205 return( NULL );
9206 else
9207 return( ssl->handshake->user_async_ctx );
9208}
9209
Gilles Peskine1febfef2018-04-30 11:54:39 +02009210void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009211 void *ctx )
9212{
9213 if( ssl->handshake != NULL )
9214 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009215}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009216#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009217
Paul Bakker5121ce52009-01-03 21:22:43 +00009218/*
9219 * SSL get accessors
9220 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009221size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009222{
9223 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9224}
9225
Hanno Becker8b170a02017-10-10 11:51:19 +01009226int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9227{
9228 /*
9229 * Case A: We're currently holding back
9230 * a message for further processing.
9231 */
9232
9233 if( ssl->keep_current_message == 1 )
9234 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009235 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009236 return( 1 );
9237 }
9238
9239 /*
9240 * Case B: Further records are pending in the current datagram.
9241 */
9242
9243#if defined(MBEDTLS_SSL_PROTO_DTLS)
9244 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9245 ssl->in_left > ssl->next_record_offset )
9246 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009247 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009248 return( 1 );
9249 }
9250#endif /* MBEDTLS_SSL_PROTO_DTLS */
9251
9252 /*
9253 * Case C: A handshake message is being processed.
9254 */
9255
Hanno Becker8b170a02017-10-10 11:51:19 +01009256 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9257 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009258 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009259 return( 1 );
9260 }
9261
9262 /*
9263 * Case D: An application data message is being processed
9264 */
9265 if( ssl->in_offt != NULL )
9266 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009267 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009268 return( 1 );
9269 }
9270
9271 /*
9272 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009273 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009274 * we implement support for multiple alerts in single records.
9275 */
9276
9277 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9278 return( 0 );
9279}
9280
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009281uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009282{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009283 if( ssl->session != NULL )
9284 return( ssl->session->verify_result );
9285
9286 if( ssl->session_negotiate != NULL )
9287 return( ssl->session_negotiate->verify_result );
9288
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009289 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009290}
9291
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009292const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009293{
Paul Bakker926c8e42013-03-06 10:23:34 +01009294 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009295 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009296
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009297 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009298}
9299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009300const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009302#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009303 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009304 {
9305 switch( ssl->minor_ver )
9306 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009307 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009308 return( "DTLSv1.0" );
9309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009310 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009311 return( "DTLSv1.2" );
9312
9313 default:
9314 return( "unknown (DTLS)" );
9315 }
9316 }
9317#endif
9318
Paul Bakker43ca69c2011-01-15 17:35:19 +00009319 switch( ssl->minor_ver )
9320 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009321 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009322 return( "SSLv3.0" );
9323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009324 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009325 return( "TLSv1.0" );
9326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009327 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009328 return( "TLSv1.1" );
9329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009330 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009331 return( "TLSv1.2" );
9332
Paul Bakker43ca69c2011-01-15 17:35:19 +00009333 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009334 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009335 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009336}
9337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009338int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009339{
Hanno Becker3136ede2018-08-17 15:28:19 +01009340 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009341 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009342 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009343
Hanno Becker5903de42019-05-03 14:46:38 +01009344 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9345
Hanno Becker78640902018-08-13 16:35:15 +01009346 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009347 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009348
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009349#if defined(MBEDTLS_ZLIB_SUPPORT)
9350 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9351 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009352#endif
9353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356 case MBEDTLS_MODE_GCM:
9357 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009358 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009360 transform_expansion = transform->minlen;
9361 break;
9362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009363 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009364
9365 block_size = mbedtls_cipher_get_block_size(
9366 &transform->cipher_ctx_enc );
9367
Hanno Becker3136ede2018-08-17 15:28:19 +01009368 /* Expansion due to the addition of the MAC. */
9369 transform_expansion += transform->maclen;
9370
9371 /* Expansion due to the addition of CBC padding;
9372 * Theoretically up to 256 bytes, but we never use
9373 * more than the block size of the underlying cipher. */
9374 transform_expansion += block_size;
9375
9376 /* For TLS 1.1 or higher, an explicit IV is added
9377 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009378#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9379 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009380 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009381#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009382
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009383 break;
9384
9385 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009386 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009387 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009388 }
9389
Hanno Beckera0e20d02019-05-15 14:03:01 +01009390#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009391 if( transform->out_cid_len != 0 )
9392 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009393#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009394
Hanno Becker5903de42019-05-03 14:46:38 +01009395 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009396}
9397
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009398#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9399size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9400{
9401 size_t max_len;
9402
9403 /*
9404 * Assume mfl_code is correct since it was checked when set
9405 */
Angus Grattond8213d02016-05-25 20:56:48 +10009406 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009407
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009408 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009409 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009410 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009411 {
Angus Grattond8213d02016-05-25 20:56:48 +10009412 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009413 }
9414
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009415 /* During a handshake, use the value being negotiated */
9416 if( ssl->session_negotiate != NULL &&
9417 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9418 {
9419 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9420 }
9421
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009422 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009423}
9424#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9425
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009426#if defined(MBEDTLS_SSL_PROTO_DTLS)
9427static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9428{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009429 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9430 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9431 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9432 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9433 return ( 0 );
9434
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009435 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9436 return( ssl->mtu );
9437
9438 if( ssl->mtu == 0 )
9439 return( ssl->handshake->mtu );
9440
9441 return( ssl->mtu < ssl->handshake->mtu ?
9442 ssl->mtu : ssl->handshake->mtu );
9443}
9444#endif /* MBEDTLS_SSL_PROTO_DTLS */
9445
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009446int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9447{
9448 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9449
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009450#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9451 !defined(MBEDTLS_SSL_PROTO_DTLS)
9452 (void) ssl;
9453#endif
9454
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9456 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9457
9458 if( max_len > mfl )
9459 max_len = mfl;
9460#endif
9461
9462#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009463 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009464 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009465 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009466 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9467 const size_t overhead = (size_t) ret;
9468
9469 if( ret < 0 )
9470 return( ret );
9471
9472 if( mtu <= overhead )
9473 {
9474 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9475 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9476 }
9477
9478 if( max_len > mtu - overhead )
9479 max_len = mtu - overhead;
9480 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009481#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482
Hanno Becker0defedb2018-08-10 12:35:02 +01009483#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9484 !defined(MBEDTLS_SSL_PROTO_DTLS)
9485 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009486#endif
9487
9488 return( (int) max_len );
9489}
9490
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009491#if defined(MBEDTLS_X509_CRT_PARSE_C)
9492const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009493{
9494 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009495 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009496
Hanno Beckere6824572019-02-07 13:18:46 +00009497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009498 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009499#else
9500 return( NULL );
9501#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009502}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009503#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009505#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009506int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9507 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009508{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009509 if( ssl == NULL ||
9510 dst == NULL ||
9511 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009512 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009513 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009514 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009515 }
9516
Hanno Becker52055ae2019-02-06 14:30:46 +00009517 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009518}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009520
Paul Bakker5121ce52009-01-03 21:22:43 +00009521/*
Paul Bakker1961b702013-01-25 14:49:24 +01009522 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009525{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009527
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009528 if( ssl == NULL || ssl->conf == NULL )
9529 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9530
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009534#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009536 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009538#endif
9539
Paul Bakker1961b702013-01-25 14:49:24 +01009540 return( ret );
9541}
9542
9543/*
9544 * Perform the SSL handshake
9545 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009546int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009547{
9548 int ret = 0;
9549
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009550 if( ssl == NULL || ssl->conf == NULL )
9551 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009555 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009556 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009558
9559 if( ret != 0 )
9560 break;
9561 }
9562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009563 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009564
9565 return( ret );
9566}
9567
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009568#if defined(MBEDTLS_SSL_RENEGOTIATION)
9569#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009570/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009571 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009574{
9575 int ret;
9576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009578
9579 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009580 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9581 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009582
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009583 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009584 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009586 return( ret );
9587 }
9588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009590
9591 return( 0 );
9592}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009594
9595/*
9596 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009597 * - any side: calling mbedtls_ssl_renegotiate(),
9598 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9599 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009600 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009601 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009602 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009603 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009605{
9606 int ret;
9607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009608 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009609
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009610 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9611 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009612
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009613 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9614 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009615#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009616 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009617 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009618 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009619 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009620 ssl->handshake->out_msg_seq = 1;
9621 else
9622 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009623 }
9624#endif
9625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009626 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9627 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009632 return( ret );
9633 }
9634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009636
9637 return( 0 );
9638}
9639
9640/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009641 * Renegotiate current connection on client,
9642 * or request renegotiation on server
9643 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009644int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009645{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009646 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009647
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009648 if( ssl == NULL || ssl->conf == NULL )
9649 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009652 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009653 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009654 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009655 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9656 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009659
9660 /* Did we already try/start sending HelloRequest? */
9661 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009663
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009664 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009665 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009666#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009668#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009669 /*
9670 * On client, either start the renegotiation process or,
9671 * if already in progress, continue the handshake
9672 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009673 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009674 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9676 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009677
9678 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009680 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009681 return( ret );
9682 }
9683 }
9684 else
9685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009686 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009687 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009689 return( ret );
9690 }
9691 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009692#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009693
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009694 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009695}
9696
9697/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009698 * Check record counters and renegotiate if they're above the limit.
9699 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009700static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009701{
Andres AG2196c7f2016-12-15 17:01:16 +00009702 size_t ep_len = ssl_ep_len( ssl );
9703 int in_ctr_cmp;
9704 int out_ctr_cmp;
9705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009706 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9707 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009708 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009709 {
9710 return( 0 );
9711 }
9712
Andres AG2196c7f2016-12-15 17:01:16 +00009713 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9714 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009715 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009716 ssl->conf->renego_period + ep_len, 8 - ep_len );
9717
9718 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009719 {
9720 return( 0 );
9721 }
9722
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009723 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009724 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009725}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009726#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009727
9728/*
9729 * Receive application data decrypted from the SSL layer
9730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009731int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009732{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009733 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009734 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009735
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009736 if( ssl == NULL || ssl->conf == NULL )
9737 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9738
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009740
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009741#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009742 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009745 return( ret );
9746
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009747 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009748 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009749 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009750 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009751 return( ret );
9752 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009753 }
9754#endif
9755
Hanno Becker4a810fb2017-05-24 16:27:30 +01009756 /*
9757 * Check if renegotiation is necessary and/or handshake is
9758 * in process. If yes, perform/continue, and fall through
9759 * if an unexpected packet is received while the client
9760 * is waiting for the ServerHello.
9761 *
9762 * (There is no equivalent to the last condition on
9763 * the server-side as it is not treated as within
9764 * a handshake while waiting for the ClientHello
9765 * after a renegotiation request.)
9766 */
9767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009768#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009769 ret = ssl_check_ctr_renegotiate( ssl );
9770 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9771 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009772 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009773 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009774 return( ret );
9775 }
9776#endif
9777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009778 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009781 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9782 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009783 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009784 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009785 return( ret );
9786 }
9787 }
9788
Hanno Beckere41158b2017-10-23 13:30:32 +01009789 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009790 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009791 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009792 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009793 if( ssl->f_get_timer != NULL &&
9794 ssl->f_get_timer( ssl->p_timer ) == -1 )
9795 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009796 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009797 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009798
Hanno Becker327c93b2018-08-15 13:56:18 +01009799 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009800 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009801 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9802 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009803
Hanno Becker4a810fb2017-05-24 16:27:30 +01009804 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9805 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009806 }
9807
9808 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009809 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009810 {
9811 /*
9812 * OpenSSL sends empty messages to randomize the IV
9813 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009814 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009817 return( 0 );
9818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009820 return( ret );
9821 }
9822 }
9823
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009824 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009825 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009826 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009827
Hanno Becker4a810fb2017-05-24 16:27:30 +01009828 /*
9829 * - For client-side, expect SERVER_HELLO_REQUEST.
9830 * - For server-side, expect CLIENT_HELLO.
9831 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9832 */
9833
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009834#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009835 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009836 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009837 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009838 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009840
9841 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009843 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009844 {
9845 continue;
9846 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009847#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009849 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009850#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009851
Hanno Becker4a810fb2017-05-24 16:27:30 +01009852#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009853 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009854 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009855 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009857
9858 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009859#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009860 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009861 {
9862 continue;
9863 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009865 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009866 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009867#endif /* MBEDTLS_SSL_SRV_C */
9868
Hanno Becker21df7f92017-10-17 11:03:26 +01009869#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009870 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009871 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9872 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9873 ssl->conf->allow_legacy_renegotiation ==
9874 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9875 {
9876 /*
9877 * Accept renegotiation request
9878 */
Paul Bakker48916f92012-09-16 19:57:18 +00009879
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009880 /* DTLS clients need to know renego is server-initiated */
9881#if defined(MBEDTLS_SSL_PROTO_DTLS)
9882 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9883 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9884 {
9885 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9886 }
9887#endif
9888 ret = ssl_start_renegotiation( ssl );
9889 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9890 ret != 0 )
9891 {
9892 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9893 return( ret );
9894 }
9895 }
9896 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009897#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009898 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009899 /*
9900 * Refuse renegotiation
9901 */
9902
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009903 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009904
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009905#if defined(MBEDTLS_SSL_PROTO_SSL3)
9906 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009907 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009908 /* SSLv3 does not have a "no_renegotiation" warning, so
9909 we send a fatal alert and abort the connection. */
9910 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9911 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9912 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009913 }
9914 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9916#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9917 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9918 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009919 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009920 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9921 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9922 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009923 {
9924 return( ret );
9925 }
Paul Bakker48916f92012-09-16 19:57:18 +00009926 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009927 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009928#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9929 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009930 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009931 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9932 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009933 }
Paul Bakker48916f92012-09-16 19:57:18 +00009934 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009935
Hanno Becker90333da2017-10-10 11:27:13 +01009936 /* At this point, we don't know whether the renegotiation has been
9937 * completed or not. The cases to consider are the following:
9938 * 1) The renegotiation is complete. In this case, no new record
9939 * has been read yet.
9940 * 2) The renegotiation is incomplete because the client received
9941 * an application data record while awaiting the ServerHello.
9942 * 3) The renegotiation is incomplete because the client received
9943 * a non-handshake, non-application data message while awaiting
9944 * the ServerHello.
9945 * In each of these case, looping will be the proper action:
9946 * - For 1), the next iteration will read a new record and check
9947 * if it's application data.
9948 * - For 2), the loop condition isn't satisfied as application data
9949 * is present, hence continue is the same as break
9950 * - For 3), the loop condition is satisfied and read_record
9951 * will re-deliver the message that was held back by the client
9952 * when expecting the ServerHello.
9953 */
9954 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009955 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009956#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009957 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009958 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009959 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009960 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009961 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009964 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009966 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009967 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009968 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009969#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009971 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9972 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009975 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009976 }
9977
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009978 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9981 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009982 }
9983
9984 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009985
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009986 /* We're going to return something now, cancel timer,
9987 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009988 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009989 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009990
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009991#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009992 /* If we requested renego but received AppData, resend HelloRequest.
9993 * Do it now, after setting in_offt, to avoid taking this branch
9994 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009995#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009996 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009998 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009999 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010002 return( ret );
10003 }
10004 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010005#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010006#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010007 }
10008
10009 n = ( len < ssl->in_msglen )
10010 ? len : ssl->in_msglen;
10011
10012 memcpy( buf, ssl->in_offt, n );
10013 ssl->in_msglen -= n;
10014
10015 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010016 {
10017 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010018 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010019 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010020 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010021 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010022 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010023 /* more data available */
10024 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010025 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010027 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010028
Paul Bakker23986e52011-04-24 08:57:21 +000010029 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010030}
10031
10032/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010033 * Send application data to be encrypted by the SSL layer, taking care of max
10034 * fragment length and buffer size.
10035 *
10036 * According to RFC 5246 Section 6.2.1:
10037 *
10038 * Zero-length fragments of Application data MAY be sent as they are
10039 * potentially useful as a traffic analysis countermeasure.
10040 *
10041 * Therefore, it is possible that the input message length is 0 and the
10042 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010043 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010044static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010045 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010046{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010047 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10048 const size_t max_len = (size_t) ret;
10049
10050 if( ret < 0 )
10051 {
10052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10053 return( ret );
10054 }
10055
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010056 if( len > max_len )
10057 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010060 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010062 "maximum fragment length: %d > %d",
10063 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010064 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010065 }
10066 else
10067#endif
10068 len = max_len;
10069 }
Paul Bakker887bd502011-06-08 13:10:54 +000010070
Paul Bakker5121ce52009-01-03 21:22:43 +000010071 if( ssl->out_left != 0 )
10072 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010073 /*
10074 * The user has previously tried to send the data and
10075 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10076 * written. In this case, we expect the high-level write function
10077 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010079 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010080 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010081 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010082 return( ret );
10083 }
10084 }
Paul Bakker887bd502011-06-08 13:10:54 +000010085 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010086 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010087 /*
10088 * The user is trying to send a message the first time, so we need to
10089 * copy the data into the internal buffers and setup the data structure
10090 * to keep track of partial writes
10091 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010092 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010093 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010094 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010095
Hanno Becker67bc7c32018-08-06 11:33:50 +010010096 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010099 return( ret );
10100 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010101 }
10102
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010103 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010104}
10105
10106/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010107 * Write application data, doing 1/n-1 splitting if necessary.
10108 *
10109 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010110 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010111 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010112 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010113#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010114static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010115 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010116{
10117 int ret;
10118
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010119 if( ssl->conf->cbc_record_splitting ==
10120 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010121 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010122 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10123 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10124 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010125 {
10126 return( ssl_write_real( ssl, buf, len ) );
10127 }
10128
10129 if( ssl->split_done == 0 )
10130 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010131 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010132 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010133 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010134 }
10135
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010136 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10137 return( ret );
10138 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010139
10140 return( ret + 1 );
10141}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010142#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010143
10144/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010145 * Write application data (public-facing wrapper)
10146 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010147int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010148{
10149 int ret;
10150
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010152
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010153 if( ssl == NULL || ssl->conf == NULL )
10154 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10155
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010156#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010157 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10158 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010159 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010160 return( ret );
10161 }
10162#endif
10163
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010164 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010165 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010166 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010167 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010169 return( ret );
10170 }
10171 }
10172
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010173#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010174 ret = ssl_write_split( ssl, buf, len );
10175#else
10176 ret = ssl_write_real( ssl, buf, len );
10177#endif
10178
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010179 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010180
10181 return( ret );
10182}
10183
10184/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010185 * Notify the peer that the connection is being closed
10186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010187int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010188{
10189 int ret;
10190
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010191 if( ssl == NULL || ssl->conf == NULL )
10192 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10193
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010194 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010195
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010196 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010199 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10202 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10203 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010206 return( ret );
10207 }
10208 }
10209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010210 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010211
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010212 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010213}
10214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010215void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010216{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010217 if( transform == NULL )
10218 return;
10219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010221 deflateEnd( &transform->ctx_deflate );
10222 inflateEnd( &transform->ctx_inflate );
10223#endif
10224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10226 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010227
Hanno Beckerd56ed242018-01-03 15:32:51 +000010228#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010229 mbedtls_md_free( &transform->md_ctx_enc );
10230 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010231#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010232
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010233 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010234}
10235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236#if defined(MBEDTLS_X509_CRT_PARSE_C)
10237static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010239 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010240
10241 while( cur != NULL )
10242 {
10243 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010245 cur = next;
10246 }
10247}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010248#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010249
Hanno Becker0271f962018-08-16 13:23:47 +010010250#if defined(MBEDTLS_SSL_PROTO_DTLS)
10251
10252static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10253{
10254 unsigned offset;
10255 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10256
10257 if( hs == NULL )
10258 return;
10259
Hanno Becker283f5ef2018-08-24 09:34:47 +010010260 ssl_free_buffered_record( ssl );
10261
Hanno Becker0271f962018-08-16 13:23:47 +010010262 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010263 ssl_buffering_free_slot( ssl, offset );
10264}
10265
10266static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10267 uint8_t slot )
10268{
10269 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10270 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010271
10272 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10273 return;
10274
Hanno Beckere605b192018-08-21 15:59:07 +010010275 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010276 {
Hanno Beckere605b192018-08-21 15:59:07 +010010277 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010278 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010279 mbedtls_free( hs_buf->data );
10280 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010281 }
10282}
10283
10284#endif /* MBEDTLS_SSL_PROTO_DTLS */
10285
Gilles Peskine9b562d52018-04-25 20:32:43 +020010286void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010287{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010288 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10289
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010290 if( handshake == NULL )
10291 return;
10292
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010293#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10294 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10295 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010296 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010297 handshake->async_in_progress = 0;
10298 }
10299#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10300
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010301#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10302 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10303 mbedtls_md5_free( &handshake->fin_md5 );
10304 mbedtls_sha1_free( &handshake->fin_sha1 );
10305#endif
10306#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10307#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010308#if defined(MBEDTLS_USE_PSA_CRYPTO)
10309 psa_hash_abort( &handshake->fin_sha256_psa );
10310#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010311 mbedtls_sha256_free( &handshake->fin_sha256 );
10312#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010313#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010314#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010315#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010316 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010317#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010318 mbedtls_sha512_free( &handshake->fin_sha512 );
10319#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010320#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010321#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010323#if defined(MBEDTLS_DHM_C)
10324 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010325#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010326#if defined(MBEDTLS_ECDH_C)
10327 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010328#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010329#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010330 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010331#if defined(MBEDTLS_SSL_CLI_C)
10332 mbedtls_free( handshake->ecjpake_cache );
10333 handshake->ecjpake_cache = NULL;
10334 handshake->ecjpake_cache_len = 0;
10335#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010336#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010337
Janos Follath4ae5c292016-02-10 11:27:43 +000010338#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10339 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010340 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010341 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010342#endif
10343
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010344#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10345 if( handshake->psk != NULL )
10346 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010347 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010348 mbedtls_free( handshake->psk );
10349 }
10350#endif
10351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010352#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10353 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010354 /*
10355 * Free only the linked list wrapper, not the keys themselves
10356 * since the belong to the SNI callback
10357 */
10358 if( handshake->sni_key_cert != NULL )
10359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010360 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010361
10362 while( cur != NULL )
10363 {
10364 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010366 cur = next;
10367 }
10368 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010369#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010370
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010371#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010372 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010373 if( handshake->ecrs_peer_cert != NULL )
10374 {
10375 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10376 mbedtls_free( handshake->ecrs_peer_cert );
10377 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010378#endif
10379
Hanno Becker75173122019-02-06 16:18:31 +000010380#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10381 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10382 mbedtls_pk_free( &handshake->peer_pubkey );
10383#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010385#if defined(MBEDTLS_SSL_PROTO_DTLS)
10386 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010387 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010388 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010389#endif
10390
Hanno Becker4a63ed42019-01-08 11:39:35 +000010391#if defined(MBEDTLS_ECDH_C) && \
10392 defined(MBEDTLS_USE_PSA_CRYPTO)
10393 psa_destroy_key( handshake->ecdh_psa_privkey );
10394#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10395
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010396 mbedtls_platform_zeroize( handshake,
10397 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010398}
10399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010400void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010401{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010402 if( session == NULL )
10403 return;
10404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010405#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010406 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010407#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010408
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010409#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010410 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010411#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010412
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010413 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010414}
10415
Paul Bakker5121ce52009-01-03 21:22:43 +000010416/*
10417 * Free an SSL context
10418 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010419void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010420{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010421 if( ssl == NULL )
10422 return;
10423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010424 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010425
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010426 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010427 {
Angus Grattond8213d02016-05-25 20:56:48 +100010428 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010429 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010430 }
10431
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010432 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010433 {
Angus Grattond8213d02016-05-25 20:56:48 +100010434 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010435 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010436 }
10437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010439 if( ssl->compress_buf != NULL )
10440 {
Angus Grattond8213d02016-05-25 20:56:48 +100010441 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010442 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010443 }
10444#endif
10445
Paul Bakker48916f92012-09-16 19:57:18 +000010446 if( ssl->transform )
10447 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010448 mbedtls_ssl_transform_free( ssl->transform );
10449 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010450 }
10451
10452 if( ssl->handshake )
10453 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010454 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010455 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10456 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010458 mbedtls_free( ssl->handshake );
10459 mbedtls_free( ssl->transform_negotiate );
10460 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010461 }
10462
Paul Bakkerc0463502013-02-14 11:19:38 +010010463 if( ssl->session )
10464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010465 mbedtls_ssl_session_free( ssl->session );
10466 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010467 }
10468
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010469#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010470 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010471 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010472 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010474 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010475#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010477#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10478 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010479 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010480 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10481 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010482 }
10483#endif
10484
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010485#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010486 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010487#endif
10488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010490
Paul Bakker86f04f42013-02-14 11:20:09 +010010491 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010492 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010493}
10494
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010495/*
10496 * Initialze mbedtls_ssl_config
10497 */
10498void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10499{
10500 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10501}
10502
Simon Butcherc97b6972015-12-27 23:48:17 +000010503#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010504static int ssl_preset_default_hashes[] = {
10505#if defined(MBEDTLS_SHA512_C)
10506 MBEDTLS_MD_SHA512,
10507 MBEDTLS_MD_SHA384,
10508#endif
10509#if defined(MBEDTLS_SHA256_C)
10510 MBEDTLS_MD_SHA256,
10511 MBEDTLS_MD_SHA224,
10512#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010513#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010514 MBEDTLS_MD_SHA1,
10515#endif
10516 MBEDTLS_MD_NONE
10517};
Simon Butcherc97b6972015-12-27 23:48:17 +000010518#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010519
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010520static int ssl_preset_suiteb_ciphersuites[] = {
10521 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10522 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10523 0
10524};
10525
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010526#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010527static int ssl_preset_suiteb_hashes[] = {
10528 MBEDTLS_MD_SHA256,
10529 MBEDTLS_MD_SHA384,
10530 MBEDTLS_MD_NONE
10531};
10532#endif
10533
10534#if defined(MBEDTLS_ECP_C)
10535static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10536 MBEDTLS_ECP_DP_SECP256R1,
10537 MBEDTLS_ECP_DP_SECP384R1,
10538 MBEDTLS_ECP_DP_NONE
10539};
10540#endif
10541
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010542/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010543 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010544 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010545int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010546 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010548#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010550#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010551
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010552 /* Use the functions here so that they are covered in tests,
10553 * but otherwise access member directly for efficiency */
10554 mbedtls_ssl_conf_endpoint( conf, endpoint );
10555 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010557 /*
10558 * Things that are common to all presets
10559 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010560#if defined(MBEDTLS_SSL_CLI_C)
10561 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10562 {
10563 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10564#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10565 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10566#endif
10567 }
10568#endif
10569
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010570#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010571 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010572#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010573
10574#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10575 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10576#endif
10577
10578#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10579 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10580#endif
10581
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010582#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10583 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10584#endif
10585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010587 conf->f_cookie_write = ssl_cookie_write_dummy;
10588 conf->f_cookie_check = ssl_cookie_check_dummy;
10589#endif
10590
10591#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10592 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10593#endif
10594
Janos Follath088ce432017-04-10 12:42:31 +010010595#if defined(MBEDTLS_SSL_SRV_C)
10596 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10597#endif
10598
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010599#if defined(MBEDTLS_SSL_PROTO_DTLS)
10600 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10601 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10602#endif
10603
10604#if defined(MBEDTLS_SSL_RENEGOTIATION)
10605 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010606 memset( conf->renego_period, 0x00, 2 );
10607 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010608#endif
10609
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010610#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10611 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10612 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010613 const unsigned char dhm_p[] =
10614 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10615 const unsigned char dhm_g[] =
10616 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10617
Hanno Beckera90658f2017-10-04 15:29:08 +010010618 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10619 dhm_p, sizeof( dhm_p ),
10620 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010621 {
10622 return( ret );
10623 }
10624 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010625#endif
10626
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010627 /*
10628 * Preset-specific defaults
10629 */
10630 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010631 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010632 /*
10633 * NSA Suite B
10634 */
10635 case MBEDTLS_SSL_PRESET_SUITEB:
10636 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10637 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10638 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10639 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10640
10641 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10642 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10643 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10644 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10645 ssl_preset_suiteb_ciphersuites;
10646
10647#if defined(MBEDTLS_X509_CRT_PARSE_C)
10648 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010649#endif
10650
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010651#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010652 conf->sig_hashes = ssl_preset_suiteb_hashes;
10653#endif
10654
10655#if defined(MBEDTLS_ECP_C)
10656 conf->curve_list = ssl_preset_suiteb_curves;
10657#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010658 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010659
10660 /*
10661 * Default
10662 */
10663 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010664 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10665 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10666 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10667 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10668 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10669 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10670 MBEDTLS_SSL_MIN_MINOR_VERSION :
10671 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010672 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10673 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10674
10675#if defined(MBEDTLS_SSL_PROTO_DTLS)
10676 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10677 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10678#endif
10679
10680 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10681 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10682 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10684 mbedtls_ssl_list_ciphersuites();
10685
10686#if defined(MBEDTLS_X509_CRT_PARSE_C)
10687 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10688#endif
10689
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010690#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010691 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010692#endif
10693
10694#if defined(MBEDTLS_ECP_C)
10695 conf->curve_list = mbedtls_ecp_grp_id_list();
10696#endif
10697
10698#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10699 conf->dhm_min_bitlen = 1024;
10700#endif
10701 }
10702
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010703 return( 0 );
10704}
10705
10706/*
10707 * Free mbedtls_ssl_config
10708 */
10709void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10710{
10711#if defined(MBEDTLS_DHM_C)
10712 mbedtls_mpi_free( &conf->dhm_P );
10713 mbedtls_mpi_free( &conf->dhm_G );
10714#endif
10715
10716#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10717 if( conf->psk != NULL )
10718 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010719 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010720 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010721 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010723 }
10724
10725 if( conf->psk_identity != NULL )
10726 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010727 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010728 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010729 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730 conf->psk_identity_len = 0;
10731 }
10732#endif
10733
10734#if defined(MBEDTLS_X509_CRT_PARSE_C)
10735 ssl_key_cert_free( conf->key_cert );
10736#endif
10737
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010738 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010739}
10740
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010741#if defined(MBEDTLS_PK_C) && \
10742 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010743/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748#if defined(MBEDTLS_RSA_C)
10749 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10750 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752#if defined(MBEDTLS_ECDSA_C)
10753 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10754 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010757}
10758
Hanno Becker7e5437a2017-04-28 17:15:26 +010010759unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10760{
10761 switch( type ) {
10762 case MBEDTLS_PK_RSA:
10763 return( MBEDTLS_SSL_SIG_RSA );
10764 case MBEDTLS_PK_ECDSA:
10765 case MBEDTLS_PK_ECKEY:
10766 return( MBEDTLS_SSL_SIG_ECDSA );
10767 default:
10768 return( MBEDTLS_SSL_SIG_ANON );
10769 }
10770}
10771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010773{
10774 switch( sig )
10775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776#if defined(MBEDTLS_RSA_C)
10777 case MBEDTLS_SSL_SIG_RSA:
10778 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780#if defined(MBEDTLS_ECDSA_C)
10781 case MBEDTLS_SSL_SIG_ECDSA:
10782 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010783#endif
10784 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010786 }
10787}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010788#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010789
Hanno Becker7e5437a2017-04-28 17:15:26 +010010790#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10791 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10792
10793/* Find an entry in a signature-hash set matching a given hash algorithm. */
10794mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10795 mbedtls_pk_type_t sig_alg )
10796{
10797 switch( sig_alg )
10798 {
10799 case MBEDTLS_PK_RSA:
10800 return( set->rsa );
10801 case MBEDTLS_PK_ECDSA:
10802 return( set->ecdsa );
10803 default:
10804 return( MBEDTLS_MD_NONE );
10805 }
10806}
10807
10808/* Add a signature-hash-pair to a signature-hash set */
10809void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10810 mbedtls_pk_type_t sig_alg,
10811 mbedtls_md_type_t md_alg )
10812{
10813 switch( sig_alg )
10814 {
10815 case MBEDTLS_PK_RSA:
10816 if( set->rsa == MBEDTLS_MD_NONE )
10817 set->rsa = md_alg;
10818 break;
10819
10820 case MBEDTLS_PK_ECDSA:
10821 if( set->ecdsa == MBEDTLS_MD_NONE )
10822 set->ecdsa = md_alg;
10823 break;
10824
10825 default:
10826 break;
10827 }
10828}
10829
10830/* Allow exactly one hash algorithm for each signature. */
10831void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10832 mbedtls_md_type_t md_alg )
10833{
10834 set->rsa = md_alg;
10835 set->ecdsa = md_alg;
10836}
10837
10838#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10839 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10840
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010841/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010842 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010845{
10846 switch( hash )
10847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848#if defined(MBEDTLS_MD5_C)
10849 case MBEDTLS_SSL_HASH_MD5:
10850 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852#if defined(MBEDTLS_SHA1_C)
10853 case MBEDTLS_SSL_HASH_SHA1:
10854 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856#if defined(MBEDTLS_SHA256_C)
10857 case MBEDTLS_SSL_HASH_SHA224:
10858 return( MBEDTLS_MD_SHA224 );
10859 case MBEDTLS_SSL_HASH_SHA256:
10860 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862#if defined(MBEDTLS_SHA512_C)
10863 case MBEDTLS_SSL_HASH_SHA384:
10864 return( MBEDTLS_MD_SHA384 );
10865 case MBEDTLS_SSL_HASH_SHA512:
10866 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010867#endif
10868 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010870 }
10871}
10872
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010873/*
10874 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10875 */
10876unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10877{
10878 switch( md )
10879 {
10880#if defined(MBEDTLS_MD5_C)
10881 case MBEDTLS_MD_MD5:
10882 return( MBEDTLS_SSL_HASH_MD5 );
10883#endif
10884#if defined(MBEDTLS_SHA1_C)
10885 case MBEDTLS_MD_SHA1:
10886 return( MBEDTLS_SSL_HASH_SHA1 );
10887#endif
10888#if defined(MBEDTLS_SHA256_C)
10889 case MBEDTLS_MD_SHA224:
10890 return( MBEDTLS_SSL_HASH_SHA224 );
10891 case MBEDTLS_MD_SHA256:
10892 return( MBEDTLS_SSL_HASH_SHA256 );
10893#endif
10894#if defined(MBEDTLS_SHA512_C)
10895 case MBEDTLS_MD_SHA384:
10896 return( MBEDTLS_SSL_HASH_SHA384 );
10897 case MBEDTLS_MD_SHA512:
10898 return( MBEDTLS_SSL_HASH_SHA512 );
10899#endif
10900 default:
10901 return( MBEDTLS_SSL_HASH_NONE );
10902 }
10903}
10904
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010905#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010906/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010907 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010908 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010909 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010910int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010913
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010914 if( ssl->conf->curve_list == NULL )
10915 return( -1 );
10916
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010917 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010918 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010919 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010920
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010921 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010922}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010923#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010924
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010925#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010926/*
10927 * Check if a hash proposed by the peer is in our list.
10928 * Return 0 if we're willing to use it, -1 otherwise.
10929 */
10930int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10931 mbedtls_md_type_t md )
10932{
10933 const int *cur;
10934
10935 if( ssl->conf->sig_hashes == NULL )
10936 return( -1 );
10937
10938 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10939 if( *cur == (int) md )
10940 return( 0 );
10941
10942 return( -1 );
10943}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010944#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if defined(MBEDTLS_X509_CRT_PARSE_C)
10947int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10948 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010949 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010950 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010951{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010952 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010954 int usage = 0;
10955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010957 const char *ext_oid;
10958 size_t ext_len;
10959#endif
10960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10962 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010963 ((void) cert);
10964 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010965 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010966#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10969 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010970 {
10971 /* Server part of the key exchange */
10972 switch( ciphersuite->key_exchange )
10973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 case MBEDTLS_KEY_EXCHANGE_RSA:
10975 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010976 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010977 break;
10978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010979 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10980 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10981 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10982 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010983 break;
10984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10986 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010987 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010988 break;
10989
10990 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991 case MBEDTLS_KEY_EXCHANGE_NONE:
10992 case MBEDTLS_KEY_EXCHANGE_PSK:
10993 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10994 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010995 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010996 usage = 0;
10997 }
10998 }
10999 else
11000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11002 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011003 }
11004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011006 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011007 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011008 ret = -1;
11009 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011010#else
11011 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11015 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11018 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011019 }
11020 else
11021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11023 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011024 }
11025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011027 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011028 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011029 ret = -1;
11030 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011032
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011033 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011036
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011037/*
11038 * Convert version numbers to/from wire format
11039 * and, for DTLS, to/from TLS equivalent.
11040 *
11041 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011042 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011043 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11044 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011047 unsigned char ver[2] )
11048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049#if defined(MBEDTLS_SSL_PROTO_DTLS)
11050 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011053 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11054
11055 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11056 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11057 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011058 else
11059#else
11060 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011061#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011062 {
11063 ver[0] = (unsigned char) major;
11064 ver[1] = (unsigned char) minor;
11065 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011066}
11067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011069 const unsigned char ver[2] )
11070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071#if defined(MBEDTLS_SSL_PROTO_DTLS)
11072 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011073 {
11074 *major = 255 - ver[0] + 2;
11075 *minor = 255 - ver[1] + 1;
11076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011078 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11079 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011080 else
11081#else
11082 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011083#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011084 {
11085 *major = ver[0];
11086 *minor = ver[1];
11087 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011088}
11089
Simon Butcher99000142016-10-13 17:21:01 +010011090int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11091{
11092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11093 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11094 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11095
11096 switch( md )
11097 {
11098#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11099#if defined(MBEDTLS_MD5_C)
11100 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011101 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011102#endif
11103#if defined(MBEDTLS_SHA1_C)
11104 case MBEDTLS_SSL_HASH_SHA1:
11105 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11106 break;
11107#endif
11108#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11109#if defined(MBEDTLS_SHA512_C)
11110 case MBEDTLS_SSL_HASH_SHA384:
11111 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11112 break;
11113#endif
11114#if defined(MBEDTLS_SHA256_C)
11115 case MBEDTLS_SSL_HASH_SHA256:
11116 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11117 break;
11118#endif
11119 default:
11120 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11121 }
11122
11123 return 0;
11124#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11125 (void) ssl;
11126 (void) md;
11127
11128 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11129#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11130}
11131
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011132#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11133 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11134int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11135 unsigned char *output,
11136 unsigned char *data, size_t data_len )
11137{
11138 int ret = 0;
11139 mbedtls_md5_context mbedtls_md5;
11140 mbedtls_sha1_context mbedtls_sha1;
11141
11142 mbedtls_md5_init( &mbedtls_md5 );
11143 mbedtls_sha1_init( &mbedtls_sha1 );
11144
11145 /*
11146 * digitally-signed struct {
11147 * opaque md5_hash[16];
11148 * opaque sha_hash[20];
11149 * };
11150 *
11151 * md5_hash
11152 * MD5(ClientHello.random + ServerHello.random
11153 * + ServerParams);
11154 * sha_hash
11155 * SHA(ClientHello.random + ServerHello.random
11156 * + ServerParams);
11157 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011158 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011159 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011161 goto exit;
11162 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 ssl->handshake->randbytes, 64 ) ) != 0 )
11165 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011167 goto exit;
11168 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011169 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011170 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 goto exit;
11173 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 goto exit;
11178 }
11179
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011180 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011183 goto exit;
11184 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 ssl->handshake->randbytes, 64 ) ) != 0 )
11187 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 goto exit;
11190 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011191 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011192 data_len ) ) != 0 )
11193 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011195 goto exit;
11196 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011197 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011198 output + 16 ) ) != 0 )
11199 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011201 goto exit;
11202 }
11203
11204exit:
11205 mbedtls_md5_free( &mbedtls_md5 );
11206 mbedtls_sha1_free( &mbedtls_sha1 );
11207
11208 if( ret != 0 )
11209 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11210 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11211
11212 return( ret );
11213
11214}
11215#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11216 MBEDTLS_SSL_PROTO_TLS1_1 */
11217
11218#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11219 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011220
11221#if defined(MBEDTLS_USE_PSA_CRYPTO)
11222int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11223 unsigned char *hash, size_t *hashlen,
11224 unsigned char *data, size_t data_len,
11225 mbedtls_md_type_t md_alg )
11226{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011227 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011228 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011229 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11230
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011232
11233 if( ( status = psa_hash_setup( &hash_operation,
11234 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011235 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011236 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011237 goto exit;
11238 }
11239
Andrzej Kurek814feff2019-01-14 04:35:19 -050011240 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11241 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011242 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011243 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011244 goto exit;
11245 }
11246
Andrzej Kurek814feff2019-01-14 04:35:19 -050011247 if( ( status = psa_hash_update( &hash_operation,
11248 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011249 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011250 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011251 goto exit;
11252 }
11253
Andrzej Kurek814feff2019-01-14 04:35:19 -050011254 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11255 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011257 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011258 goto exit;
11259 }
11260
11261exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011262 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011263 {
11264 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11265 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011266 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011267 {
11268 case PSA_ERROR_NOT_SUPPORTED:
11269 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011270 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011271 case PSA_ERROR_BUFFER_TOO_SMALL:
11272 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11273 case PSA_ERROR_INSUFFICIENT_MEMORY:
11274 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11275 default:
11276 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11277 }
11278 }
11279 return( 0 );
11280}
11281
11282#else
11283
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011284int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011285 unsigned char *hash, size_t *hashlen,
11286 unsigned char *data, size_t data_len,
11287 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011288{
11289 int ret = 0;
11290 mbedtls_md_context_t ctx;
11291 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011292 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011293
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011295
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296 mbedtls_md_init( &ctx );
11297
11298 /*
11299 * digitally-signed struct {
11300 * opaque client_random[32];
11301 * opaque server_random[32];
11302 * ServerDHParams params;
11303 * };
11304 */
11305 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11306 {
11307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11308 goto exit;
11309 }
11310 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11311 {
11312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11313 goto exit;
11314 }
11315 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11316 {
11317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11318 goto exit;
11319 }
11320 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11321 {
11322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11323 goto exit;
11324 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011325 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011326 {
11327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11328 goto exit;
11329 }
11330
11331exit:
11332 mbedtls_md_free( &ctx );
11333
11334 if( ret != 0 )
11335 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11336 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11337
11338 return( ret );
11339}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011340#endif /* MBEDTLS_USE_PSA_CRYPTO */
11341
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011342#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11343 MBEDTLS_SSL_PROTO_TLS1_2 */
11344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011345#endif /* MBEDTLS_SSL_TLS_C */