blob: 3f9818cd20200d1e31bd8e45219fc7d4fd6c3319 [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
Hanno Becker2a43f6f2018-08-10 11:12:52 +010062static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010063static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010064
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010065/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020069 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010070 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010071#else
72 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010073#endif
74 return( 0 );
75}
76
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020077/*
78 * Start a timer.
79 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020080 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020082{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020083 if( ssl->f_set_timer == NULL )
84 return;
85
86 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
87 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020088}
89
90/*
91 * Return -1 is timer is expired, 0 if it isn't.
92 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020094{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020095 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +020096 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020097
98 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020099 {
100 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200101 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200102 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200103
104 return( 0 );
105}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200106
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100107static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
108 mbedtls_ssl_transform *transform );
Hanno Becker79594fd2019-05-08 09:38:41 +0100109static void ssl_update_in_pointers( mbedtls_ssl_context *ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100110
111#define SSL_DONT_FORCE_FLUSH 0
112#define SSL_FORCE_FLUSH 1
113
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200114#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100115
Hanno Beckera0e20d02019-05-15 14:03:01 +0100116#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100117/* Top-level Connection ID API */
118
Hanno Becker8367ccc2019-05-14 11:30:10 +0100119int mbedtls_ssl_conf_cid( mbedtls_ssl_config *conf,
120 size_t len,
121 int ignore_other_cid )
Hanno Beckerad4a1372019-05-03 13:06:44 +0100122{
123 if( len > MBEDTLS_SSL_CID_IN_LEN_MAX )
124 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
125
Hanno Becker611ac772019-05-14 11:45:26 +0100126 if( ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
127 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
128 {
129 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
130 }
131
132 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100133 conf->cid_len = len;
134 return( 0 );
135}
136
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100137int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
138 int enable,
139 unsigned char const *own_cid,
140 size_t own_cid_len )
141{
Hanno Becker76a79ab2019-05-03 14:38:32 +0100142 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
143 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
144
Hanno Beckerca092242019-04-25 16:01:49 +0100145 ssl->negotiate_cid = enable;
146 if( enable == MBEDTLS_SSL_CID_DISABLED )
147 {
148 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
149 return( 0 );
150 }
151 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
Hanno Beckerad4a1372019-05-03 13:06:44 +0100152 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerca092242019-04-25 16:01:49 +0100153
Hanno Beckerad4a1372019-05-03 13:06:44 +0100154 if( own_cid_len != ssl->conf->cid_len )
Hanno Beckerca092242019-04-25 16:01:49 +0100155 {
Hanno Beckerad4a1372019-05-03 13:06:44 +0100156 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID length %u does not match CID length %u in config",
157 (unsigned) own_cid_len,
158 (unsigned) ssl->conf->cid_len ) );
Hanno Beckerca092242019-04-25 16:01:49 +0100159 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
160 }
161
162 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100163 /* Truncation is not an issue here because
164 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
165 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100166
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100167 return( 0 );
168}
169
170int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
171 int *enabled,
172 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
173 size_t *peer_cid_len )
174{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100175 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100176
Hanno Becker76a79ab2019-05-03 14:38:32 +0100177 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
178 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
179 {
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100180 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Becker76a79ab2019-05-03 14:38:32 +0100181 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100182
Hanno Beckerc5f24222019-05-03 12:54:52 +0100183 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
184 * were used, but client and server requested the empty CID.
185 * This is indistinguishable from not using the CID extension
186 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100187 if( ssl->transform_in->in_cid_len == 0 &&
188 ssl->transform_in->out_cid_len == 0 )
189 {
190 return( 0 );
191 }
192
Hanno Becker615ef172019-05-22 16:50:35 +0100193 if( peer_cid_len != NULL )
194 {
195 *peer_cid_len = ssl->transform_in->out_cid_len;
196 if( peer_cid != NULL )
197 {
198 memcpy( peer_cid, ssl->transform_in->out_cid,
199 ssl->transform_in->out_cid_len );
200 }
201 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100202
203 *enabled = MBEDTLS_SSL_CID_ENABLED;
204
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100205 return( 0 );
206}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100207#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100208
Hanno Beckerd5847772018-08-28 10:09:23 +0100209/* Forward declarations for functions related to message buffering. */
210static void ssl_buffering_free( mbedtls_ssl_context *ssl );
211static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
212 uint8_t slot );
213static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
214static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
215static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
216static int ssl_buffer_message( mbedtls_ssl_context *ssl );
217static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100218static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100219
Hanno Beckera67dee22018-08-22 10:05:20 +0100220static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100221static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100222{
Hanno Becker11682cc2018-08-22 14:41:02 +0100223 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100224
225 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100226 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100227
228 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
229}
230
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
232{
Hanno Becker11682cc2018-08-22 14:41:02 +0100233 size_t const bytes_written = ssl->out_left;
234 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100235
236 /* Double-check that the write-index hasn't gone
237 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100238 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100239 {
240 /* Should never happen... */
241 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
242 }
243
244 return( (int) ( mtu - bytes_written ) );
245}
246
247static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
248{
249 int ret;
250 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400251 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100252
253#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
254 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
255
256 if( max_len > mfl )
257 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100258
259 /* By the standard (RFC 6066 Sect. 4), the MFL extension
260 * only limits the maximum record payload size, so in theory
261 * we would be allowed to pack multiple records of payload size
262 * MFL into a single datagram. However, this would mean that there's
263 * no way to explicitly communicate MTU restrictions to the peer.
264 *
265 * The following reduction of max_len makes sure that we never
266 * write datagrams larger than MFL + Record Expansion Overhead.
267 */
268 if( max_len <= ssl->out_left )
269 return( 0 );
270
271 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100272#endif
273
274 ret = ssl_get_remaining_space_in_datagram( ssl );
275 if( ret < 0 )
276 return( ret );
277 remaining = (size_t) ret;
278
279 ret = mbedtls_ssl_get_record_expansion( ssl );
280 if( ret < 0 )
281 return( ret );
282 expansion = (size_t) ret;
283
284 if( remaining <= expansion )
285 return( 0 );
286
287 remaining -= expansion;
288 if( remaining >= max_len )
289 remaining = max_len;
290
291 return( (int) remaining );
292}
293
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200294/*
295 * Double the retransmit timeout value, within the allowed range,
296 * returning -1 if the maximum value has already been reached.
297 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200299{
300 uint32_t new_timeout;
301
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200302 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303 return( -1 );
304
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200305 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
306 * in the following way: after the initial transmission and a first
307 * retransmission, back off to a temporary estimated MTU of 508 bytes.
308 * This value is guaranteed to be deliverable (if not guaranteed to be
309 * delivered) of any compliant IPv4 (and IPv6) network, and should work
310 * on most non-IP stacks too. */
311 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400312 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200313 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
315 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200316
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200317 new_timeout = 2 * ssl->handshake->retransmit_timeout;
318
319 /* Avoid arithmetic overflow and range overflow */
320 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200321 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200322 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200323 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200324 }
325
326 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200327 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200328 ssl->handshake->retransmit_timeout ) );
329
330 return( 0 );
331}
332
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200333static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200334{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200335 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200337 ssl->handshake->retransmit_timeout ) );
338}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200339#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200341#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200342/*
343 * Convert max_fragment_length codes to length.
344 * RFC 6066 says:
345 * enum{
346 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
347 * } MaxFragmentLength;
348 * and we add 0 -> extension unused
349 */
Angus Grattond8213d02016-05-25 20:56:48 +1000350static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200351{
Angus Grattond8213d02016-05-25 20:56:48 +1000352 switch( mfl )
353 {
354 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
355 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
356 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
357 return 512;
358 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
359 return 1024;
360 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
361 return 2048;
362 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
363 return 4096;
364 default:
365 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
366 }
367}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200369
Hanno Becker52055ae2019-02-06 14:30:46 +0000370int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
371 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200373 mbedtls_ssl_session_free( dst );
374 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000377
378#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200379 if( src->peer_cert != NULL )
380 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200381 int ret;
382
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200383 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200384 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200385 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200387 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200388
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200390 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200393 dst->peer_cert = NULL;
394 return( ret );
395 }
396 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000397#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000398 if( src->peer_cert_digest != NULL )
399 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000400 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000401 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000402 if( dst->peer_cert_digest == NULL )
403 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
404
405 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
406 src->peer_cert_digest_len );
407 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000408 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000409 }
410#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200412#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200413
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200414#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200415 if( src->ticket != NULL )
416 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200417 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200418 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200419 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200420
421 memcpy( dst->ticket, src->ticket, src->ticket_len );
422 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200423#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200424
425 return( 0 );
426}
427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
429int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200430 const unsigned char *key_enc, const unsigned char *key_dec,
431 size_t keylen,
432 const unsigned char *iv_enc, const unsigned char *iv_dec,
433 size_t ivlen,
434 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200435 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
437int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
438int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
439int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
440int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000442
Paul Bakker5121ce52009-01-03 21:22:43 +0000443/*
444 * Key material generation
445 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200446#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200447static int ssl3_prf( const unsigned char *secret, size_t slen,
448 const char *label,
449 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000450 unsigned char *dstbuf, size_t dlen )
451{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100452 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000453 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200454 mbedtls_md5_context md5;
455 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000456 unsigned char padding[16];
457 unsigned char sha1sum[20];
458 ((void)label);
459
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200460 mbedtls_md5_init( &md5 );
461 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200462
Paul Bakker5f70b252012-09-13 14:23:06 +0000463 /*
464 * SSLv3:
465 * block =
466 * MD5( secret + SHA1( 'A' + secret + random ) ) +
467 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
468 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
469 * ...
470 */
471 for( i = 0; i < dlen / 16; i++ )
472 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200473 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000474
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100475 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100476 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100477 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100478 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100479 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 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, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100483 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100484 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100486 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100487 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100488 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100489 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100490 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100491 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100492 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100493 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000494 }
495
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100496exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200497 mbedtls_md5_free( &md5 );
498 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000499
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500500 mbedtls_platform_zeroize( padding, sizeof( padding ) );
501 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000502
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100503 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000504}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200508static int tls1_prf( const unsigned char *secret, size_t slen,
509 const char *label,
510 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000511 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000512{
Paul Bakker23986e52011-04-24 08:57:21 +0000513 size_t nb, hs;
514 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200515 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300516 unsigned char *tmp;
517 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000518 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200519 const mbedtls_md_info_t *md_info;
520 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100521 int ret;
522
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000524
Ron Eldor3b350852019-05-07 18:31:49 +0300525 tmp_len = 20 + strlen( label ) + rlen;
526 tmp = mbedtls_calloc( 1, tmp_len );
527 if( tmp == NULL )
528 {
529 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
530 goto exit;
531 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000532
533 hs = ( slen + 1 ) / 2;
534 S1 = secret;
535 S2 = secret + slen - hs;
536
537 nb = strlen( label );
538 memcpy( tmp + 20, label, nb );
539 memcpy( tmp + 20 + nb, random, rlen );
540 nb += rlen;
541
542 /*
543 * First compute P_md5(secret,label+random)[0..dlen]
544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300546 {
547 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
548 goto exit;
549 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300552 {
553 goto exit;
554 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200556 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
557 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
558 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000559
560 for( i = 0; i < dlen; i += 16 )
561 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_md_hmac_reset ( &md_ctx );
563 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
564 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100565
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 );
568 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000569
570 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
571
572 for( j = 0; j < k; j++ )
573 dstbuf[i + j] = h_i[j];
574 }
575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100577
Paul Bakker5121ce52009-01-03 21:22:43 +0000578 /*
579 * XOR out with P_sha1(secret,label+random)[0..dlen]
580 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300582 {
583 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
584 goto exit;
585 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200587 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300588 {
589 goto exit;
590 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
593 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
594 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000595
596 for( i = 0; i < dlen; i += 20 )
597 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200598 mbedtls_md_hmac_reset ( &md_ctx );
599 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
600 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100601
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 );
604 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000605
606 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
607
608 for( j = 0; j < k; j++ )
609 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
610 }
611
Ron Eldor3b350852019-05-07 18:31:49 +0300612exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200613 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100614
Ron Eldor3b350852019-05-07 18:31:49 +0300615 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500616 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000617
Ron Eldor3b350852019-05-07 18:31:49 +0300618 mbedtls_free( tmp );
619 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200621#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200623#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500624#if defined(MBEDTLS_USE_PSA_CRYPTO)
625static int tls_prf_generic( mbedtls_md_type_t md_type,
626 const unsigned char *secret, size_t slen,
627 const char *label,
628 const unsigned char *random, size_t rlen,
629 unsigned char *dstbuf, size_t dlen )
630{
631 psa_status_t status;
632 psa_algorithm_t alg;
633 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500634 psa_key_handle_t master_slot;
Janos Follath7374ee62019-07-30 12:43:12 +0100635 psa_crypto_generator_t generator = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500636
Andrzej Kurek2f760752019-01-28 08:08:15 -0500637 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500638 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500639
Andrzej Kurekc929a822019-01-14 03:51:11 -0500640 if( md_type == MBEDTLS_MD_SHA384 )
641 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
642 else
643 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
644
Andrzej Kurek2f760752019-01-28 08:08:15 -0500645 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500646 psa_key_policy_set_usage( &policy,
647 PSA_KEY_USAGE_DERIVE,
648 alg );
649 status = psa_set_key_policy( master_slot, &policy );
650 if( status != PSA_SUCCESS )
651 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
652
653 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
654 if( status != PSA_SUCCESS )
655 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
656
657 status = psa_key_derivation( &generator,
658 master_slot, alg,
659 random, rlen,
660 (unsigned char const *) label,
661 (size_t) strlen( label ),
662 dlen );
663 if( status != PSA_SUCCESS )
664 {
665 psa_generator_abort( &generator );
666 psa_destroy_key( master_slot );
667 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
668 }
669
670 status = psa_generator_read( &generator, dstbuf, dlen );
671 if( status != PSA_SUCCESS )
672 {
673 psa_generator_abort( &generator );
674 psa_destroy_key( master_slot );
675 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
676 }
677
678 status = psa_generator_abort( &generator );
679 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500680 {
681 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500682 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500683 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500684
685 status = psa_destroy_key( master_slot );
686 if( status != PSA_SUCCESS )
687 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
688
Andrzej Kurek33171262019-01-15 03:25:18 -0500689 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500690}
691
692#else /* MBEDTLS_USE_PSA_CRYPTO */
693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200694static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100695 const unsigned char *secret, size_t slen,
696 const char *label,
697 const unsigned char *random, size_t rlen,
698 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000699{
700 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100701 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300702 unsigned char *tmp;
703 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
705 const mbedtls_md_info_t *md_info;
706 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100707 int ret;
708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
712 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100715
Ron Eldor3b350852019-05-07 18:31:49 +0300716 tmp_len = md_len + strlen( label ) + rlen;
717 tmp = mbedtls_calloc( 1, tmp_len );
718 if( tmp == NULL )
719 {
720 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
721 goto exit;
722 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000723
724 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100725 memcpy( tmp + md_len, label, nb );
726 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727 nb += rlen;
728
729 /*
730 * Compute P_<hash>(secret, label + random)[0..dlen]
731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200732 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300733 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
736 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
737 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100738
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100739 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000740 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200741 mbedtls_md_hmac_reset ( &md_ctx );
742 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
743 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100744
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 );
747 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000748
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100749 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000750
751 for( j = 0; j < k; j++ )
752 dstbuf[i + j] = h_i[j];
753 }
754
Ron Eldor3b350852019-05-07 18:31:49 +0300755exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100757
Ron Eldor3b350852019-05-07 18:31:49 +0300758 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500759 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000760
Ron Eldor3b350852019-05-07 18:31:49 +0300761 mbedtls_free( tmp );
762
763 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000764}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500765#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200766#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100767static int tls_prf_sha256( const unsigned char *secret, size_t slen,
768 const char *label,
769 const unsigned char *random, size_t rlen,
770 unsigned char *dstbuf, size_t dlen )
771{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100773 label, random, rlen, dstbuf, dlen ) );
774}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200777#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200778static int tls_prf_sha384( const unsigned char *secret, size_t slen,
779 const char *label,
780 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000781 unsigned char *dstbuf, size_t dlen )
782{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200783 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100784 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000785}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#endif /* MBEDTLS_SHA512_C */
787#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000788
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200789static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
792 defined(MBEDTLS_SSL_PROTO_TLS1_1)
793static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794#endif
Paul Bakker380da532012-04-18 16:10:25 +0000795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_SSL_PROTO_SSL3)
797static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
798static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200799#endif
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
802static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
803static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200804#endif
805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200806#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
807#if defined(MBEDTLS_SHA256_C)
808static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
809static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
810static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200811#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100812
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#if defined(MBEDTLS_SHA512_C)
814static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
815static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
816static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100817#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200818#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000819
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100820#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100821 defined(MBEDTLS_USE_PSA_CRYPTO)
822static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
823{
824 if( ssl->conf->f_psk != NULL )
825 {
826 /* If we've used a callback to select the PSK,
827 * the static configuration is irrelevant. */
828 if( ssl->handshake->psk_opaque != 0 )
829 return( 1 );
830
831 return( 0 );
832 }
833
834 if( ssl->conf->psk_opaque != 0 )
835 return( 1 );
836
837 return( 0 );
838}
839#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100840 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100841
Ron Eldorcf280092019-05-14 20:19:13 +0300842#if defined(MBEDTLS_SSL_EXPORT_KEYS)
843static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
844{
845#if defined(MBEDTLS_SSL_PROTO_SSL3)
846 if( tls_prf == ssl3_prf )
847 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300848 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300849 }
850 else
851#endif
852#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
853 if( tls_prf == tls1_prf )
854 {
855 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
856 }
857 else
858#endif
859#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
860#if defined(MBEDTLS_SHA512_C)
861 if( tls_prf == tls_prf_sha384 )
862 {
863 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
864 }
865 else
866#endif
867#if defined(MBEDTLS_SHA256_C)
868 if( tls_prf == tls_prf_sha256 )
869 {
870 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
871 }
872 else
873#endif
874#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
875 return( MBEDTLS_SSL_TLS_PRF_NONE );
876}
877#endif /* MBEDTLS_SSL_EXPORT_KEYS */
878
Ron Eldor51d3ab52019-05-12 14:54:30 +0300879int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
880 const unsigned char *secret, size_t slen,
881 const char *label,
882 const unsigned char *random, size_t rlen,
883 unsigned char *dstbuf, size_t dlen )
884{
885 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
886
887 switch( prf )
888 {
889#if defined(MBEDTLS_SSL_PROTO_SSL3)
890 case MBEDTLS_SSL_TLS_PRF_SSL3:
891 tls_prf = ssl3_prf;
892 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300893#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300894#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
895 case MBEDTLS_SSL_TLS_PRF_TLS1:
896 tls_prf = tls1_prf;
897 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300898#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
899
900#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300901#if defined(MBEDTLS_SHA512_C)
902 case MBEDTLS_SSL_TLS_PRF_SHA384:
903 tls_prf = tls_prf_sha384;
904 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300905#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300906#if defined(MBEDTLS_SHA256_C)
907 case MBEDTLS_SSL_TLS_PRF_SHA256:
908 tls_prf = tls_prf_sha256;
909 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300910#endif /* MBEDTLS_SHA256_C */
911#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300912 default:
913 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
914 }
915
916 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
917}
918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200919int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000920{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200921 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000922#if defined(MBEDTLS_USE_PSA_CRYPTO)
923 int psa_fallthrough;
924#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000925 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 unsigned char keyblk[256];
927 unsigned char *key1;
928 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100929 unsigned char *mac_enc;
930 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000931 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200932 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000933 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000934 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200935 const mbedtls_cipher_info_t *cipher_info;
936 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100937
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000938 /* cf. RFC 5246, Section 8.1:
939 * "The master secret is always exactly 48 bytes in length." */
940 size_t const master_secret_len = 48;
941
Hanno Becker35b23c72018-10-23 12:10:41 +0100942#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
943 unsigned char session_hash[48];
944#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200946 mbedtls_ssl_session *session = ssl->session_negotiate;
947 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
948 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000951
Jaeden Amero2de07f12019-06-05 13:32:08 +0100952#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
953 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000954 transform->encrypt_then_mac = session->encrypt_then_mac;
955#endif
956 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000957
958 ciphersuite_info = handshake->ciphersuite_info;
959 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100960 if( cipher_info == NULL )
961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000963 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200964 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100965 }
966
Hanno Beckere694c3e2017-12-27 21:34:08 +0000967 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100968 if( md_info == NULL )
969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200970 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000971 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200972 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100973 }
974
Hanno Beckera0e20d02019-05-15 14:03:01 +0100975#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100976 /* Copy own and peer's CID if the use of the CID
977 * extension has been negotiated. */
978 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
979 {
980 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100981
Hanno Becker05154c32019-05-03 15:23:51 +0100982 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100983 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100984 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100985 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100986
987 transform->out_cid_len = ssl->handshake->peer_cid_len;
988 memcpy( transform->out_cid, ssl->handshake->peer_cid,
989 ssl->handshake->peer_cid_len );
990 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
991 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100992 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100993#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +0100994
Paul Bakker5121ce52009-01-03 21:22:43 +0000995 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000996 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200998#if defined(MBEDTLS_SSL_PROTO_SSL3)
999 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 {
Paul Bakker48916f92012-09-16 19:57:18 +00001001 handshake->tls_prf = ssl3_prf;
1002 handshake->calc_verify = ssl_calc_verify_ssl;
1003 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001004 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001005 else
1006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1008 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 {
Paul Bakker48916f92012-09-16 19:57:18 +00001010 handshake->tls_prf = tls1_prf;
1011 handshake->calc_verify = ssl_calc_verify_tls;
1012 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014 else
1015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1017#if defined(MBEDTLS_SHA512_C)
1018 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001019 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001020 {
Paul Bakker48916f92012-09-16 19:57:18 +00001021 handshake->tls_prf = tls_prf_sha384;
1022 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1023 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001024 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001025 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001026#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001027#if defined(MBEDTLS_SHA256_C)
1028 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001029 {
Paul Bakker48916f92012-09-16 19:57:18 +00001030 handshake->tls_prf = tls_prf_sha256;
1031 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1032 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001033 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001034 else
1035#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001037 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1039 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001040 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001041
1042 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001043 * SSLv3:
1044 * master =
1045 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1046 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1047 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001048 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001049 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001050 * master = PRF( premaster, "master secret", randbytes )[0..47]
1051 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001052 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001053 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001054 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1055 }
1056 else
1057 {
1058 /* The label for the KDF used for key expansion.
1059 * This is either "master secret" or "extended master secret"
1060 * depending on whether the Extended Master Secret extension
1061 * is used. */
1062 char const *lbl = "master secret";
1063
1064 /* The salt for the KDF used for key expansion.
1065 * - If the Extended Master Secret extension is not used,
1066 * this is ClientHello.Random + ServerHello.Random
1067 * (see Sect. 8.1 in RFC 5246).
1068 * - If the Extended Master Secret extension is used,
1069 * this is the transcript of the handshake so far.
1070 * (see Sect. 4 in RFC 7627). */
1071 unsigned char const *salt = handshake->randbytes;
1072 size_t salt_len = 64;
1073
1074#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001078
Hanno Becker35b23c72018-10-23 12:10:41 +01001079 lbl = "extended master secret";
1080 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001081 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1083 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001084 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001085#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001086 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1087 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001088 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001089 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001090 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001091#endif /* MBEDTLS_SHA512_C */
1092 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001093 }
1094 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001095#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001096 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001097
Hanno Becker35b23c72018-10-23 12:10:41 +01001098 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001099 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001100#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1101
Hanno Becker7d0a5692018-10-23 15:26:22 +01001102#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1103 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1104 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1105 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1106 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001107 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001108 /* Perform PSK-to-MS expansion in a single step. */
1109 psa_status_t status;
1110 psa_algorithm_t alg;
Janos Follath7374ee62019-07-30 12:43:12 +01001111 psa_crypto_generator_t generator = PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001112 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001113
1114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1115
1116 psk = ssl->conf->psk_opaque;
1117 if( ssl->handshake->psk_opaque != 0 )
1118 psk = ssl->handshake->psk_opaque;
1119
Hanno Becker22bf1452019-04-05 11:21:08 +01001120 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001121 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1122 else
1123 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1124
1125 status = psa_key_derivation( &generator, psk, alg,
1126 salt, salt_len,
1127 (unsigned char const *) lbl,
1128 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001129 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001130 if( status != PSA_SUCCESS )
1131 {
1132 psa_generator_abort( &generator );
1133 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1134 }
1135
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001136 status = psa_generator_read( &generator, session->master,
1137 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001138 if( status != PSA_SUCCESS )
1139 {
1140 psa_generator_abort( &generator );
1141 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1142 }
1143
1144 status = psa_generator_abort( &generator );
1145 if( status != PSA_SUCCESS )
1146 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001147 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001148 else
1149#endif
1150 {
1151 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1152 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001153 session->master,
1154 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001155 if( ret != 0 )
1156 {
1157 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1158 return( ret );
1159 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001160
Hanno Becker7d0a5692018-10-23 15:26:22 +01001161 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1162 handshake->premaster,
1163 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001164
Hanno Becker7d0a5692018-10-23 15:26:22 +01001165 mbedtls_platform_zeroize( handshake->premaster,
1166 sizeof(handshake->premaster) );
1167 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001168 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001169
1170 /*
1171 * Swap the client and server random values.
1172 */
Paul Bakker48916f92012-09-16 19:57:18 +00001173 memcpy( tmp, handshake->randbytes, 64 );
1174 memcpy( handshake->randbytes, tmp + 32, 32 );
1175 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001176 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
1178 /*
1179 * SSLv3:
1180 * key block =
1181 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1182 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1183 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1184 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1185 * ...
1186 *
1187 * TLSv1:
1188 * key block = PRF( master, "key expansion", randbytes )
1189 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001190 ret = handshake->tls_prf( session->master, 48, "key expansion",
1191 handshake->randbytes, 64, keyblk, 256 );
1192 if( ret != 0 )
1193 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001194 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001195 return( ret );
1196 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001198 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1199 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1200 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1201 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1202 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001203
Paul Bakker5121ce52009-01-03 21:22:43 +00001204 /*
1205 * Determine the appropriate key, IV and MAC length.
1206 */
Paul Bakker68884e32013-01-07 18:20:04 +01001207
Hanno Becker88aaf652017-12-27 08:17:40 +00001208 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001209
Hanno Becker8031d062018-01-03 15:32:31 +00001210#if defined(MBEDTLS_GCM_C) || \
1211 defined(MBEDTLS_CCM_C) || \
1212 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001213 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001214 cipher_info->mode == MBEDTLS_MODE_CCM ||
1215 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001216 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001217 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001218
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001219 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001220 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001221 transform->taglen =
1222 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001223
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001224 /* All modes haves 96-bit IVs;
1225 * GCM and CCM has 4 implicit and 8 explicit bytes
1226 * ChachaPoly has all 12 bytes implicit
1227 */
Paul Bakker68884e32013-01-07 18:20:04 +01001228 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001229 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1230 transform->fixed_ivlen = 12;
1231 else
1232 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001233
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001234 /* Minimum length of encrypted record */
1235 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001236 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001237 }
1238 else
Hanno Becker8031d062018-01-03 15:32:31 +00001239#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1240#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1241 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1242 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001243 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001244 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1246 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001247 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001248 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001249 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001250 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001251
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001252 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001253 mac_key_len = mbedtls_md_get_size( md_info );
1254 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001256#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001257 /*
1258 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1259 * (rfc 6066 page 13 or rfc 2104 section 4),
1260 * so we only need to adjust the length here.
1261 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001262 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001265
1266#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1267 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001268 * HMAC implementation which also truncates the key
1269 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001270 mac_key_len = transform->maclen;
1271#endif
1272 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001273#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001274
1275 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001276 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001277
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001278 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001279 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001280 transform->minlen = transform->maclen;
1281 else
Paul Bakker68884e32013-01-07 18:20:04 +01001282 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001283 /*
1284 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001285 * 1. if EtM is in use: one block plus MAC
1286 * otherwise: * first multiple of blocklen greater than maclen
1287 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1290 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001291 {
1292 transform->minlen = transform->maclen
1293 + cipher_info->block_size;
1294 }
1295 else
1296#endif
1297 {
1298 transform->minlen = transform->maclen
1299 + cipher_info->block_size
1300 - transform->maclen % cipher_info->block_size;
1301 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001303#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1304 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1305 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001306 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001307 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001308#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001309#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1310 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1311 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001312 {
1313 transform->minlen += transform->ivlen;
1314 }
1315 else
1316#endif
1317 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001319 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1320 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001321 }
Paul Bakker68884e32013-01-07 18:20:04 +01001322 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001323 }
Hanno Becker8031d062018-01-03 15:32:31 +00001324 else
1325#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1326 {
1327 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1328 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1329 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001330
Hanno Becker88aaf652017-12-27 08:17:40 +00001331 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1332 (unsigned) keylen,
1333 (unsigned) transform->minlen,
1334 (unsigned) transform->ivlen,
1335 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001336
1337 /*
1338 * Finally setup the cipher contexts, IVs and MAC secrets.
1339 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001340#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001341 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001343 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001344 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Paul Bakker68884e32013-01-07 18:20:04 +01001346 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001347 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001349 /*
1350 * This is not used in TLS v1.1.
1351 */
Paul Bakker48916f92012-09-16 19:57:18 +00001352 iv_copy_len = ( transform->fixed_ivlen ) ?
1353 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001354 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1355 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
1358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_CLI_C */
1360#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001361 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001362 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001363 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001364 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Hanno Becker81c7b182017-11-09 18:39:33 +00001366 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001367 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001368
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001369 /*
1370 * This is not used in TLS v1.1.
1371 */
Paul Bakker48916f92012-09-16 19:57:18 +00001372 iv_copy_len = ( transform->fixed_ivlen ) ?
1373 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001374 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1375 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001376 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001377 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001378 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001379#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001382 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1383 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001384 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001385
Hanno Beckerd56ed242018-01-03 15:32:51 +00001386#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001387#if defined(MBEDTLS_SSL_PROTO_SSL3)
1388 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001389 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001390 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001391 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001392 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001393 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1394 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001395 }
1396
Hanno Becker81c7b182017-11-09 18:39:33 +00001397 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1398 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001399 }
1400 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001401#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1402#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1403 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1404 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001405 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001406 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1407 For AEAD-based ciphersuites, there is nothing to do here. */
1408 if( mac_key_len != 0 )
1409 {
1410 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1411 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1412 }
Paul Bakker68884e32013-01-07 18:20:04 +01001413 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001414 else
1415#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001416 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001417 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001418 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1419 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001420 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001421#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001423#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1424 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001425 {
1426 int ret = 0;
1427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001429
Hanno Becker88aaf652017-12-27 08:17:40 +00001430 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001431 transform->iv_enc, transform->iv_dec,
1432 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001433 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001434 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001437 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1438 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001439 }
1440 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001441#else
1442 ((void) mac_dec);
1443 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001444#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001445
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001446#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1447 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001448 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001449 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1450 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001451 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001452 iv_copy_len );
1453 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001454
1455 if( ssl->conf->f_export_keys_ext != NULL )
1456 {
1457 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1458 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001459 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001460 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001461 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001462 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001463 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001464 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001465#endif
1466
Hanno Beckerf704bef2018-11-16 15:21:18 +00001467#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001468
1469 /* Only use PSA-based ciphers for TLS-1.2.
1470 * That's relevant at least for TLS-1.0, where
1471 * we assume that mbedtls_cipher_crypt() updates
1472 * the structure field for the IV, which the PSA-based
1473 * implementation currently doesn't. */
1474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1475 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001476 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001477 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001478 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001479 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1480 {
1481 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001482 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001483 }
1484
1485 if( ret == 0 )
1486 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001487 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001488 psa_fallthrough = 0;
1489 }
1490 else
1491 {
1492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1493 psa_fallthrough = 1;
1494 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001495 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001496 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001497 psa_fallthrough = 1;
1498#else
1499 psa_fallthrough = 1;
1500#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001501
Hanno Beckercb1cc802018-11-17 22:27:38 +00001502 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001503#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001504 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001505 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001506 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001507 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001508 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001509 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001510
Hanno Beckerf704bef2018-11-16 15:21:18 +00001511#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001512 /* Only use PSA-based ciphers for TLS-1.2.
1513 * That's relevant at least for TLS-1.0, where
1514 * we assume that mbedtls_cipher_crypt() updates
1515 * the structure field for the IV, which the PSA-based
1516 * implementation currently doesn't. */
1517#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1518 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001519 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001520 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001521 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001522 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1523 {
1524 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001525 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001526 }
1527
1528 if( ret == 0 )
1529 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001530 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001531 psa_fallthrough = 0;
1532 }
1533 else
1534 {
1535 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1536 psa_fallthrough = 1;
1537 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001538 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001539 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001540 psa_fallthrough = 1;
1541#else
1542 psa_fallthrough = 1;
1543#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001544
Hanno Beckercb1cc802018-11-17 22:27:38 +00001545 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001546#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 cipher_info ) ) != 0 )
1549 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001550 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001551 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001552 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001555 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001559 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001560 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001563 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001567 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001568 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_CIPHER_MODE_CBC)
1571 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1574 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001577 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001578 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1581 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001584 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001585 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001586 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001588
Paul Bakker5121ce52009-01-03 21:22:43 +00001589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 // Initialize compression
1592 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001593 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001594 {
Paul Bakker16770332013-10-11 09:59:44 +02001595 if( ssl->compress_buf == NULL )
1596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001598 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001599 if( ssl->compress_buf == NULL )
1600 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001601 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001602 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001603 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1604 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001605 }
1606 }
1607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609
Paul Bakker48916f92012-09-16 19:57:18 +00001610 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1611 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001612
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001613 if( deflateInit( &transform->ctx_deflate,
1614 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001615 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001616 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001618 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1619 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001620 }
1621 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001625end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001626 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1627 mbedtls_platform_zeroize( handshake->randbytes,
1628 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001629 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630}
1631
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001632#if defined(MBEDTLS_SSL_PROTO_SSL3)
1633void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001634{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001635 mbedtls_md5_context md5;
1636 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001637 unsigned char pad_1[48];
1638 unsigned char pad_2[48];
1639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001641
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001642 mbedtls_md5_init( &md5 );
1643 mbedtls_sha1_init( &sha1 );
1644
1645 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1646 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Paul Bakker380da532012-04-18 16:10:25 +00001648 memset( pad_1, 0x36, 48 );
1649 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001651 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1652 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1653 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001654
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001655 mbedtls_md5_starts_ret( &md5 );
1656 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1657 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1658 mbedtls_md5_update_ret( &md5, hash, 16 );
1659 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001660
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001661 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1662 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1663 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001664
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001665 mbedtls_sha1_starts_ret( &sha1 );
1666 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1667 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1668 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1669 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001671 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001673
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001674 mbedtls_md5_free( &md5 );
1675 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001676
Paul Bakker380da532012-04-18 16:10:25 +00001677 return;
1678}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001679#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001680
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1682void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001683{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001684 mbedtls_md5_context md5;
1685 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001688
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001689 mbedtls_md5_init( &md5 );
1690 mbedtls_sha1_init( &sha1 );
1691
1692 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1693 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001694
Andrzej Kurekeb342242019-01-29 09:14:33 -05001695 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001696 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001698 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001700
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001701 mbedtls_md5_free( &md5 );
1702 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001703
Paul Bakker380da532012-04-18 16:10:25 +00001704 return;
1705}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1709#if defined(MBEDTLS_SHA256_C)
1710void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001711{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001712#if defined(MBEDTLS_USE_PSA_CRYPTO)
1713 size_t hash_size;
1714 psa_status_t status;
1715 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1716
1717 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1718 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1719 if( status != PSA_SUCCESS )
1720 {
1721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1722 return;
1723 }
1724
1725 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1726 if( status != PSA_SUCCESS )
1727 {
1728 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1729 return;
1730 }
1731 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1732 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1733#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001734 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001735
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001736 mbedtls_sha256_init( &sha256 );
1737
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001739
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001740 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001741 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001743 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001745
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001746 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001747#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001748 return;
1749}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001750#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001751
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#if defined(MBEDTLS_SHA512_C)
1753void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001754{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001755#if defined(MBEDTLS_USE_PSA_CRYPTO)
1756 size_t hash_size;
1757 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001758 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001759
1760 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001761 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001762 if( status != PSA_SUCCESS )
1763 {
1764 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1765 return;
1766 }
1767
Andrzej Kurek972fba52019-01-30 03:29:12 -05001768 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001769 if( status != PSA_SUCCESS )
1770 {
1771 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1772 return;
1773 }
1774 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1775 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1776#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001777 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001778
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001779 mbedtls_sha512_init( &sha512 );
1780
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001781 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001782
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001783 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001784 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1787 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001788
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001789 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001790#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001791 return;
1792}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#endif /* MBEDTLS_SHA512_C */
1794#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001796#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1797int 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 +02001798{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001799 unsigned char *p = ssl->handshake->premaster;
1800 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001801 const unsigned char *psk = ssl->conf->psk;
1802 size_t psk_len = ssl->conf->psk_len;
1803
1804 /* If the psk callback was called, use its result */
1805 if( ssl->handshake->psk != NULL )
1806 {
1807 psk = ssl->handshake->psk;
1808 psk_len = ssl->handshake->psk_len;
1809 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001810
1811 /*
1812 * PMS = struct {
1813 * opaque other_secret<0..2^16-1>;
1814 * opaque psk<0..2^16-1>;
1815 * };
1816 * with "other_secret" depending on the particular key exchange
1817 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001818#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1819 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001820 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001821 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001822 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001823
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001824 *(p++) = (unsigned char)( psk_len >> 8 );
1825 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001826
1827 if( end < p || (size_t)( end - p ) < psk_len )
1828 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1829
1830 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001831 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001832 }
1833 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001834#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1835#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1836 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001837 {
1838 /*
1839 * other_secret already set by the ClientKeyExchange message,
1840 * and is 48 bytes long
1841 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001842 if( end - p < 2 )
1843 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1844
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001845 *p++ = 0;
1846 *p++ = 48;
1847 p += 48;
1848 }
1849 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001850#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1851#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1852 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001854 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001855 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001856
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001857 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001858 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001859 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001860 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001861 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001862 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001863 return( ret );
1864 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001865 *(p++) = (unsigned char)( len >> 8 );
1866 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001867 p += len;
1868
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001870 }
1871 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001872#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1873#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1874 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001875 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001876 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 size_t zlen;
1878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001879 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001880 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001881 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001884 return( ret );
1885 }
1886
1887 *(p++) = (unsigned char)( zlen >> 8 );
1888 *(p++) = (unsigned char)( zlen );
1889 p += zlen;
1890
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001891 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1892 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 }
1894 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001895#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1898 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899 }
1900
1901 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001902 if( end - p < 2 )
1903 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001904
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001905 *(p++) = (unsigned char)( psk_len >> 8 );
1906 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001907
1908 if( end < p || (size_t)( end - p ) < psk_len )
1909 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1910
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001911 memcpy( p, psk, psk_len );
1912 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001913
1914 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1915
1916 return( 0 );
1917}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001921/*
1922 * SSLv3.0 MAC functions
1923 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001924#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001925static void ssl_mac( mbedtls_md_context_t *md_ctx,
1926 const unsigned char *secret,
1927 const unsigned char *buf, size_t len,
1928 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001929 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001930{
1931 unsigned char header[11];
1932 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001933 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001934 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1935 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001936
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001937 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001938 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001939 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001940 else
Paul Bakker68884e32013-01-07 18:20:04 +01001941 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001942
1943 memcpy( header, ctr, 8 );
1944 header[ 8] = (unsigned char) type;
1945 header[ 9] = (unsigned char)( len >> 8 );
1946 header[10] = (unsigned char)( len );
1947
Paul Bakker68884e32013-01-07 18:20:04 +01001948 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001949 mbedtls_md_starts( md_ctx );
1950 mbedtls_md_update( md_ctx, secret, md_size );
1951 mbedtls_md_update( md_ctx, padding, padlen );
1952 mbedtls_md_update( md_ctx, header, 11 );
1953 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001954 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001955
Paul Bakker68884e32013-01-07 18:20:04 +01001956 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001957 mbedtls_md_starts( md_ctx );
1958 mbedtls_md_update( md_ctx, secret, md_size );
1959 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001960 mbedtls_md_update( md_ctx, out, md_size );
1961 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001963#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001964
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001965/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001966 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001967#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001968 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1969 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1970 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1971/* This function makes sure every byte in the memory region is accessed
1972 * (in ascending addresses order) */
1973static void ssl_read_memory( unsigned char *p, size_t len )
1974{
1975 unsigned char acc = 0;
1976 volatile unsigned char force;
1977
1978 for( ; len != 0; p++, len-- )
1979 acc ^= *p;
1980
1981 force = acc;
1982 (void) force;
1983}
1984#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1985
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001986/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001987 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001988 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001989
Hanno Beckera0e20d02019-05-15 14:03:01 +01001990#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001991/* This functions transforms a DTLS plaintext fragment and a record content
1992 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001993 *
1994 * struct {
1995 * opaque content[DTLSPlaintext.length];
1996 * ContentType real_type;
1997 * uint8 zeros[length_of_padding];
1998 * } DTLSInnerPlaintext;
1999 *
2000 * Input:
2001 * - `content`: The beginning of the buffer holding the
2002 * plaintext to be wrapped.
2003 * - `*content_size`: The length of the plaintext in Bytes.
2004 * - `max_len`: The number of Bytes available starting from
2005 * `content`. This must be `>= *content_size`.
2006 * - `rec_type`: The desired record content type.
2007 *
2008 * Output:
2009 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2010 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2011 *
2012 * Returns:
2013 * - `0` on success.
2014 * - A negative error code if `max_len` didn't offer enough space
2015 * for the expansion.
2016 */
2017static int ssl_cid_build_inner_plaintext( unsigned char *content,
2018 size_t *content_size,
2019 size_t remaining,
2020 uint8_t rec_type )
2021{
2022 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002023 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2024 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2025 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002026
2027 /* Write real content type */
2028 if( remaining == 0 )
2029 return( -1 );
2030 content[ len ] = rec_type;
2031 len++;
2032 remaining--;
2033
2034 if( remaining < pad )
2035 return( -1 );
2036 memset( content + len, 0, pad );
2037 len += pad;
2038 remaining -= pad;
2039
2040 *content_size = len;
2041 return( 0 );
2042}
2043
Hanno Becker07dc97d2019-05-20 15:08:01 +01002044/* This function parses a DTLSInnerPlaintext structure.
2045 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002046static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2047 size_t *content_size,
2048 uint8_t *rec_type )
2049{
2050 size_t remaining = *content_size;
2051
2052 /* Determine length of padding by skipping zeroes from the back. */
2053 do
2054 {
2055 if( remaining == 0 )
2056 return( -1 );
2057 remaining--;
2058 } while( content[ remaining ] == 0 );
2059
2060 *content_size = remaining;
2061 *rec_type = content[ remaining ];
2062
2063 return( 0 );
2064}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002065#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002066
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002067/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002068 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002069static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002070 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002071 mbedtls_record *rec )
2072{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002073 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002074 *
2075 * additional_data = seq_num + TLSCompressed.type +
2076 * TLSCompressed.version + TLSCompressed.length;
2077 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002078 * For the CID extension, this is extended as follows
2079 * (quoting draft-ietf-tls-dtls-connection-id-05,
2080 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002081 *
2082 * additional_data = seq_num + DTLSPlaintext.type +
2083 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002084 * cid +
2085 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002086 * length_of_DTLSInnerPlaintext;
2087 */
2088
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002089 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2090 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002091 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002092
Hanno Beckera0e20d02019-05-15 14:03:01 +01002093#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002094 if( rec->cid_len != 0 )
2095 {
2096 memcpy( add_data + 11, rec->cid, rec->cid_len );
2097 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2098 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2099 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2100 *add_data_len = 13 + 1 + rec->cid_len;
2101 }
2102 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002103#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002104 {
2105 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2106 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2107 *add_data_len = 13;
2108 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002109}
2110
Hanno Beckera18d1322018-01-03 14:27:32 +00002111int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2112 mbedtls_ssl_transform *transform,
2113 mbedtls_record *rec,
2114 int (*f_rng)(void *, unsigned char *, size_t),
2115 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002116{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002117 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002118 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002119 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002120 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002121 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 size_t post_avail;
2123
2124 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002125#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002126 ((void) ssl);
2127#endif
2128
2129 /* The PRNG is used for dynamic IV generation that's used
2130 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2131#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2132 ( defined(MBEDTLS_AES_C) || \
2133 defined(MBEDTLS_ARIA_C) || \
2134 defined(MBEDTLS_CAMELLIA_C) ) && \
2135 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2136 ((void) f_rng);
2137 ((void) p_rng);
2138#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002140 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002142 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002143 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002144 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2145 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2146 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002147 if( rec == NULL
2148 || rec->buf == NULL
2149 || rec->buf_len < rec->data_offset
2150 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002151#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002152 || rec->cid_len != 0
2153#endif
2154 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002155 {
2156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002157 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002158 }
2159
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002160 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002161 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002162 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002163 data, rec->data_len );
2164
2165 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2166
2167 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2168 {
2169 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2170 (unsigned) rec->data_len,
2171 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2172 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2173 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002174
Hanno Beckera0e20d02019-05-15 14:03:01 +01002175#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002176 /*
2177 * Add CID information
2178 */
2179 rec->cid_len = transform->out_cid_len;
2180 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2181 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002182
2183 if( rec->cid_len != 0 )
2184 {
2185 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002186 * Wrap plaintext into DTLSInnerPlaintext structure.
2187 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002188 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002189 * Note that this changes `rec->data_len`, and hence
2190 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002191 */
2192 if( ssl_cid_build_inner_plaintext( data,
2193 &rec->data_len,
2194 post_avail,
2195 rec->type ) != 0 )
2196 {
2197 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2198 }
2199
2200 rec->type = MBEDTLS_SSL_MSG_CID;
2201 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002202#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002203
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002204 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2205
Paul Bakker5121ce52009-01-03 21:22:43 +00002206 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002207 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 */
Hanno Becker52344c22018-01-03 15:24:20 +00002209#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002210 if( mode == MBEDTLS_MODE_STREAM ||
2211 ( mode == MBEDTLS_MODE_CBC
2212#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002213 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002214#endif
2215 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002216 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002217 if( post_avail < transform->maclen )
2218 {
2219 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2220 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2221 }
2222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002223#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002225 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002226 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002227 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2228 data, rec->data_len, rec->ctr, rec->type, mac );
2229 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002230 }
2231 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002233#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2234 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002235 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002236 {
Hanno Becker992b6872017-11-09 18:57:39 +00002237 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2238
Hanno Beckercab87e62019-04-29 13:52:53 +01002239 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002240
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002241 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002242 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2244 data, rec->data_len );
2245 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2246 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2247
2248 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002249 }
2250 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002251#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002253 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2254 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002255 }
2256
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2258 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002259
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002260 rec->data_len += transform->maclen;
2261 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002262 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002263 }
Hanno Becker52344c22018-01-03 15:24:20 +00002264#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002265
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002266 /*
2267 * Encrypt
2268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002269#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2270 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002271 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002272 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002273 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002274 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002275 "including %d bytes of padding",
2276 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002277
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002278 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2279 transform->iv_enc, transform->ivlen,
2280 data, rec->data_len,
2281 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002282 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002284 return( ret );
2285 }
2286
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002287 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002288 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002289 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2290 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002291 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002292 }
Paul Bakker68884e32013-01-07 18:20:04 +01002293 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002294#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002295
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002296#if defined(MBEDTLS_GCM_C) || \
2297 defined(MBEDTLS_CCM_C) || \
2298 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002299 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002300 mode == MBEDTLS_MODE_CCM ||
2301 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002302 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002303 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002304 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002305 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002306
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 /* Check that there's space for both the authentication tag
2308 * and the explicit IV before and after the record content. */
2309 if( post_avail < transform->taglen ||
2310 rec->data_offset < explicit_iv_len )
2311 {
2312 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2313 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2314 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002315
Paul Bakker68884e32013-01-07 18:20:04 +01002316 /*
2317 * Generate IV
2318 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002319 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2320 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002321 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002322 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002323 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2324 explicit_iv_len );
2325 /* Prefix record content with explicit IV. */
2326 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002327 }
2328 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2329 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002330 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002331 unsigned char i;
2332
2333 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2334
2335 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002336 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002337 }
2338 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002339 {
2340 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002341 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2342 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002343 }
2344
Hanno Beckercab87e62019-04-29 13:52:53 +01002345 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002346
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002347 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2348 iv, transform->ivlen );
2349 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002350 data - explicit_iv_len, explicit_iv_len );
2351 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002352 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002353 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002354 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002355 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002356
Paul Bakker68884e32013-01-07 18:20:04 +01002357 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002358 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002359 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002360
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002361 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002362 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002363 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002364 data, rec->data_len, /* source */
2365 data, &rec->data_len, /* destination */
2366 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002367 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002368 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002369 return( ret );
2370 }
2371
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002372 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2373 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002374
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002375 rec->data_len += transform->taglen + explicit_iv_len;
2376 rec->data_offset -= explicit_iv_len;
2377 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002378 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002379 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2382#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002383 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002384 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002385 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002386 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 size_t padlen, i;
2388 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002389
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002390 /* Currently we're always using minimal padding
2391 * (up to 255 bytes would be allowed). */
2392 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2393 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002394 padlen = 0;
2395
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002396 /* Check there's enough space in the buffer for the padding. */
2397 if( post_avail < padlen + 1 )
2398 {
2399 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2400 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2401 }
2402
Paul Bakker5121ce52009-01-03 21:22:43 +00002403 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002404 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002405
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 rec->data_len += padlen + 1;
2407 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002409#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002410 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002411 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2412 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002413 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002414 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002415 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002416 if( f_rng == NULL )
2417 {
2418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2419 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2420 }
2421
2422 if( rec->data_offset < transform->ivlen )
2423 {
2424 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2425 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2426 }
2427
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002428 /*
2429 * Generate IV
2430 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002431 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002432 if( ret != 0 )
2433 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002434
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002435 memcpy( data - transform->ivlen, transform->iv_enc,
2436 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002437
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002439#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002442 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002443 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002444 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002445
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002446 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2447 transform->iv_enc,
2448 transform->ivlen,
2449 data, rec->data_len,
2450 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002452 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002453 return( ret );
2454 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002455
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002456 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002457 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002458 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2459 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002460 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002462#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002463 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002464 {
2465 /*
2466 * Save IV in SSL3 and TLS1
2467 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002468 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2469 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002470 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002471 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002472#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002473 {
2474 data -= transform->ivlen;
2475 rec->data_offset -= transform->ivlen;
2476 rec->data_len += transform->ivlen;
2477 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002479#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002480 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002482 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2483
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002484 /*
2485 * MAC(MAC_write_key, seq_num +
2486 * TLSCipherText.type +
2487 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002488 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002489 * IV + // except for TLS 1.0
2490 * ENC(content + padding + padding_length));
2491 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002492
2493 if( post_avail < transform->maclen)
2494 {
2495 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2496 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2497 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002498
Hanno Beckercab87e62019-04-29 13:52:53 +01002499 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002501 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002502 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002503 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002504
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002505 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002506 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002507 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2508 data, rec->data_len );
2509 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2510 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002511
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002512 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002514 rec->data_len += transform->maclen;
2515 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002516 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002517 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002519 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002520 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002522 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2525 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002526 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002527
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002528 /* Make extra sure authentication was performed, exactly once */
2529 if( auth_done != 1 )
2530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002531 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2532 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002533 }
2534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002535 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002536
2537 return( 0 );
2538}
2539
Hanno Beckera18d1322018-01-03 14:27:32 +00002540int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2541 mbedtls_ssl_transform *transform,
2542 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002543{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002544 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002545 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002547#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002548 size_t padlen = 0, correct = 1;
2549#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002551 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002552 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002553
Hanno Beckera18d1322018-01-03 14:27:32 +00002554#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002555 ((void) ssl);
2556#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002558 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002559 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002560 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2562 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2563 }
2564 if( rec == NULL ||
2565 rec->buf == NULL ||
2566 rec->buf_len < rec->data_offset ||
2567 rec->buf_len - rec->data_offset < rec->data_len )
2568 {
2569 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002570 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002571 }
2572
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002573 data = rec->buf + rec->data_offset;
2574 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002575
Hanno Beckera0e20d02019-05-15 14:03:01 +01002576#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002577 /*
2578 * Match record's CID with incoming CID.
2579 */
Hanno Becker938489a2019-05-08 13:02:22 +01002580 if( rec->cid_len != transform->in_cid_len ||
2581 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2582 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002583 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002584 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002585#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002587#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2588 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002589 {
2590 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002591 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2592 transform->iv_dec,
2593 transform->ivlen,
2594 data, rec->data_len,
2595 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002597 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002598 return( ret );
2599 }
2600
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002601 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002602 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2604 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002605 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002606 }
Paul Bakker68884e32013-01-07 18:20:04 +01002607 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609#if defined(MBEDTLS_GCM_C) || \
2610 defined(MBEDTLS_CCM_C) || \
2611 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002612 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 mode == MBEDTLS_MODE_CCM ||
2614 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002615 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002616 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002617 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002618
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 /*
2620 * Compute and update sizes
2621 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002622 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002625 "+ taglen (%d)", rec->data_len,
2626 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002627 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002628 }
Paul Bakker68884e32013-01-07 18:20:04 +01002629
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002630 /*
2631 * Prepare IV
2632 */
2633 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2634 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002635 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002637 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002638
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 }
2640 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2641 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002642 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002643 unsigned char i;
2644
2645 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2646
2647 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002648 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002649 }
2650 else
2651 {
2652 /* Reminder if we ever add an AEAD mode with a different size */
2653 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2654 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2655 }
2656
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002657 data += explicit_iv_len;
2658 rec->data_offset += explicit_iv_len;
2659 rec->data_len -= explicit_iv_len + transform->taglen;
2660
Hanno Beckercab87e62019-04-29 13:52:53 +01002661 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002662 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002663 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664
2665 memcpy( transform->iv_dec + transform->fixed_ivlen,
2666 data - explicit_iv_len, explicit_iv_len );
2667
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002668 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002670 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002671
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002673 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002674 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002675 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2677 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002678 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002679 data, rec->data_len,
2680 data, &olen,
2681 data + rec->data_len,
2682 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2687 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002688
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002689 return( ret );
2690 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002691 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002692
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002693 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002694 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2696 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002697 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002698 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002699 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2701#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002702 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002703 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002705 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002706
Paul Bakker5121ce52009-01-03 21:22:43 +00002707 /*
Paul Bakker45829992013-01-03 14:52:21 +01002708 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002710#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002711 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2712 {
2713 /* The ciphertext is prefixed with the CBC IV. */
2714 minlen += transform->ivlen;
2715 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002716#endif
Paul Bakker45829992013-01-03 14:52:21 +01002717
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002718 /* Size considerations:
2719 *
2720 * - The CBC cipher text must not be empty and hence
2721 * at least of size transform->ivlen.
2722 *
2723 * Together with the potential IV-prefix, this explains
2724 * the first of the two checks below.
2725 *
2726 * - The record must contain a MAC, either in plain or
2727 * encrypted, depending on whether Encrypt-then-MAC
2728 * is used or not.
2729 * - If it is, the message contains the IV-prefix,
2730 * the CBC ciphertext, and the MAC.
2731 * - If it is not, the padded plaintext, and hence
2732 * the CBC ciphertext, has at least length maclen + 1
2733 * because there is at least the padding length byte.
2734 *
2735 * As the CBC ciphertext is not empty, both cases give the
2736 * lower bound minlen + maclen + 1 on the record size, which
2737 * we test for in the second check below.
2738 */
2739 if( rec->data_len < minlen + transform->ivlen ||
2740 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002742 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002743 "+ 1 ) ( + expl IV )", rec->data_len,
2744 transform->ivlen,
2745 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002746 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002747 }
2748
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002749 /*
2750 * Authenticate before decrypt if enabled
2751 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002752#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002753 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002754 {
Hanno Becker992b6872017-11-09 18:57:39 +00002755 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002757 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002758
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002759 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2760 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002761
Hanno Beckercab87e62019-04-29 13:52:53 +01002762 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002763
Hanno Beckercab87e62019-04-29 13:52:53 +01002764 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2765 add_data_len );
2766 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2767 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002768 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2769 data, rec->data_len );
2770 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2771 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002772
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2774 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002775 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002776 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002778 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2779 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002781 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002782 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002783 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002784 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002786#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787
2788 /*
2789 * Check length sanity
2790 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002792 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002793 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002794 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002796 }
2797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002798#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002799 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002800 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002802 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002804 /* This is safe because data_len >= minlen + maclen + 1 initially,
2805 * and at this point we have at most subtracted maclen (note that
2806 * minlen == transform->ivlen here). */
2807 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002808
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002809 data += transform->ivlen;
2810 rec->data_offset += transform->ivlen;
2811 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002812 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002813#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002815 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2816 transform->iv_dec, transform->ivlen,
2817 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002818 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002819 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002820 return( ret );
2821 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002822
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002823 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2826 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002827 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002829#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002830 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002831 {
2832 /*
2833 * Save IV in SSL3 and TLS1
2834 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002835 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2836 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002837 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002838#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002839
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002840 /* Safe since data_len >= minlen + maclen + 1, so after having
2841 * subtracted at most minlen and maclen up to this point,
2842 * data_len > 0. */
2843 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002844
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002845 if( auth_done == 1 )
2846 {
2847 correct *= ( rec->data_len >= padlen + 1 );
2848 padlen *= ( rec->data_len >= padlen + 1 );
2849 }
2850 else
Paul Bakker45829992013-01-03 14:52:21 +01002851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002852#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002853 if( rec->data_len < transform->maclen + padlen + 1 )
2854 {
2855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2856 rec->data_len,
2857 transform->maclen,
2858 padlen + 1 ) );
2859 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002860#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002861
2862 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2863 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002864 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002865
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002866 padlen++;
2867
2868 /* Regardless of the validity of the padding,
2869 * we have data_len >= padlen here. */
2870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002871#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002872 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002873 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002876#if defined(MBEDTLS_SSL_DEBUG_ALL)
2877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002878 "should be no more than %d",
2879 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002880#endif
Paul Bakker45829992013-01-03 14:52:21 +01002881 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002882 }
2883 }
2884 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002885#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2886#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2887 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002888 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002889 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 /* The padding check involves a series of up to 256
2891 * consecutive memory reads at the end of the record
2892 * plaintext buffer. In order to hide the length and
2893 * validity of the padding, always perform exactly
2894 * `min(256,plaintext_len)` reads (but take into account
2895 * only the last `padlen` bytes for the padding check). */
2896 size_t pad_count = 0;
2897 size_t real_count = 0;
2898 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002899
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002900 /* Index of first padding byte; it has been ensured above
2901 * that the subtraction is safe. */
2902 size_t const padding_idx = rec->data_len - padlen;
2903 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2904 size_t const start_idx = rec->data_len - num_checks;
2905 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002906
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002907 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002908 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 real_count |= ( idx >= padding_idx );
2910 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002911 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002912 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002914#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002915 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002917#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002918 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002919 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002920 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2922 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002923 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002924 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002926 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002927
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002928 /* If the padding was found to be invalid, padlen == 0
2929 * and the subtraction is safe. If the padding was found valid,
2930 * padlen hasn't been changed and the previous assertion
2931 * data_len >= padlen still holds. */
2932 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002933 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002934 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002936 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002937 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2939 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002940 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002941
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002942#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002943 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002944 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002945#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002946
2947 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002948 * Authenticate if not done yet.
2949 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002950 */
Hanno Becker52344c22018-01-03 15:24:20 +00002951#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002952 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002953 {
Hanno Becker992b6872017-11-09 18:57:39 +00002954 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002955
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002956 /* If the initial value of padlen was such that
2957 * data_len < maclen + padlen + 1, then padlen
2958 * got reset to 1, and the initial check
2959 * data_len >= minlen + maclen + 1
2960 * guarantees that at this point we still
2961 * have at least data_len >= maclen.
2962 *
2963 * If the initial value of padlen was such that
2964 * data_len >= maclen + padlen + 1, then we have
2965 * subtracted either padlen + 1 (if the padding was correct)
2966 * or 0 (if the padding was incorrect) since then,
2967 * hence data_len >= maclen in any case.
2968 */
2969 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Hanno Beckercab87e62019-04-29 13:52:53 +01002971 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002973#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002974 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002975 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002976 ssl_mac( &transform->md_ctx_dec,
2977 transform->mac_dec,
2978 data, rec->data_len,
2979 rec->ctr, rec->type,
2980 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002981 }
2982 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2984#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2985 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002986 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002987 {
2988 /*
2989 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002990 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002991 *
2992 * Known timing attacks:
2993 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2994 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002995 * To compensate for different timings for the MAC calculation
2996 * depending on how much padding was removed (which is determined
2997 * by padlen), process extra_run more blocks through the hash
2998 * function.
2999 *
3000 * The formula in the paper is
3001 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3002 * where L1 is the size of the header plus the decrypted message
3003 * plus CBC padding and L2 is the size of the header plus the
3004 * decrypted message. This is for an underlying hash function
3005 * with 64-byte blocks.
3006 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3007 * correctly. We round down instead of up, so -56 is the correct
3008 * value for our calculations instead of -55.
3009 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003010 * Repeat the formula rather than defining a block_size variable.
3011 * This avoids requiring division by a variable at runtime
3012 * (which would be marginally less efficient and would require
3013 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003014 */
3015 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003016 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003017
3018 /*
3019 * The next two sizes are the minimum and maximum values of
3020 * in_msglen over all padlen values.
3021 *
3022 * They're independent of padlen, since we previously did
3023 * in_msglen -= padlen.
3024 *
3025 * Note that max_len + maclen is never more than the buffer
3026 * length, as we previously did in_msglen -= maclen too.
3027 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003028 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003029 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3030
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003031 memset( tmp, 0, sizeof( tmp ) );
3032
3033 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003034 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003035#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3036 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 case MBEDTLS_MD_MD5:
3038 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003040 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003041 extra_run =
3042 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3043 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003044 break;
3045#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003046#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003047 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003048 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003049 extra_run =
3050 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3051 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003052 break;
3053#endif
3054 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003055 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003056 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3057 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003058
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003059 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003060
Hanno Beckercab87e62019-04-29 13:52:53 +01003061 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3062 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003063 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3064 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003065 /* Make sure we access everything even when padlen > 0. This
3066 * makes the synchronisation requirements for just-in-time
3067 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003068 ssl_read_memory( data + rec->data_len, padlen );
3069 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003070
3071 /* Call mbedtls_md_process at least once due to cache attacks
3072 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003073 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003074 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003075
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003077
3078 /* Make sure we access all the memory that could contain the MAC,
3079 * before we check it in the next code block. This makes the
3080 * synchronisation requirements for just-in-time Prime+Probe
3081 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003082 ssl_read_memory( data + min_len,
3083 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003084 }
3085 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3087 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003088 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3090 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003091 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003093#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003094 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3095 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003096#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003097
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003098 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3099 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003100 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003101#if defined(MBEDTLS_SSL_DEBUG_ALL)
3102 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003103#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003104 correct = 0;
3105 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003106 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003107 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003108
3109 /*
3110 * Finally check the correct flag
3111 */
3112 if( correct == 0 )
3113 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003114#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003115
3116 /* Make extra sure authentication was performed, exactly once */
3117 if( auth_done != 1 )
3118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003119 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3120 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003121 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003122
Hanno Beckera0e20d02019-05-15 14:03:01 +01003123#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003124 if( rec->cid_len != 0 )
3125 {
3126 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3127 &rec->type );
3128 if( ret != 0 )
3129 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3130 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003131#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003134
3135 return( 0 );
3136}
3137
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003138#undef MAC_NONE
3139#undef MAC_PLAINTEXT
3140#undef MAC_CIPHERTEXT
3141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003143/*
3144 * Compression/decompression functions
3145 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003147{
3148 int ret;
3149 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003150 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003151 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003152 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003156 if( len_pre == 0 )
3157 return( 0 );
3158
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159 memcpy( msg_pre, ssl->out_msg, len_pre );
3160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162 ssl->out_msglen ) );
3163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003164 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003165 ssl->out_msg, ssl->out_msglen );
3166
Paul Bakker48916f92012-09-16 19:57:18 +00003167 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3168 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3169 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003170 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003171
Paul Bakker48916f92012-09-16 19:57:18 +00003172 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173 if( ret != Z_OK )
3174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3176 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003177 }
3178
Angus Grattond8213d02016-05-25 20:56:48 +10003179 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003180 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183 ssl->out_msglen ) );
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186 ssl->out_msg, ssl->out_msglen );
3187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003189
3190 return( 0 );
3191}
3192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003193static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003194{
3195 int ret;
3196 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003197 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003198 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003199 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003203 if( len_pre == 0 )
3204 return( 0 );
3205
Paul Bakker2770fbd2012-07-03 13:30:23 +00003206 memcpy( msg_pre, ssl->in_msg, len_pre );
3207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003209 ssl->in_msglen ) );
3210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003211 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003212 ssl->in_msg, ssl->in_msglen );
3213
Paul Bakker48916f92012-09-16 19:57:18 +00003214 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3215 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3216 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003217 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003218 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003219
Paul Bakker48916f92012-09-16 19:57:18 +00003220 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221 if( ret != Z_OK )
3222 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3224 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003225 }
3226
Angus Grattond8213d02016-05-25 20:56:48 +10003227 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003228 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003231 ssl->in_msglen ) );
3232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003234 ssl->in_msg, ssl->in_msglen );
3235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003236 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003237
3238 return( 0 );
3239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003240#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3243static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003245#if defined(MBEDTLS_SSL_PROTO_DTLS)
3246static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003247{
3248 /* If renegotiation is not enforced, retransmit until we would reach max
3249 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003250 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003251 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003253 unsigned char doublings = 1;
3254
3255 while( ratio != 0 )
3256 {
3257 ++doublings;
3258 ratio >>= 1;
3259 }
3260
3261 if( ++ssl->renego_records_seen > doublings )
3262 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003263 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003264 return( 0 );
3265 }
3266 }
3267
3268 return( ssl_write_hello_request( ssl ) );
3269}
3270#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003271#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003272
Paul Bakker5121ce52009-01-03 21:22:43 +00003273/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003274 * Fill the input message buffer by appending data to it.
3275 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003276 *
3277 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3278 * available (from this read and/or a previous one). Otherwise, an error code
3279 * is returned (possibly EOF or WANT_READ).
3280 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003281 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3282 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3283 * since we always read a whole datagram at once.
3284 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003285 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003286 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003287 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003289{
Paul Bakker23986e52011-04-24 08:57:21 +00003290 int ret;
3291 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003295 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3296 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003297 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003298 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003300 }
3301
Angus Grattond8213d02016-05-25 20:56:48 +10003302 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003303 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003304 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3305 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003306 }
3307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003308#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003309 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003310 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003311 uint32_t timeout;
3312
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003313 /* Just to be sure */
3314 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3315 {
3316 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3317 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3318 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3319 }
3320
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003321 /*
3322 * The point is, we need to always read a full datagram at once, so we
3323 * sometimes read more then requested, and handle the additional data.
3324 * It could be the rest of the current record (while fetching the
3325 * header) and/or some other records in the same datagram.
3326 */
3327
3328 /*
3329 * Move to the next record in the already read datagram if applicable
3330 */
3331 if( ssl->next_record_offset != 0 )
3332 {
3333 if( ssl->in_left < ssl->next_record_offset )
3334 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003335 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3336 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003337 }
3338
3339 ssl->in_left -= ssl->next_record_offset;
3340
3341 if( ssl->in_left != 0 )
3342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003344 ssl->next_record_offset ) );
3345 memmove( ssl->in_hdr,
3346 ssl->in_hdr + ssl->next_record_offset,
3347 ssl->in_left );
3348 }
3349
3350 ssl->next_record_offset = 0;
3351 }
3352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003354 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003355
3356 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003357 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003358 */
3359 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003362 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003363 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003364
3365 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003366 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003367 * are not at the beginning of a new record, the caller did something
3368 * wrong.
3369 */
3370 if( ssl->in_left != 0 )
3371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3373 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003374 }
3375
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003376 /*
3377 * Don't even try to read if time's out already.
3378 * This avoids by-passing the timer when repeatedly receiving messages
3379 * that will end up being dropped.
3380 */
3381 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003382 {
3383 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003384 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003385 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003386 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003387 {
Angus Grattond8213d02016-05-25 20:56:48 +10003388 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003389
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003390 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391 timeout = ssl->handshake->retransmit_timeout;
3392 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003393 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003394
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003396
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003397 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3399 timeout );
3400 else
3401 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003404
3405 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003406 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407 }
3408
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003409 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003412 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003414 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003415 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003416 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3417 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003418 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003419 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003420 }
3421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003422 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003423 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003425 return( ret );
3426 }
3427
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003428 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003429 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003430#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003431 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003433 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003434 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003435 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003436 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003437 return( ret );
3438 }
3439
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003440 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003441 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003442#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003443 }
3444
Paul Bakker5121ce52009-01-03 21:22:43 +00003445 if( ret < 0 )
3446 return( ret );
3447
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003448 ssl->in_left = ret;
3449 }
3450 else
3451#endif
3452 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003454 ssl->in_left, nb_want ) );
3455
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003456 while( ssl->in_left < nb_want )
3457 {
3458 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003459
3460 if( ssl_check_timer( ssl ) != 0 )
3461 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3462 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003463 {
3464 if( ssl->f_recv_timeout != NULL )
3465 {
3466 ret = ssl->f_recv_timeout( ssl->p_bio,
3467 ssl->in_hdr + ssl->in_left, len,
3468 ssl->conf->read_timeout );
3469 }
3470 else
3471 {
3472 ret = ssl->f_recv( ssl->p_bio,
3473 ssl->in_hdr + ssl->in_left, len );
3474 }
3475 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003478 ssl->in_left, nb_want ) );
3479 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003480
3481 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003482 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003483
3484 if( ret < 0 )
3485 return( ret );
3486
mohammad160352aecb92018-03-28 23:41:40 -07003487 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003488 {
Darryl Green11999bb2018-03-13 15:22:58 +00003489 MBEDTLS_SSL_DEBUG_MSG( 1,
3490 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003491 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003492 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3493 }
3494
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003495 ssl->in_left += ret;
3496 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003497 }
3498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003499 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003500
3501 return( 0 );
3502}
3503
3504/*
3505 * Flush any data not yet written
3506 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003508{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003509 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003510 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003512 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003514 if( ssl->f_send == NULL )
3515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003516 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003517 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003519 }
3520
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003521 /* Avoid incrementing counter if data is flushed */
3522 if( ssl->out_left == 0 )
3523 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003525 return( 0 );
3526 }
3527
Paul Bakker5121ce52009-01-03 21:22:43 +00003528 while( ssl->out_left > 0 )
3529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003531 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003532
Hanno Becker2b1e3542018-08-06 11:19:13 +01003533 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003534 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003535
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003536 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003537
3538 if( ret <= 0 )
3539 return( ret );
3540
mohammad160352aecb92018-03-28 23:41:40 -07003541 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003542 {
Darryl Green11999bb2018-03-13 15:22:58 +00003543 MBEDTLS_SSL_DEBUG_MSG( 1,
3544 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003545 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003546 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3547 }
3548
Paul Bakker5121ce52009-01-03 21:22:43 +00003549 ssl->out_left -= ret;
3550 }
3551
Hanno Becker2b1e3542018-08-06 11:19:13 +01003552#if defined(MBEDTLS_SSL_PROTO_DTLS)
3553 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003554 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003555 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003556 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003557 else
3558#endif
3559 {
3560 ssl->out_hdr = ssl->out_buf + 8;
3561 }
3562 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003565
3566 return( 0 );
3567}
3568
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003569/*
3570 * Functions to handle the DTLS retransmission state machine
3571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003572#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003573/*
3574 * Append current handshake message to current outgoing flight
3575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003576static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003577{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3580 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3581 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582
3583 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003584 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003585 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003586 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003587 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003588 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589 }
3590
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003591 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003592 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003593 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003594 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003595 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003596 }
3597
3598 /* Copy current handshake message with headers */
3599 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3600 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003601 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003602 msg->next = NULL;
3603
3604 /* Append to the current flight */
3605 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003606 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 else
3608 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003609 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003610 while( cur->next != NULL )
3611 cur = cur->next;
3612 cur->next = msg;
3613 }
3614
Hanno Becker3b235902018-08-06 09:54:53 +01003615 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003616 return( 0 );
3617}
3618
3619/*
3620 * Free the current flight of handshake messages
3621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003622static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624 mbedtls_ssl_flight_item *cur = flight;
3625 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003626
3627 while( cur != NULL )
3628 {
3629 next = cur->next;
3630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003631 mbedtls_free( cur->p );
3632 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003633
3634 cur = next;
3635 }
3636}
3637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003638#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3639static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003640#endif
3641
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003642/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003643 * Swap transform_out and out_ctr with the alternative ones
3644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003645static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648 unsigned char tmp_out_ctr[8];
3649
3650 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3651 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003652 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003653 return;
3654 }
3655
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003656 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003657
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003658 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003659 tmp_transform = ssl->transform_out;
3660 ssl->transform_out = ssl->handshake->alt_transform_out;
3661 ssl->handshake->alt_transform_out = tmp_transform;
3662
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003663 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003664 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3665 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003666 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003667
3668 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003669 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003670
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3672 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003674 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3677 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003678 }
3679 }
3680#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003681}
3682
3683/*
3684 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003685 */
3686int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3687{
3688 int ret = 0;
3689
3690 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3691
3692 ret = mbedtls_ssl_flight_transmit( ssl );
3693
3694 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3695
3696 return( ret );
3697}
3698
3699/*
3700 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003701 *
3702 * Need to remember the current message in case flush_output returns
3703 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003704 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003705 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003706int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003707{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003708 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003709 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003710
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003711 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003712 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003713 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003714
3715 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003716 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003717 ssl_swap_epochs( ssl );
3718
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003719 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003720 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003721
3722 while( ssl->handshake->cur_msg != NULL )
3723 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003724 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003725 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003726
Hanno Beckere1dcb032018-08-17 16:47:58 +01003727 int const is_finished =
3728 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3729 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3730
Hanno Becker04da1892018-08-14 13:22:10 +01003731 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3732 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3733
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003734 /* Swap epochs before sending Finished: we can't do it after
3735 * sending ChangeCipherSpec, in case write returns WANT_READ.
3736 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003737 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003738 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003739 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003740 ssl_swap_epochs( ssl );
3741 }
3742
Hanno Becker67bc7c32018-08-06 11:33:50 +01003743 ret = ssl_get_remaining_payload_in_datagram( ssl );
3744 if( ret < 0 )
3745 return( ret );
3746 max_frag_len = (size_t) ret;
3747
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003748 /* CCS is copied as is, while HS messages may need fragmentation */
3749 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3750 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003751 if( max_frag_len == 0 )
3752 {
3753 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3754 return( ret );
3755
3756 continue;
3757 }
3758
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003759 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003760 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003761 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003762
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003763 /* Update position inside current message */
3764 ssl->handshake->cur_msg_p += cur->len;
3765 }
3766 else
3767 {
3768 const unsigned char * const p = ssl->handshake->cur_msg_p;
3769 const size_t hs_len = cur->len - 12;
3770 const size_t frag_off = p - ( cur->p + 12 );
3771 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003772 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003773
Hanno Beckere1dcb032018-08-17 16:47:58 +01003774 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003775 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003776 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003777 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003778
Hanno Becker67bc7c32018-08-06 11:33:50 +01003779 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3780 return( ret );
3781
3782 continue;
3783 }
3784 max_hs_frag_len = max_frag_len - 12;
3785
3786 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3787 max_hs_frag_len : rem_len;
3788
3789 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003790 {
3791 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003792 (unsigned) cur_hs_frag_len,
3793 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003794 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003795
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003796 /* Messages are stored with handshake headers as if not fragmented,
3797 * copy beginning of headers then fill fragmentation fields.
3798 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3799 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003800
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003801 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3802 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3803 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3804
Hanno Becker67bc7c32018-08-06 11:33:50 +01003805 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3806 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3807 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003808
3809 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3810
Hanno Becker3f7b9732018-08-28 09:53:25 +01003811 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003812 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3813 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003814 ssl->out_msgtype = cur->type;
3815
3816 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003817 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003818 }
3819
3820 /* If done with the current message move to the next one if any */
3821 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3822 {
3823 if( cur->next != NULL )
3824 {
3825 ssl->handshake->cur_msg = cur->next;
3826 ssl->handshake->cur_msg_p = cur->next->p + 12;
3827 }
3828 else
3829 {
3830 ssl->handshake->cur_msg = NULL;
3831 ssl->handshake->cur_msg_p = NULL;
3832 }
3833 }
3834
3835 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003836 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003838 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003839 return( ret );
3840 }
3841 }
3842
Hanno Becker67bc7c32018-08-06 11:33:50 +01003843 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3844 return( ret );
3845
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003846 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003847 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3848 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003849 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003852 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3853 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003854
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003855 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003856
3857 return( 0 );
3858}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003859
3860/*
3861 * To be called when the last message of an incoming flight is received.
3862 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003863void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003864{
3865 /* We won't need to resend that one any more */
3866 ssl_flight_free( ssl->handshake->flight );
3867 ssl->handshake->flight = NULL;
3868 ssl->handshake->cur_msg = NULL;
3869
3870 /* The next incoming flight will start with this msg_seq */
3871 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3872
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003873 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003874 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003875
Hanno Becker0271f962018-08-16 13:23:47 +01003876 /* Clear future message buffering structure. */
3877 ssl_buffering_free( ssl );
3878
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003879 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003880 ssl_set_timer( ssl, 0 );
3881
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3883 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003886 }
3887 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003889}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003890
3891/*
3892 * To be called when the last message of an outgoing flight is send.
3893 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003894void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003895{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003896 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003897 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3900 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003903 }
3904 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003905 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003906}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003908
Paul Bakker5121ce52009-01-03 21:22:43 +00003909/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003910 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003911 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003912
3913/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003914 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003915 *
3916 * - fill in handshake headers
3917 * - update handshake checksum
3918 * - DTLS: save message for resending
3919 * - then pass to the record layer
3920 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003921 * DTLS: except for HelloRequest, messages are only queued, and will only be
3922 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003923 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003924 * Inputs:
3925 * - ssl->out_msglen: 4 + actual handshake message len
3926 * (4 is the size of handshake headers for TLS)
3927 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3928 * - ssl->out_msg + 4: the handshake message body
3929 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003930 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003931 * - ssl->out_msglen: the length of the record contents
3932 * (including handshake headers but excluding record headers)
3933 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003934 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003935int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003936{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003937 int ret;
3938 const size_t hs_len = ssl->out_msglen - 4;
3939 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003940
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003941 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3942
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003943 /*
3944 * Sanity checks
3945 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003946 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003947 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3948 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003949 /* In SSLv3, the client might send a NoCertificate alert. */
3950#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3951 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3952 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3953 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3954#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3955 {
3956 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3957 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3958 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003959 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003960
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003961 /* Whenever we send anything different from a
3962 * HelloRequest we should be in a handshake - double check. */
3963 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3964 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003965 ssl->handshake == NULL )
3966 {
3967 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3968 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3969 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003972 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003973 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003974 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3977 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003978 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003979#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003980
Hanno Beckerb50a2532018-08-06 11:52:54 +01003981 /* Double-check that we did not exceed the bounds
3982 * of the outgoing record buffer.
3983 * This should never fail as the various message
3984 * writing functions must obey the bounds of the
3985 * outgoing record buffer, but better be safe.
3986 *
3987 * Note: We deliberately do not check for the MTU or MFL here.
3988 */
3989 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3990 {
3991 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3992 "size %u, maximum %u",
3993 (unsigned) ssl->out_msglen,
3994 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3995 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3996 }
3997
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003998 /*
3999 * Fill handshake headers
4000 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004002 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004003 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4004 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4005 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004006
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004007 /*
4008 * DTLS has additional fields in the Handshake layer,
4009 * between the length field and the actual payload:
4010 * uint16 message_seq;
4011 * uint24 fragment_offset;
4012 * uint24 fragment_length;
4013 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004015 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004016 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004017 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004018 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004019 {
4020 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4021 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004022 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004023 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4025 }
4026
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004027 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004028 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004029
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004030 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004031 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004032 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004033 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4034 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4035 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004036 }
4037 else
4038 {
4039 ssl->out_msg[4] = 0;
4040 ssl->out_msg[5] = 0;
4041 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004042
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004043 /* Handshake hashes are computed without fragmentation,
4044 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004045 memset( ssl->out_msg + 6, 0x00, 3 );
4046 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004047 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004048#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004049
Hanno Becker0207e532018-08-28 10:28:28 +01004050 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004051 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4052 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004053 }
4054
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004055 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004057 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004058 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4059 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004060 {
4061 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4062 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004063 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004064 return( ret );
4065 }
4066 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004067 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004068#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004069 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004070 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 {
4072 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4073 return( ret );
4074 }
4075 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004076
4077 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4078
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004079 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004080}
4081
4082/*
4083 * Record layer functions
4084 */
4085
4086/*
4087 * Write current record.
4088 *
4089 * Uses:
4090 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4091 * - ssl->out_msglen: length of the record content (excl headers)
4092 * - ssl->out_msg: record content
4093 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004094int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004095{
4096 int ret, done = 0;
4097 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004098 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004099
4100 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004102#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004103 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004105 {
4106 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4107 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004108 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004109 return( ret );
4110 }
4111
4112 len = ssl->out_msglen;
4113 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004114#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4117 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004118 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 ret = mbedtls_ssl_hw_record_write( ssl );
4122 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004124 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4125 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004126 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004127
4128 if( ret == 0 )
4129 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004132 if( !done )
4133 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004134 unsigned i;
4135 size_t protected_record_size;
4136
Hanno Becker6430faf2019-05-08 11:57:13 +01004137 /* Skip writing the record content type to after the encryption,
4138 * as it may change when using the CID extension. */
4139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004140 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004141 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004142
Hanno Becker19859472018-08-06 09:40:20 +01004143 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004144 ssl->out_len[0] = (unsigned char)( len >> 8 );
4145 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004146
Paul Bakker48916f92012-09-16 19:57:18 +00004147 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004148 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004149 mbedtls_record rec;
4150
4151 rec.buf = ssl->out_iv;
4152 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4153 ( ssl->out_iv - ssl->out_buf );
4154 rec.data_len = ssl->out_msglen;
4155 rec.data_offset = ssl->out_msg - rec.buf;
4156
4157 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4158 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4159 ssl->conf->transport, rec.ver );
4160 rec.type = ssl->out_msgtype;
4161
Hanno Beckera0e20d02019-05-15 14:03:01 +01004162#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004163 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004164 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004165#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004166
Hanno Beckera18d1322018-01-03 14:27:32 +00004167 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004168 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004170 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004171 return( ret );
4172 }
4173
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004174 if( rec.data_offset != 0 )
4175 {
4176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4177 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4178 }
4179
Hanno Becker6430faf2019-05-08 11:57:13 +01004180 /* Update the record content type and CID. */
4181 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004182#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004183 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004184#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004185 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004186 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4187 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004188 }
4189
Hanno Becker5903de42019-05-03 14:46:38 +01004190 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004191
4192#if defined(MBEDTLS_SSL_PROTO_DTLS)
4193 /* In case of DTLS, double-check that we don't exceed
4194 * the remaining space in the datagram. */
4195 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4196 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004197 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004198 if( ret < 0 )
4199 return( ret );
4200
4201 if( protected_record_size > (size_t) ret )
4202 {
4203 /* Should never happen */
4204 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4205 }
4206 }
4207#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004208
Hanno Becker6430faf2019-05-08 11:57:13 +01004209 /* Now write the potentially updated record content type. */
4210 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004212 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004213 "version = [%d:%d], msglen = %d",
4214 ssl->out_hdr[0], ssl->out_hdr[1],
4215 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004217 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004218 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004219
4220 ssl->out_left += protected_record_size;
4221 ssl->out_hdr += protected_record_size;
4222 ssl_update_out_pointers( ssl, ssl->transform_out );
4223
Hanno Becker04484622018-08-06 09:49:38 +01004224 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4225 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4226 break;
4227
4228 /* The loop goes to its end iff the counter is wrapping */
4229 if( i == ssl_ep_len( ssl ) )
4230 {
4231 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4232 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4233 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004234 }
4235
Hanno Becker67bc7c32018-08-06 11:33:50 +01004236#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004237 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4238 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004239 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004240 size_t remaining;
4241 ret = ssl_get_remaining_payload_in_datagram( ssl );
4242 if( ret < 0 )
4243 {
4244 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4245 ret );
4246 return( ret );
4247 }
4248
4249 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004250 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004251 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004253 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004254 else
4255 {
Hanno Becker513815a2018-08-20 11:56:09 +01004256 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004257 }
4258 }
4259#endif /* MBEDTLS_SSL_PROTO_DTLS */
4260
4261 if( ( flush == SSL_FORCE_FLUSH ) &&
4262 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004263 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004264 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004265 return( ret );
4266 }
4267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004268 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004269
4270 return( 0 );
4271}
4272
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004274
4275static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4276{
4277 if( ssl->in_msglen < ssl->in_hslen ||
4278 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4279 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4280 {
4281 return( 1 );
4282 }
4283 return( 0 );
4284}
Hanno Becker44650b72018-08-16 12:51:11 +01004285
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004286static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004287{
4288 return( ( ssl->in_msg[9] << 16 ) |
4289 ( ssl->in_msg[10] << 8 ) |
4290 ssl->in_msg[11] );
4291}
4292
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004293static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004294{
4295 return( ( ssl->in_msg[6] << 16 ) |
4296 ( ssl->in_msg[7] << 8 ) |
4297 ssl->in_msg[8] );
4298}
4299
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004300static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004301{
4302 uint32_t msg_len, frag_off, frag_len;
4303
4304 msg_len = ssl_get_hs_total_len( ssl );
4305 frag_off = ssl_get_hs_frag_off( ssl );
4306 frag_len = ssl_get_hs_frag_len( ssl );
4307
4308 if( frag_off > msg_len )
4309 return( -1 );
4310
4311 if( frag_len > msg_len - frag_off )
4312 return( -1 );
4313
4314 if( frag_len + 12 > ssl->in_msglen )
4315 return( -1 );
4316
4317 return( 0 );
4318}
4319
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004320/*
4321 * Mark bits in bitmask (used for DTLS HS reassembly)
4322 */
4323static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4324{
4325 unsigned int start_bits, end_bits;
4326
4327 start_bits = 8 - ( offset % 8 );
4328 if( start_bits != 8 )
4329 {
4330 size_t first_byte_idx = offset / 8;
4331
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004332 /* Special case */
4333 if( len <= start_bits )
4334 {
4335 for( ; len != 0; len-- )
4336 mask[first_byte_idx] |= 1 << ( start_bits - len );
4337
4338 /* Avoid potential issues with offset or len becoming invalid */
4339 return;
4340 }
4341
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004342 offset += start_bits; /* Now offset % 8 == 0 */
4343 len -= start_bits;
4344
4345 for( ; start_bits != 0; start_bits-- )
4346 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4347 }
4348
4349 end_bits = len % 8;
4350 if( end_bits != 0 )
4351 {
4352 size_t last_byte_idx = ( offset + len ) / 8;
4353
4354 len -= end_bits; /* Now len % 8 == 0 */
4355
4356 for( ; end_bits != 0; end_bits-- )
4357 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4358 }
4359
4360 memset( mask + offset / 8, 0xFF, len / 8 );
4361}
4362
4363/*
4364 * Check that bitmask is full
4365 */
4366static int ssl_bitmask_check( unsigned char *mask, size_t len )
4367{
4368 size_t i;
4369
4370 for( i = 0; i < len / 8; i++ )
4371 if( mask[i] != 0xFF )
4372 return( -1 );
4373
4374 for( i = 0; i < len % 8; i++ )
4375 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4376 return( -1 );
4377
4378 return( 0 );
4379}
4380
Hanno Becker56e205e2018-08-16 09:06:12 +01004381/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004382static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004383 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004384{
Hanno Becker56e205e2018-08-16 09:06:12 +01004385 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004386
Hanno Becker56e205e2018-08-16 09:06:12 +01004387 alloc_len = 12; /* Handshake header */
4388 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004389
Hanno Beckerd07df862018-08-16 09:14:58 +01004390 if( add_bitmap )
4391 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004392
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004393 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004394}
Hanno Becker56e205e2018-08-16 09:06:12 +01004395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004397
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004398static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004399{
4400 return( ( ssl->in_msg[1] << 16 ) |
4401 ( ssl->in_msg[2] << 8 ) |
4402 ssl->in_msg[3] );
4403}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004404
Simon Butcher99000142016-10-13 17:21:01 +01004405int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004406{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004408 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004410 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004412 }
4413
Hanno Becker12555c62018-08-16 12:47:53 +01004414 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004416 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004417 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004418 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004421 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004422 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004423 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004424 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004425
Hanno Becker44650b72018-08-16 12:51:11 +01004426 if( ssl_check_hs_header( ssl ) != 0 )
4427 {
4428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4429 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4430 }
4431
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004432 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004433 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4434 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4435 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4436 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004437 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004438 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4439 {
4440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4441 recv_msg_seq,
4442 ssl->handshake->in_msg_seq ) );
4443 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4444 }
4445
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004446 /* Retransmit only on last message from previous flight, to avoid
4447 * too many retransmissions.
4448 * Besides, No sane server ever retransmits HelloVerifyRequest */
4449 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004450 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 "message_seq = %d, start_of_flight = %d",
4454 recv_msg_seq,
4455 ssl->handshake->in_flight_start_seq ) );
4456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004457 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004458 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004460 return( ret );
4461 }
4462 }
4463 else
4464 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004466 "message_seq = %d, expected = %d",
4467 recv_msg_seq,
4468 ssl->handshake->in_msg_seq ) );
4469 }
4470
Hanno Becker90333da2017-10-10 11:27:13 +01004471 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004472 }
4473 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004474
Hanno Becker6d97ef52018-08-16 13:09:04 +01004475 /* Message reassembly is handled alongside buffering of future
4476 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004477 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004478 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004479 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004482 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004483 }
4484 }
4485 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004486#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004487 /* With TLS we don't handle fragmentation (for now) */
4488 if( ssl->in_msglen < ssl->in_hslen )
4489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4491 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004492 }
4493
Simon Butcher99000142016-10-13 17:21:01 +01004494 return( 0 );
4495}
4496
4497void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4498{
Hanno Becker0271f962018-08-16 13:23:47 +01004499 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004500
Hanno Becker0271f962018-08-16 13:23:47 +01004501 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004502 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004503 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004504 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004505
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004506 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004507#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004508 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004509 ssl->handshake != NULL )
4510 {
Hanno Becker0271f962018-08-16 13:23:47 +01004511 unsigned offset;
4512 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004513
Hanno Becker0271f962018-08-16 13:23:47 +01004514 /* Increment handshake sequence number */
4515 hs->in_msg_seq++;
4516
4517 /*
4518 * Clear up handshake buffering and reassembly structure.
4519 */
4520
4521 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004522 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004523
4524 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004525 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4526 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004527 offset++, hs_buf++ )
4528 {
4529 *hs_buf = *(hs_buf + 1);
4530 }
4531
4532 /* Create a fresh last entry */
4533 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004534 }
4535#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004536}
4537
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004538/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004539 * DTLS anti-replay: RFC 6347 4.1.2.6
4540 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004541 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4542 * Bit n is set iff record number in_window_top - n has been seen.
4543 *
4544 * Usually, in_window_top is the last record number seen and the lsb of
4545 * in_window is set. The only exception is the initial state (record number 0
4546 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004547 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004548#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4549static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004550{
4551 ssl->in_window_top = 0;
4552 ssl->in_window = 0;
4553}
4554
4555static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4556{
4557 return( ( (uint64_t) buf[0] << 40 ) |
4558 ( (uint64_t) buf[1] << 32 ) |
4559 ( (uint64_t) buf[2] << 24 ) |
4560 ( (uint64_t) buf[3] << 16 ) |
4561 ( (uint64_t) buf[4] << 8 ) |
4562 ( (uint64_t) buf[5] ) );
4563}
4564
4565/*
4566 * Return 0 if sequence number is acceptable, -1 otherwise
4567 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004568int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004569{
4570 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4571 uint64_t bit;
4572
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004573 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004574 return( 0 );
4575
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004576 if( rec_seqnum > ssl->in_window_top )
4577 return( 0 );
4578
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004579 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004580
4581 if( bit >= 64 )
4582 return( -1 );
4583
4584 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4585 return( -1 );
4586
4587 return( 0 );
4588}
4589
4590/*
4591 * Update replay window on new validated record
4592 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004593void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004594{
4595 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4596
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004597 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004598 return;
4599
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004600 if( rec_seqnum > ssl->in_window_top )
4601 {
4602 /* Update window_top and the contents of the window */
4603 uint64_t shift = rec_seqnum - ssl->in_window_top;
4604
4605 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004606 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004607 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004608 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004609 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004610 ssl->in_window |= 1;
4611 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612
4613 ssl->in_window_top = rec_seqnum;
4614 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004615 else
4616 {
4617 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004618 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004619
4620 if( bit < 64 ) /* Always true, but be extra sure */
4621 ssl->in_window |= (uint64_t) 1 << bit;
4622 }
4623}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004624#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004625
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004626#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004627/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004628static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4629
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004630/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004631 * Without any SSL context, check if a datagram looks like a ClientHello with
4632 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004633 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004634 *
4635 * - if cookie is valid, return 0
4636 * - if ClientHello looks superficially valid but cookie is not,
4637 * fill obuf and set olen, then
4638 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4639 * - otherwise return a specific error code
4640 */
4641static int ssl_check_dtls_clihlo_cookie(
4642 mbedtls_ssl_cookie_write_t *f_cookie_write,
4643 mbedtls_ssl_cookie_check_t *f_cookie_check,
4644 void *p_cookie,
4645 const unsigned char *cli_id, size_t cli_id_len,
4646 const unsigned char *in, size_t in_len,
4647 unsigned char *obuf, size_t buf_len, size_t *olen )
4648{
4649 size_t sid_len, cookie_len;
4650 unsigned char *p;
4651
4652 if( f_cookie_write == NULL || f_cookie_check == NULL )
4653 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4654
4655 /*
4656 * Structure of ClientHello with record and handshake headers,
4657 * and expected values. We don't need to check a lot, more checks will be
4658 * done when actually parsing the ClientHello - skipping those checks
4659 * avoids code duplication and does not make cookie forging any easier.
4660 *
4661 * 0-0 ContentType type; copied, must be handshake
4662 * 1-2 ProtocolVersion version; copied
4663 * 3-4 uint16 epoch; copied, must be 0
4664 * 5-10 uint48 sequence_number; copied
4665 * 11-12 uint16 length; (ignored)
4666 *
4667 * 13-13 HandshakeType msg_type; (ignored)
4668 * 14-16 uint24 length; (ignored)
4669 * 17-18 uint16 message_seq; copied
4670 * 19-21 uint24 fragment_offset; copied, must be 0
4671 * 22-24 uint24 fragment_length; (ignored)
4672 *
4673 * 25-26 ProtocolVersion client_version; (ignored)
4674 * 27-58 Random random; (ignored)
4675 * 59-xx SessionID session_id; 1 byte len + sid_len content
4676 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4677 * ...
4678 *
4679 * Minimum length is 61 bytes.
4680 */
4681 if( in_len < 61 ||
4682 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4683 in[3] != 0 || in[4] != 0 ||
4684 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4685 {
4686 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4687 }
4688
4689 sid_len = in[59];
4690 if( sid_len > in_len - 61 )
4691 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4692
4693 cookie_len = in[60 + sid_len];
4694 if( cookie_len > in_len - 60 )
4695 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4696
4697 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4698 cli_id, cli_id_len ) == 0 )
4699 {
4700 /* Valid cookie */
4701 return( 0 );
4702 }
4703
4704 /*
4705 * If we get here, we've got an invalid cookie, let's prepare HVR.
4706 *
4707 * 0-0 ContentType type; copied
4708 * 1-2 ProtocolVersion version; copied
4709 * 3-4 uint16 epoch; copied
4710 * 5-10 uint48 sequence_number; copied
4711 * 11-12 uint16 length; olen - 13
4712 *
4713 * 13-13 HandshakeType msg_type; hello_verify_request
4714 * 14-16 uint24 length; olen - 25
4715 * 17-18 uint16 message_seq; copied
4716 * 19-21 uint24 fragment_offset; copied
4717 * 22-24 uint24 fragment_length; olen - 25
4718 *
4719 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4720 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4721 *
4722 * Minimum length is 28.
4723 */
4724 if( buf_len < 28 )
4725 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4726
4727 /* Copy most fields and adapt others */
4728 memcpy( obuf, in, 25 );
4729 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4730 obuf[25] = 0xfe;
4731 obuf[26] = 0xff;
4732
4733 /* Generate and write actual cookie */
4734 p = obuf + 28;
4735 if( f_cookie_write( p_cookie,
4736 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4737 {
4738 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4739 }
4740
4741 *olen = p - obuf;
4742
4743 /* Go back and fill length fields */
4744 obuf[27] = (unsigned char)( *olen - 28 );
4745
4746 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4747 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4748 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4749
4750 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4751 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4752
4753 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4754}
4755
4756/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004757 * Handle possible client reconnect with the same UDP quadruplet
4758 * (RFC 6347 Section 4.2.8).
4759 *
4760 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4761 * that looks like a ClientHello.
4762 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004763 * - if the input looks like a ClientHello without cookies,
4764 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004765 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766 * - if the input looks like a ClientHello with a valid cookie,
4767 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004768 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004769 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004770 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004771 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004772 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4773 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004774 */
4775static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4776{
4777 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004778 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004779
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 ret = ssl_check_dtls_clihlo_cookie(
4781 ssl->conf->f_cookie_write,
4782 ssl->conf->f_cookie_check,
4783 ssl->conf->p_cookie,
4784 ssl->cli_id, ssl->cli_id_len,
4785 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004786 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004787
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004788 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4789
4790 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004791 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004792 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004793 * If the error is permanent we'll catch it later,
4794 * if it's not, then hopefully it'll work next time. */
4795 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4796
4797 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004798 }
4799
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004800 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004801 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004802 /* Got a valid cookie, partially reset context */
4803 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4804 {
4805 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4806 return( ret );
4807 }
4808
4809 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004810 }
4811
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004812 return( ret );
4813}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004814#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004815
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004816static int ssl_check_record_type( uint8_t record_type )
4817{
4818 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4819 record_type != MBEDTLS_SSL_MSG_ALERT &&
4820 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4821 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4822 {
4823 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4824 }
4825
4826 return( 0 );
4827}
4828
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004829/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004830 * ContentType type;
4831 * ProtocolVersion version;
4832 * uint16 epoch; // DTLS only
4833 * uint48 sequence_number; // DTLS only
4834 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004835 *
4836 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004837 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004838 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4839 *
4840 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004841 * 1. proceed with the record if this function returns 0
4842 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4843 * 3. return CLIENT_RECONNECT if this function return that value
4844 * 4. drop the whole datagram if this function returns anything else.
4845 * Point 2 is needed when the peer is resending, and we have already received
4846 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004847 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004848static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004849{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004850 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004851 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004852
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004853 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004854
Paul Bakker5121ce52009-01-03 21:22:43 +00004855 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004856 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004857
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004858 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004859#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004860 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4861 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4862 ssl->conf->cid_len != 0 )
4863 {
4864 /* Shift pointers to account for record header including CID
4865 * struct {
4866 * ContentType special_type = tls12_cid;
4867 * ProtocolVersion version;
4868 * uint16 epoch;
4869 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004870 * opaque cid[cid_length]; // Additional field compared to
4871 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004872 * uint16 length;
4873 * opaque enc_content[DTLSCiphertext.length];
4874 * } DTLSCiphertext;
4875 */
4876
4877 /* So far, we only support static CID lengths
4878 * fixed in the configuration. */
4879 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4880 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4881 }
4882 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004883#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004884 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004885 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004886 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004887
4888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004889 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4890 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004891 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4892#endif /* MBEDTLS_SSL_PROTO_DTLS */
4893 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4894 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004896 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004897 }
4898
4899 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004900 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004902 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4903 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 }
4905
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004906 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004907 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004908 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4909 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004910 }
4911
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004912 /* Now that the total length of the record header is known, ensure
4913 * that the current datagram is large enough to hold it.
4914 * This would fail, for example, if we received a datagram of
4915 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4916 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4917 if( ret != 0 )
4918 {
4919 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4920 return( ret );
4921 }
4922 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4923
4924 /* Parse and validate record length
4925 * This must happen after the CID parsing because
4926 * its position in the record header depends on
4927 * the presence of a CID. */
4928
4929 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004930 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004931 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004932 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004933 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4934 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004935 }
4936
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004937 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004938 "version = [%d:%d], msglen = %d",
4939 ssl->in_msgtype,
4940 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004941
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004942 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004943 * DTLS-related tests.
4944 * Check epoch before checking length constraint because
4945 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4946 * message gets duplicated before the corresponding Finished message,
4947 * the second ChangeCipherSpec should be discarded because it belongs
4948 * to an old epoch, but not because its length is shorter than
4949 * the minimum record length for packets using the new record transform.
4950 * Note that these two kinds of failures are handled differently,
4951 * as an unexpected record is silently skipped but an invalid
4952 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004953 */
4954#if defined(MBEDTLS_SSL_PROTO_DTLS)
4955 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4956 {
4957 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4958
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004959 /* Check epoch (and sequence number) with DTLS */
4960 if( rec_epoch != ssl->in_epoch )
4961 {
4962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4963 "expected %d, received %d",
4964 ssl->in_epoch, rec_epoch ) );
4965
4966#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4967 /*
4968 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4969 * access the first byte of record content (handshake type), as we
4970 * have an active transform (possibly iv_len != 0), so use the
4971 * fact that the record header len is 13 instead.
4972 */
4973 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4974 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4975 rec_epoch == 0 &&
4976 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4977 ssl->in_left > 13 &&
4978 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4979 {
4980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4981 "from the same port" ) );
4982 return( ssl_handle_possible_reconnect( ssl ) );
4983 }
4984 else
4985#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004986 {
4987 /* Consider buffering the record. */
4988 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4989 {
4990 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4991 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4992 }
4993
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004994 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004995 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004996 }
4997
4998#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4999 /* Replay detection only works for the current epoch */
5000 if( rec_epoch == ssl->in_epoch &&
5001 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5002 {
5003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5004 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5005 }
5006#endif
5007 }
5008#endif /* MBEDTLS_SSL_PROTO_DTLS */
5009
Hanno Becker52c6dc62017-05-26 16:07:36 +01005010
5011 /* Check length against bounds of the current transform and version */
5012 if( ssl->transform_in == NULL )
5013 {
5014 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005015 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005016 {
5017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5019 }
5020 }
5021 else
5022 {
5023 if( ssl->in_msglen < ssl->transform_in->minlen )
5024 {
5025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5026 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5027 }
5028
5029#if defined(MBEDTLS_SSL_PROTO_SSL3)
5030 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005031 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005032 {
5033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5034 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5035 }
5036#endif
5037#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5038 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5039 /*
5040 * TLS encrypted messages can have up to 256 bytes of padding
5041 */
5042 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5043 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005044 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005045 {
5046 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5047 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5048 }
5049#endif
5050 }
5051
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005052 return( 0 );
5053}
Paul Bakker5121ce52009-01-03 21:22:43 +00005054
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005055/*
5056 * If applicable, decrypt (and decompress) record content
5057 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005058static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005059{
5060 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005063 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5066 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005067 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005068 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 ret = mbedtls_ssl_hw_record_read( ssl );
5071 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005072 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005073 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5074 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005075 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005076
5077 if( ret == 0 )
5078 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005079 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005080#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005081 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005082 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005083 mbedtls_record rec;
5084
5085 rec.buf = ssl->in_iv;
5086 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5087 - ( ssl->in_iv - ssl->in_buf );
5088 rec.data_len = ssl->in_msglen;
5089 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005090#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005091 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005092 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005093#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005094
5095 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5096 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5097 ssl->conf->transport, rec.ver );
5098 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005099 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5100 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005101 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005102 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005103
Hanno Beckera0e20d02019-05-15 14:03:01 +01005104#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005105 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5106 ssl->conf->ignore_unexpected_cid
5107 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5108 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005109 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005110 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005111 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005112#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005113
Paul Bakker5121ce52009-01-03 21:22:43 +00005114 return( ret );
5115 }
5116
Hanno Becker6430faf2019-05-08 11:57:13 +01005117 if( ssl->in_msgtype != rec.type )
5118 {
5119 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5120 ssl->in_msgtype, rec.type ) );
5121 }
5122
5123 /* The record content type may change during decryption,
5124 * so re-read it. */
5125 ssl->in_msgtype = rec.type;
5126 /* Also update the input buffer, because unfortunately
5127 * the server-side ssl_parse_client_hello() reparses the
5128 * record header when receiving a ClientHello initiating
5129 * a renegotiation. */
5130 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005131 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005132 ssl->in_msglen = rec.data_len;
5133 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5134 ssl->in_len[1] = (unsigned char)( rec.data_len );
5135
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005136 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5137 ssl->in_msg, ssl->in_msglen );
5138
Hanno Beckera0e20d02019-05-15 14:03:01 +01005139#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005140 /* We have already checked the record content type
5141 * in ssl_parse_record_header(), failing or silently
5142 * dropping the record in the case of an unknown type.
5143 *
5144 * Since with the use of CIDs, the record content type
5145 * might change during decryption, re-check the record
5146 * content type, but treat a failure as fatal this time. */
5147 if( ssl_check_record_type( ssl->in_msgtype ) )
5148 {
5149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5150 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5151 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005152#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005153
Angus Grattond8213d02016-05-25 20:56:48 +10005154 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005156 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5157 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005158 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005159 else if( ssl->in_msglen == 0 )
5160 {
5161#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5162 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5163 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5164 {
5165 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5167 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5168 }
5169#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5170
5171 ssl->nb_zero++;
5172
5173 /*
5174 * Three or more empty messages may be a DoS attack
5175 * (excessive CPU consumption).
5176 */
5177 if( ssl->nb_zero > 3 )
5178 {
5179 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005180 "messages, possible DoS attack" ) );
5181 /* Treat the records as if they were not properly authenticated,
5182 * thereby failing the connection if we see more than allowed
5183 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005184 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5185 }
5186 }
5187 else
5188 ssl->nb_zero = 0;
5189
5190#if defined(MBEDTLS_SSL_PROTO_DTLS)
5191 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5192 {
5193 ; /* in_ctr read from peer, not maintained internally */
5194 }
5195 else
5196#endif
5197 {
5198 unsigned i;
5199 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5200 if( ++ssl->in_ctr[i - 1] != 0 )
5201 break;
5202
5203 /* The loop goes to its end iff the counter is wrapping */
5204 if( i == ssl_ep_len( ssl ) )
5205 {
5206 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5207 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5208 }
5209 }
5210
Paul Bakker5121ce52009-01-03 21:22:43 +00005211 }
5212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005213#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005214 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005216 {
5217 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5218 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005219 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005220 return( ret );
5221 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005222 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005223#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005226 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005228 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005229 }
5230#endif
5231
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005232 return( 0 );
5233}
5234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005235static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005236
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005237/*
5238 * Read a record.
5239 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005240 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5241 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5242 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005243 */
Hanno Becker1097b342018-08-15 14:09:41 +01005244
5245/* Helper functions for mbedtls_ssl_read_record(). */
5246static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005247static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5248static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005249
Hanno Becker327c93b2018-08-15 13:56:18 +01005250int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005251 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005252{
5253 int ret;
5254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005256
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005257 if( ssl->keep_current_message == 0 )
5258 {
5259 do {
Simon Butcher99000142016-10-13 17:21:01 +01005260
Hanno Becker26994592018-08-15 14:14:59 +01005261 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005262 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005263 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005264
Hanno Beckere74d5562018-08-15 14:26:08 +01005265 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005266 {
Hanno Becker40f50842018-08-15 14:48:01 +01005267#if defined(MBEDTLS_SSL_PROTO_DTLS)
5268 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005269
Hanno Becker40f50842018-08-15 14:48:01 +01005270 /* We only check for buffered messages if the
5271 * current datagram is fully consumed. */
5272 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005273 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005274 {
Hanno Becker40f50842018-08-15 14:48:01 +01005275 if( ssl_load_buffered_message( ssl ) == 0 )
5276 have_buffered = 1;
5277 }
5278
5279 if( have_buffered == 0 )
5280#endif /* MBEDTLS_SSL_PROTO_DTLS */
5281 {
5282 ret = ssl_get_next_record( ssl );
5283 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5284 continue;
5285
5286 if( ret != 0 )
5287 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005288 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005289 return( ret );
5290 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005291 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005292 }
5293
5294 ret = mbedtls_ssl_handle_message_type( ssl );
5295
Hanno Becker40f50842018-08-15 14:48:01 +01005296#if defined(MBEDTLS_SSL_PROTO_DTLS)
5297 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5298 {
5299 /* Buffer future message */
5300 ret = ssl_buffer_message( ssl );
5301 if( ret != 0 )
5302 return( ret );
5303
5304 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5305 }
5306#endif /* MBEDTLS_SSL_PROTO_DTLS */
5307
Hanno Becker90333da2017-10-10 11:27:13 +01005308 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5309 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005310
5311 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005312 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005313 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005314 return( ret );
5315 }
5316
Hanno Becker327c93b2018-08-15 13:56:18 +01005317 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005318 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005319 {
5320 mbedtls_ssl_update_handshake_status( ssl );
5321 }
Simon Butcher99000142016-10-13 17:21:01 +01005322 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005323 else
Simon Butcher99000142016-10-13 17:21:01 +01005324 {
Hanno Becker02f59072018-08-15 14:00:24 +01005325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005326 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005327 }
5328
5329 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5330
5331 return( 0 );
5332}
5333
Hanno Becker40f50842018-08-15 14:48:01 +01005334#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005335static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005336{
Hanno Becker40f50842018-08-15 14:48:01 +01005337 if( ssl->in_left > ssl->next_record_offset )
5338 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005339
Hanno Becker40f50842018-08-15 14:48:01 +01005340 return( 0 );
5341}
5342
5343static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5344{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005345 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005346 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005347 int ret = 0;
5348
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005349 if( hs == NULL )
5350 return( -1 );
5351
Hanno Beckere00ae372018-08-20 09:39:42 +01005352 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5353
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005354 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5355 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5356 {
5357 /* Check if we have seen a ChangeCipherSpec before.
5358 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005359 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005360 {
5361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5362 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005363 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005364 }
5365
Hanno Becker39b8bc92018-08-28 17:17:13 +01005366 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005367 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5368 ssl->in_msglen = 1;
5369 ssl->in_msg[0] = 1;
5370
5371 /* As long as they are equal, the exact value doesn't matter. */
5372 ssl->in_left = 0;
5373 ssl->next_record_offset = 0;
5374
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005375 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005376 goto exit;
5377 }
Hanno Becker37f95322018-08-16 13:55:32 +01005378
Hanno Beckerb8f50142018-08-28 10:01:34 +01005379#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005380 /* Debug only */
5381 {
5382 unsigned offset;
5383 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5384 {
5385 hs_buf = &hs->buffering.hs[offset];
5386 if( hs_buf->is_valid == 1 )
5387 {
5388 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5389 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005390 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005391 }
5392 }
5393 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005394#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005395
5396 /* Check if we have buffered and/or fully reassembled the
5397 * next handshake message. */
5398 hs_buf = &hs->buffering.hs[0];
5399 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5400 {
5401 /* Synthesize a record containing the buffered HS message. */
5402 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5403 ( hs_buf->data[2] << 8 ) |
5404 hs_buf->data[3];
5405
5406 /* Double-check that we haven't accidentally buffered
5407 * a message that doesn't fit into the input buffer. */
5408 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5409 {
5410 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5411 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5412 }
5413
5414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5415 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5416 hs_buf->data, msg_len + 12 );
5417
5418 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5419 ssl->in_hslen = msg_len + 12;
5420 ssl->in_msglen = msg_len + 12;
5421 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5422
5423 ret = 0;
5424 goto exit;
5425 }
5426 else
5427 {
5428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5429 hs->in_msg_seq ) );
5430 }
5431
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005432 ret = -1;
5433
5434exit:
5435
5436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5437 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005438}
5439
Hanno Beckera02b0b42018-08-21 17:20:27 +01005440static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5441 size_t desired )
5442{
5443 int offset;
5444 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005445 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5446 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005447
Hanno Becker01315ea2018-08-21 17:22:17 +01005448 /* Get rid of future records epoch first, if such exist. */
5449 ssl_free_buffered_record( ssl );
5450
5451 /* Check if we have enough space available now. */
5452 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5453 hs->buffering.total_bytes_buffered ) )
5454 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005456 return( 0 );
5457 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005458
Hanno Becker4f432ad2018-08-28 10:02:32 +01005459 /* We don't have enough space to buffer the next expected handshake
5460 * message. Remove buffers used for future messages to gain space,
5461 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005462 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5463 offset >= 0; offset-- )
5464 {
5465 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5466 offset ) );
5467
Hanno Beckerb309b922018-08-23 13:18:05 +01005468 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005469
5470 /* Check if we have enough space available now. */
5471 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5472 hs->buffering.total_bytes_buffered ) )
5473 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005475 return( 0 );
5476 }
5477 }
5478
5479 return( -1 );
5480}
5481
Hanno Becker40f50842018-08-15 14:48:01 +01005482static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5483{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005484 int ret = 0;
5485 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5486
5487 if( hs == NULL )
5488 return( 0 );
5489
5490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5491
5492 switch( ssl->in_msgtype )
5493 {
5494 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5495 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005496
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005497 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005498 break;
5499
5500 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005501 {
5502 unsigned recv_msg_seq_offset;
5503 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5504 mbedtls_ssl_hs_buffer *hs_buf;
5505 size_t msg_len = ssl->in_hslen - 12;
5506
5507 /* We should never receive an old handshake
5508 * message - double-check nonetheless. */
5509 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5510 {
5511 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5512 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5513 }
5514
5515 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5516 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5517 {
5518 /* Silently ignore -- message too far in the future */
5519 MBEDTLS_SSL_DEBUG_MSG( 2,
5520 ( "Ignore future HS message with sequence number %u, "
5521 "buffering window %u - %u",
5522 recv_msg_seq, ssl->handshake->in_msg_seq,
5523 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5524
5525 goto exit;
5526 }
5527
5528 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5529 recv_msg_seq, recv_msg_seq_offset ) );
5530
5531 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5532
5533 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005534 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005535 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005536 size_t reassembly_buf_sz;
5537
Hanno Becker37f95322018-08-16 13:55:32 +01005538 hs_buf->is_fragmented =
5539 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5540
5541 /* We copy the message back into the input buffer
5542 * after reassembly, so check that it's not too large.
5543 * This is an implementation-specific limitation
5544 * and not one from the standard, hence it is not
5545 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005546 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005547 {
5548 /* Ignore message */
5549 goto exit;
5550 }
5551
Hanno Beckere0b150f2018-08-21 15:51:03 +01005552 /* Check if we have enough space to buffer the message. */
5553 if( hs->buffering.total_bytes_buffered >
5554 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5555 {
5556 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5557 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5558 }
5559
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005560 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5561 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005562
5563 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5564 hs->buffering.total_bytes_buffered ) )
5565 {
5566 if( recv_msg_seq_offset > 0 )
5567 {
5568 /* If we can't buffer a future message because
5569 * of space limitations -- ignore. */
5570 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",
5571 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5572 (unsigned) hs->buffering.total_bytes_buffered ) );
5573 goto exit;
5574 }
Hanno Beckere1801392018-08-21 16:51:05 +01005575 else
5576 {
5577 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",
5578 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5579 (unsigned) hs->buffering.total_bytes_buffered ) );
5580 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005581
Hanno Beckera02b0b42018-08-21 17:20:27 +01005582 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005583 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005584 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",
5585 (unsigned) msg_len,
5586 (unsigned) reassembly_buf_sz,
5587 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005588 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005589 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5590 goto exit;
5591 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005592 }
5593
5594 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5595 msg_len ) );
5596
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005597 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5598 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005599 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005600 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005601 goto exit;
5602 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005603 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005604
5605 /* Prepare final header: copy msg_type, length and message_seq,
5606 * then add standardised fragment_offset and fragment_length */
5607 memcpy( hs_buf->data, ssl->in_msg, 6 );
5608 memset( hs_buf->data + 6, 0, 3 );
5609 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5610
5611 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005612
5613 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005614 }
5615 else
5616 {
5617 /* Make sure msg_type and length are consistent */
5618 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5619 {
5620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5621 /* Ignore */
5622 goto exit;
5623 }
5624 }
5625
Hanno Becker4422bbb2018-08-20 09:40:19 +01005626 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005627 {
5628 size_t frag_len, frag_off;
5629 unsigned char * const msg = hs_buf->data + 12;
5630
5631 /*
5632 * Check and copy current fragment
5633 */
5634
5635 /* Validation of header fields already done in
5636 * mbedtls_ssl_prepare_handshake_record(). */
5637 frag_off = ssl_get_hs_frag_off( ssl );
5638 frag_len = ssl_get_hs_frag_len( ssl );
5639
5640 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5641 frag_off, frag_len ) );
5642 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5643
5644 if( hs_buf->is_fragmented )
5645 {
5646 unsigned char * const bitmask = msg + msg_len;
5647 ssl_bitmask_set( bitmask, frag_off, frag_len );
5648 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5649 msg_len ) == 0 );
5650 }
5651 else
5652 {
5653 hs_buf->is_complete = 1;
5654 }
5655
5656 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5657 hs_buf->is_complete ? "" : "not yet " ) );
5658 }
5659
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005660 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005661 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005662
5663 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005664 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005665 break;
5666 }
5667
5668exit:
5669
5670 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5671 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005672}
5673#endif /* MBEDTLS_SSL_PROTO_DTLS */
5674
Hanno Becker1097b342018-08-15 14:09:41 +01005675static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005676{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005677 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005678 * Consume last content-layer message and potentially
5679 * update in_msglen which keeps track of the contents'
5680 * consumption state.
5681 *
5682 * (1) Handshake messages:
5683 * Remove last handshake message, move content
5684 * and adapt in_msglen.
5685 *
5686 * (2) Alert messages:
5687 * Consume whole record content, in_msglen = 0.
5688 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005689 * (3) Change cipher spec:
5690 * Consume whole record content, in_msglen = 0.
5691 *
5692 * (4) Application data:
5693 * Don't do anything - the record layer provides
5694 * the application data as a stream transport
5695 * and consumes through mbedtls_ssl_read only.
5696 *
5697 */
5698
5699 /* Case (1): Handshake messages */
5700 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005701 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005702 /* Hard assertion to be sure that no application data
5703 * is in flight, as corrupting ssl->in_msglen during
5704 * ssl->in_offt != NULL is fatal. */
5705 if( ssl->in_offt != NULL )
5706 {
5707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5708 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5709 }
5710
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005711 /*
5712 * Get next Handshake message in the current record
5713 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005714
Hanno Becker4a810fb2017-05-24 16:27:30 +01005715 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005716 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005717 * current handshake content: If DTLS handshake
5718 * fragmentation is used, that's the fragment
5719 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005720 * size here is faulty and should be changed at
5721 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005722 * (2) While it doesn't seem to cause problems, one
5723 * has to be very careful not to assume that in_hslen
5724 * is always <= in_msglen in a sensible communication.
5725 * Again, it's wrong for DTLS handshake fragmentation.
5726 * The following check is therefore mandatory, and
5727 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005728 * Additionally, ssl->in_hslen might be arbitrarily out of
5729 * bounds after handling a DTLS message with an unexpected
5730 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005731 */
5732 if( ssl->in_hslen < ssl->in_msglen )
5733 {
5734 ssl->in_msglen -= ssl->in_hslen;
5735 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5736 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005737
Hanno Becker4a810fb2017-05-24 16:27:30 +01005738 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5739 ssl->in_msg, ssl->in_msglen );
5740 }
5741 else
5742 {
5743 ssl->in_msglen = 0;
5744 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005745
Hanno Becker4a810fb2017-05-24 16:27:30 +01005746 ssl->in_hslen = 0;
5747 }
5748 /* Case (4): Application data */
5749 else if( ssl->in_offt != NULL )
5750 {
5751 return( 0 );
5752 }
5753 /* Everything else (CCS & Alerts) */
5754 else
5755 {
5756 ssl->in_msglen = 0;
5757 }
5758
Hanno Becker1097b342018-08-15 14:09:41 +01005759 return( 0 );
5760}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005761
Hanno Beckere74d5562018-08-15 14:26:08 +01005762static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5763{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005764 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005765 return( 1 );
5766
5767 return( 0 );
5768}
5769
Hanno Becker5f066e72018-08-16 14:56:31 +01005770#if defined(MBEDTLS_SSL_PROTO_DTLS)
5771
5772static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5773{
5774 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5775 if( hs == NULL )
5776 return;
5777
Hanno Becker01315ea2018-08-21 17:22:17 +01005778 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005779 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005780 hs->buffering.total_bytes_buffered -=
5781 hs->buffering.future_record.len;
5782
5783 mbedtls_free( hs->buffering.future_record.data );
5784 hs->buffering.future_record.data = NULL;
5785 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005786}
5787
5788static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5789{
5790 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5791 unsigned char * rec;
5792 size_t rec_len;
5793 unsigned rec_epoch;
5794
5795 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5796 return( 0 );
5797
5798 if( hs == NULL )
5799 return( 0 );
5800
Hanno Becker5f066e72018-08-16 14:56:31 +01005801 rec = hs->buffering.future_record.data;
5802 rec_len = hs->buffering.future_record.len;
5803 rec_epoch = hs->buffering.future_record.epoch;
5804
5805 if( rec == NULL )
5806 return( 0 );
5807
Hanno Becker4cb782d2018-08-20 11:19:05 +01005808 /* Only consider loading future records if the
5809 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005810 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005811 return( 0 );
5812
Hanno Becker5f066e72018-08-16 14:56:31 +01005813 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5814
5815 if( rec_epoch != ssl->in_epoch )
5816 {
5817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5818 goto exit;
5819 }
5820
5821 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5822
5823 /* Double-check that the record is not too large */
5824 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5825 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5826 {
5827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5829 }
5830
5831 memcpy( ssl->in_hdr, rec, rec_len );
5832 ssl->in_left = rec_len;
5833 ssl->next_record_offset = 0;
5834
5835 ssl_free_buffered_record( ssl );
5836
5837exit:
5838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5839 return( 0 );
5840}
5841
5842static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5843{
5844 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5845 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005846 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005847
5848 /* Don't buffer future records outside handshakes. */
5849 if( hs == NULL )
5850 return( 0 );
5851
5852 /* Only buffer handshake records (we are only interested
5853 * in Finished messages). */
5854 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5855 return( 0 );
5856
5857 /* Don't buffer more than one future epoch record. */
5858 if( hs->buffering.future_record.data != NULL )
5859 return( 0 );
5860
Hanno Becker01315ea2018-08-21 17:22:17 +01005861 /* Don't buffer record if there's not enough buffering space remaining. */
5862 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5863 hs->buffering.total_bytes_buffered ) )
5864 {
5865 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",
5866 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5867 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005868 return( 0 );
5869 }
5870
Hanno Becker5f066e72018-08-16 14:56:31 +01005871 /* Buffer record */
5872 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5873 ssl->in_epoch + 1 ) );
5874 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5875 rec_hdr_len + ssl->in_msglen );
5876
5877 /* ssl_parse_record_header() only considers records
5878 * of the next epoch as candidates for buffering. */
5879 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005880 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005881
5882 hs->buffering.future_record.data =
5883 mbedtls_calloc( 1, hs->buffering.future_record.len );
5884 if( hs->buffering.future_record.data == NULL )
5885 {
5886 /* If we run out of RAM trying to buffer a
5887 * record from the next epoch, just ignore. */
5888 return( 0 );
5889 }
5890
Hanno Becker01315ea2018-08-21 17:22:17 +01005891 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005892
Hanno Becker01315ea2018-08-21 17:22:17 +01005893 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005894 return( 0 );
5895}
5896
5897#endif /* MBEDTLS_SSL_PROTO_DTLS */
5898
Hanno Beckere74d5562018-08-15 14:26:08 +01005899static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005900{
5901 int ret;
5902
Hanno Becker5f066e72018-08-16 14:56:31 +01005903#if defined(MBEDTLS_SSL_PROTO_DTLS)
5904 /* We might have buffered a future record; if so,
5905 * and if the epoch matches now, load it.
5906 * On success, this call will set ssl->in_left to
5907 * the length of the buffered record, so that
5908 * the calls to ssl_fetch_input() below will
5909 * essentially be no-ops. */
5910 ret = ssl_load_buffered_record( ssl );
5911 if( ret != 0 )
5912 return( ret );
5913#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005914
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005915 /* Reset in pointers to default state for TLS/DTLS records,
5916 * assuming no CID and no offset between record content and
5917 * record plaintext. */
5918 ssl_update_in_pointers( ssl );
5919
5920 /* Ensure that we have enough space available for the default form
5921 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5922 * with no space for CIDs counted in). */
5923 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5924 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005926 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005927 return( ret );
5928 }
5929
5930 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005932#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005933 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5934 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005935 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005936 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5937 {
5938 ret = ssl_buffer_future_record( ssl );
5939 if( ret != 0 )
5940 return( ret );
5941
5942 /* Fall through to handling of unexpected records */
5943 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5944 }
5945
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005946 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5947 {
5948 /* Skip unexpected record (but not whole datagram) */
5949 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005950 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005951
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005952 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5953 "(header)" ) );
5954 }
5955 else
5956 {
5957 /* Skip invalid record and the rest of the datagram */
5958 ssl->next_record_offset = 0;
5959 ssl->in_left = 0;
5960
5961 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5962 "(header)" ) );
5963 }
5964
5965 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005966 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005967 }
5968#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005969 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005970 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005971
5972 /*
5973 * Read and optionally decrypt the message contents
5974 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005976 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005977 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005978 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005979 return( ret );
5980 }
5981
5982 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005983#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005984 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005985 {
Hanno Becker5903de42019-05-03 14:46:38 +01005986 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005987 if( ssl->next_record_offset < ssl->in_left )
5988 {
5989 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5990 }
5991 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005992 else
5993#endif
5994 ssl->in_left = 0;
5995
5996 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005997 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005998#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005999 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006000 {
6001 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006002 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006003 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006004 /* Except when waiting for Finished as a bad mac here
6005 * probably means something went wrong in the handshake
6006 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6007 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6008 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6009 {
6010#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6011 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6012 {
6013 mbedtls_ssl_send_alert_message( ssl,
6014 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6015 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6016 }
6017#endif
6018 return( ret );
6019 }
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006022 if( ssl->conf->badmac_limit != 0 &&
6023 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006025 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6026 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006027 }
6028#endif
6029
Hanno Becker4a810fb2017-05-24 16:27:30 +01006030 /* As above, invalid records cause
6031 * dismissal of the whole datagram. */
6032
6033 ssl->next_record_offset = 0;
6034 ssl->in_left = 0;
6035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006036 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006037 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006038 }
6039
6040 return( ret );
6041 }
6042 else
6043#endif
6044 {
6045 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6047 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006048 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006049 mbedtls_ssl_send_alert_message( ssl,
6050 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6051 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006052 }
6053#endif
6054 return( ret );
6055 }
6056 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006057
Simon Butcher99000142016-10-13 17:21:01 +01006058 return( 0 );
6059}
6060
6061int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6062{
6063 int ret;
6064
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006065 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006066 * Handle particular types of records
6067 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006068 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006069 {
Simon Butcher99000142016-10-13 17:21:01 +01006070 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6071 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006072 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006073 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006074 }
6075
Hanno Beckere678eaa2018-08-21 14:57:46 +01006076 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006077 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006078 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006079 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006080 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6081 ssl->in_msglen ) );
6082 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006083 }
6084
Hanno Beckere678eaa2018-08-21 14:57:46 +01006085 if( ssl->in_msg[0] != 1 )
6086 {
6087 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6088 ssl->in_msg[0] ) );
6089 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6090 }
6091
6092#if defined(MBEDTLS_SSL_PROTO_DTLS)
6093 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6094 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6095 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6096 {
6097 if( ssl->handshake == NULL )
6098 {
6099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6100 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6101 }
6102
6103 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6104 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6105 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006106#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006107 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006109 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006110 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006111 if( ssl->in_msglen != 2 )
6112 {
6113 /* Note: Standard allows for more than one 2 byte alert
6114 to be packed in a single message, but Mbed TLS doesn't
6115 currently support this. */
6116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6117 ssl->in_msglen ) );
6118 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6119 }
6120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006122 ssl->in_msg[0], ssl->in_msg[1] ) );
6123
6124 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006125 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006126 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006127 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006130 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006132 }
6133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6135 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6138 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006139 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006140
6141#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6142 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6143 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6144 {
Hanno Becker90333da2017-10-10 11:27:13 +01006145 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006146 /* Will be handled when trying to parse ServerHello */
6147 return( 0 );
6148 }
6149#endif
6150
6151#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6152 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6153 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6154 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6155 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6156 {
6157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6158 /* Will be handled in mbedtls_ssl_parse_certificate() */
6159 return( 0 );
6160 }
6161#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6162
6163 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006164 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006165 }
6166
Hanno Beckerc76c6192017-06-06 10:03:17 +01006167#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006168 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006169 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006170 /* Drop unexpected ApplicationData records,
6171 * except at the beginning of renegotiations */
6172 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6173 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6174#if defined(MBEDTLS_SSL_RENEGOTIATION)
6175 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6176 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006177#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006178 )
6179 {
6180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6181 return( MBEDTLS_ERR_SSL_NON_FATAL );
6182 }
6183
6184 if( ssl->handshake != NULL &&
6185 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6186 {
6187 ssl_handshake_wrapup_free_hs_transform( ssl );
6188 }
6189 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006190#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006191
Paul Bakker5121ce52009-01-03 21:22:43 +00006192 return( 0 );
6193}
6194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006196{
6197 int ret;
6198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6200 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6201 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006202 {
6203 return( ret );
6204 }
6205
6206 return( 0 );
6207}
6208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006210 unsigned char level,
6211 unsigned char message )
6212{
6213 int ret;
6214
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006215 if( ssl == NULL || ssl->conf == NULL )
6216 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006219 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006221 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006222 ssl->out_msglen = 2;
6223 ssl->out_msg[0] = level;
6224 ssl->out_msg[1] = message;
6225
Hanno Becker67bc7c32018-08-06 11:33:50 +01006226 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006229 return( ret );
6230 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006231 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006232
6233 return( 0 );
6234}
6235
Hanno Beckerb9d44792019-02-08 07:19:04 +00006236#if defined(MBEDTLS_X509_CRT_PARSE_C)
6237static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6238{
6239#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6240 if( session->peer_cert != NULL )
6241 {
6242 mbedtls_x509_crt_free( session->peer_cert );
6243 mbedtls_free( session->peer_cert );
6244 session->peer_cert = NULL;
6245 }
6246#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6247 if( session->peer_cert_digest != NULL )
6248 {
6249 /* Zeroization is not necessary. */
6250 mbedtls_free( session->peer_cert_digest );
6251 session->peer_cert_digest = NULL;
6252 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6253 session->peer_cert_digest_len = 0;
6254 }
6255#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6256}
6257#endif /* MBEDTLS_X509_CRT_PARSE_C */
6258
Paul Bakker5121ce52009-01-03 21:22:43 +00006259/*
6260 * Handshake functions
6261 */
Hanno Becker21489932019-02-05 13:20:55 +00006262#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006263/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006264int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006265{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006266 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6267 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006268
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Hanno Becker7177a882019-02-05 13:36:46 +00006271 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006272 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006273 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006274 ssl->state++;
6275 return( 0 );
6276 }
6277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006280}
6281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006282int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006283{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006284 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6285 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006287 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006288
Hanno Becker7177a882019-02-05 13:36:46 +00006289 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006291 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292 ssl->state++;
6293 return( 0 );
6294 }
6295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6297 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006298}
Gilles Peskinef9828522017-05-03 12:28:43 +02006299
Hanno Becker21489932019-02-05 13:20:55 +00006300#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006301/* Some certificate support -> implement write and parse */
6302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006303int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006306 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006308 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6309 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006311 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006312
Hanno Becker7177a882019-02-05 13:36:46 +00006313 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006315 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006316 ssl->state++;
6317 return( 0 );
6318 }
6319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006320#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006322 {
6323 if( ssl->client_auth == 0 )
6324 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006326 ssl->state++;
6327 return( 0 );
6328 }
6329
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006330#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006331 /*
6332 * If using SSLv3 and got no cert, send an Alert message
6333 * (otherwise an empty Certificate message will be sent).
6334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006335 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6336 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006337 {
6338 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006339 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6340 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6341 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006344 goto write_msg;
6345 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006346#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348#endif /* MBEDTLS_SSL_CLI_C */
6349#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006350 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006352 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6355 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006356 }
6357 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006358#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006360 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
6362 /*
6363 * 0 . 0 handshake type
6364 * 1 . 3 handshake length
6365 * 4 . 6 length of all certs
6366 * 7 . 9 length of cert. 1
6367 * 10 . n-1 peer certificate
6368 * n . n+2 length of cert. 2
6369 * n+3 . ... upper level cert, etc.
6370 */
6371 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006372 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006373
Paul Bakker29087132010-03-21 21:03:34 +00006374 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006375 {
6376 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006377 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006379 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006380 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 }
6383
6384 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6385 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6386 ssl->out_msg[i + 2] = (unsigned char)( n );
6387
6388 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6389 i += n; crt = crt->next;
6390 }
6391
6392 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6393 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6394 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6395
6396 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006397 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6398 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006399
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006400#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006401write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006402#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006403
6404 ssl->state++;
6405
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006406 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006407 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006408 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 return( ret );
6410 }
6411
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006412 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006413
Paul Bakkered27a042013-04-18 22:46:23 +02006414 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415}
6416
Hanno Becker84879e32019-01-31 07:44:03 +00006417#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006418
6419#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006420static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6421 unsigned char *crt_buf,
6422 size_t crt_buf_len )
6423{
6424 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6425
6426 if( peer_crt == NULL )
6427 return( -1 );
6428
6429 if( peer_crt->raw.len != crt_buf_len )
6430 return( -1 );
6431
Hanno Becker46f34d02019-02-08 14:00:04 +00006432 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006433}
Hanno Becker177475a2019-02-05 17:02:46 +00006434#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6435static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6436 unsigned char *crt_buf,
6437 size_t crt_buf_len )
6438{
6439 int ret;
6440 unsigned char const * const peer_cert_digest =
6441 ssl->session->peer_cert_digest;
6442 mbedtls_md_type_t const peer_cert_digest_type =
6443 ssl->session->peer_cert_digest_type;
6444 mbedtls_md_info_t const * const digest_info =
6445 mbedtls_md_info_from_type( peer_cert_digest_type );
6446 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6447 size_t digest_len;
6448
6449 if( peer_cert_digest == NULL || digest_info == NULL )
6450 return( -1 );
6451
6452 digest_len = mbedtls_md_get_size( digest_info );
6453 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6454 return( -1 );
6455
6456 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6457 if( ret != 0 )
6458 return( -1 );
6459
6460 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6461}
6462#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006463#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006464
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006465/*
6466 * Once the certificate message is read, parse it into a cert chain and
6467 * perform basic checks, but leave actual verification to the caller
6468 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006469static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6470 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006471{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006472 int ret;
6473#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6474 int crt_cnt=0;
6475#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006476 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006477 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006479 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006482 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6483 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006485 }
6486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6488 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006489 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006491 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6492 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006494 }
6495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006497
Paul Bakker5121ce52009-01-03 21:22:43 +00006498 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006499 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006501 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006502
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006503 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006504 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006507 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6508 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006509 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006510 }
6511
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006512 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6513 i += 3;
6514
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006515 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006516 while( i < ssl->in_hslen )
6517 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006518 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006519 if ( i + 3 > ssl->in_hslen ) {
6520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006521 mbedtls_ssl_send_alert_message( ssl,
6522 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6523 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006524 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6525 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006526 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6527 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006528 if( ssl->in_msg[i] != 0 )
6529 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006530 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006531 mbedtls_ssl_send_alert_message( ssl,
6532 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6533 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006534 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 }
6536
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006537 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006538 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6539 | (unsigned int) ssl->in_msg[i + 2];
6540 i += 3;
6541
6542 if( n < 128 || i + n > ssl->in_hslen )
6543 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006544 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006545 mbedtls_ssl_send_alert_message( ssl,
6546 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6547 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006548 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006549 }
6550
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006551 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006552#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6553 if( crt_cnt++ == 0 &&
6554 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6555 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006556 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006557 /* During client-side renegotiation, check that the server's
6558 * end-CRTs hasn't changed compared to the initial handshake,
6559 * mitigating the triple handshake attack. On success, reuse
6560 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006561 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6562 if( ssl_check_peer_crt_unchanged( ssl,
6563 &ssl->in_msg[i],
6564 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006565 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6567 mbedtls_ssl_send_alert_message( ssl,
6568 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6569 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6570 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006571 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006572
6573 /* Now we can safely free the original chain. */
6574 ssl_clear_peer_cert( ssl->session );
6575 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006576#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6577
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006578 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006579#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006580 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006581#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006582 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006583 * it in-place from the input buffer instead of making a copy. */
6584 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6585#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006586 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006587 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006588 case 0: /*ok*/
6589 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6590 /* Ignore certificate with an unknown algorithm: maybe a
6591 prior certificate was already trusted. */
6592 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006593
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006594 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6595 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6596 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006597
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006598 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6599 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6600 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006601
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006602 default:
6603 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6604 crt_parse_der_failed:
6605 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6606 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6607 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006608 }
6609
6610 i += n;
6611 }
6612
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006613 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006614 return( 0 );
6615}
6616
Hanno Becker4a55f632019-02-05 12:49:06 +00006617#if defined(MBEDTLS_SSL_SRV_C)
6618static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6619{
6620 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6621 return( -1 );
6622
6623#if defined(MBEDTLS_SSL_PROTO_SSL3)
6624 /*
6625 * Check if the client sent an empty certificate
6626 */
6627 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6628 {
6629 if( ssl->in_msglen == 2 &&
6630 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6631 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6632 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6633 {
6634 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6635 return( 0 );
6636 }
6637
6638 return( -1 );
6639 }
6640#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6641
6642#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6643 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6644 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6645 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6646 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6647 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6648 {
6649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6650 return( 0 );
6651 }
6652
6653 return( -1 );
6654#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6655 MBEDTLS_SSL_PROTO_TLS1_2 */
6656}
6657#endif /* MBEDTLS_SSL_SRV_C */
6658
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006659/* Check if a certificate message is expected.
6660 * Return either
6661 * - SSL_CERTIFICATE_EXPECTED, or
6662 * - SSL_CERTIFICATE_SKIP
6663 * indicating whether a Certificate message is expected or not.
6664 */
6665#define SSL_CERTIFICATE_EXPECTED 0
6666#define SSL_CERTIFICATE_SKIP 1
6667static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6668 int authmode )
6669{
6670 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006671 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006672
6673 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6674 return( SSL_CERTIFICATE_SKIP );
6675
6676#if defined(MBEDTLS_SSL_SRV_C)
6677 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6678 {
6679 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6680 return( SSL_CERTIFICATE_SKIP );
6681
6682 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6683 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006684 ssl->session_negotiate->verify_result =
6685 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6686 return( SSL_CERTIFICATE_SKIP );
6687 }
6688 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006689#else
6690 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006691#endif /* MBEDTLS_SSL_SRV_C */
6692
6693 return( SSL_CERTIFICATE_EXPECTED );
6694}
6695
Hanno Becker68636192019-02-05 14:36:34 +00006696static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6697 int authmode,
6698 mbedtls_x509_crt *chain,
6699 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006700{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006701 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006702 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006703 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006704 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006705
Hanno Becker8927c832019-04-03 12:52:50 +01006706 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6707 void *p_vrfy;
6708
Hanno Becker68636192019-02-05 14:36:34 +00006709 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6710 return( 0 );
6711
Hanno Becker8927c832019-04-03 12:52:50 +01006712 if( ssl->f_vrfy != NULL )
6713 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006714 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006715 f_vrfy = ssl->f_vrfy;
6716 p_vrfy = ssl->p_vrfy;
6717 }
6718 else
6719 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006720 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006721 f_vrfy = ssl->conf->f_vrfy;
6722 p_vrfy = ssl->conf->p_vrfy;
6723 }
6724
Hanno Becker68636192019-02-05 14:36:34 +00006725 /*
6726 * Main check: verify certificate
6727 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006728#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6729 if( ssl->conf->f_ca_cb != NULL )
6730 {
6731 ((void) rs_ctx);
6732 have_ca_chain = 1;
6733
6734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006735 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006736 chain,
6737 ssl->conf->f_ca_cb,
6738 ssl->conf->p_ca_cb,
6739 ssl->conf->cert_profile,
6740 ssl->hostname,
6741 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006742 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006743 }
6744 else
6745#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6746 {
6747 mbedtls_x509_crt *ca_chain;
6748 mbedtls_x509_crl *ca_crl;
6749
6750#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6751 if( ssl->handshake->sni_ca_chain != NULL )
6752 {
6753 ca_chain = ssl->handshake->sni_ca_chain;
6754 ca_crl = ssl->handshake->sni_ca_crl;
6755 }
6756 else
6757#endif
6758 {
6759 ca_chain = ssl->conf->ca_chain;
6760 ca_crl = ssl->conf->ca_crl;
6761 }
6762
6763 if( ca_chain != NULL )
6764 have_ca_chain = 1;
6765
6766 ret = mbedtls_x509_crt_verify_restartable(
6767 chain,
6768 ca_chain, ca_crl,
6769 ssl->conf->cert_profile,
6770 ssl->hostname,
6771 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006772 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006773 }
Hanno Becker68636192019-02-05 14:36:34 +00006774
6775 if( ret != 0 )
6776 {
6777 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6778 }
6779
6780#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6781 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6782 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6783#endif
6784
6785 /*
6786 * Secondary checks: always done, but change 'ret' only if it was 0
6787 */
6788
6789#if defined(MBEDTLS_ECP_C)
6790 {
6791 const mbedtls_pk_context *pk = &chain->pk;
6792
6793 /* If certificate uses an EC key, make sure the curve is OK */
6794 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6795 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6796 {
6797 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6798
6799 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6800 if( ret == 0 )
6801 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6802 }
6803 }
6804#endif /* MBEDTLS_ECP_C */
6805
6806 if( mbedtls_ssl_check_cert_usage( chain,
6807 ciphersuite_info,
6808 ! ssl->conf->endpoint,
6809 &ssl->session_negotiate->verify_result ) != 0 )
6810 {
6811 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6812 if( ret == 0 )
6813 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6814 }
6815
6816 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6817 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6818 * with details encoded in the verification flags. All other kinds
6819 * of error codes, including those from the user provided f_vrfy
6820 * functions, are treated as fatal and lead to a failure of
6821 * ssl_parse_certificate even if verification was optional. */
6822 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6823 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6824 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6825 {
6826 ret = 0;
6827 }
6828
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006829 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006830 {
6831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6832 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6833 }
6834
6835 if( ret != 0 )
6836 {
6837 uint8_t alert;
6838
6839 /* The certificate may have been rejected for several reasons.
6840 Pick one and send the corresponding alert. Which alert to send
6841 may be a subject of debate in some cases. */
6842 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6843 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6844 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6845 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6847 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6849 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6850 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6851 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6852 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6853 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6854 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6855 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6856 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6857 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6858 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6859 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6860 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6861 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6862 else
6863 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6864 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6865 alert );
6866 }
6867
6868#if defined(MBEDTLS_DEBUG_C)
6869 if( ssl->session_negotiate->verify_result != 0 )
6870 {
6871 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6872 ssl->session_negotiate->verify_result ) );
6873 }
6874 else
6875 {
6876 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6877 }
6878#endif /* MBEDTLS_DEBUG_C */
6879
6880 return( ret );
6881}
6882
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006883#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6884static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6885 unsigned char *start, size_t len )
6886{
6887 int ret;
6888 /* Remember digest of the peer's end-CRT. */
6889 ssl->session_negotiate->peer_cert_digest =
6890 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6891 if( ssl->session_negotiate->peer_cert_digest == NULL )
6892 {
6893 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6894 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6895 mbedtls_ssl_send_alert_message( ssl,
6896 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6897 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6898
6899 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6900 }
6901
6902 ret = mbedtls_md( mbedtls_md_info_from_type(
6903 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6904 start, len,
6905 ssl->session_negotiate->peer_cert_digest );
6906
6907 ssl->session_negotiate->peer_cert_digest_type =
6908 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6909 ssl->session_negotiate->peer_cert_digest_len =
6910 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6911
6912 return( ret );
6913}
6914
6915static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6916 unsigned char *start, size_t len )
6917{
6918 unsigned char *end = start + len;
6919 int ret;
6920
6921 /* Make a copy of the peer's raw public key. */
6922 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6923 ret = mbedtls_pk_parse_subpubkey( &start, end,
6924 &ssl->handshake->peer_pubkey );
6925 if( ret != 0 )
6926 {
6927 /* We should have parsed the public key before. */
6928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6929 }
6930
6931 return( 0 );
6932}
6933#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6934
Hanno Becker68636192019-02-05 14:36:34 +00006935int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6936{
6937 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006938 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006939#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6940 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6941 ? ssl->handshake->sni_authmode
6942 : ssl->conf->authmode;
6943#else
6944 const int authmode = ssl->conf->authmode;
6945#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006946 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006947 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006948
6949 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6950
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006951 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6952 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006953 {
6954 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006955 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006956 }
6957
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006958#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6959 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006960 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006961 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006962 chain = ssl->handshake->ecrs_peer_cert;
6963 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006964 goto crt_verify;
6965 }
6966#endif
6967
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006968 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006969 {
6970 /* mbedtls_ssl_read_record may have sent an alert already. We
6971 let it decide whether to alert. */
6972 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006973 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006974 }
6975
Hanno Becker4a55f632019-02-05 12:49:06 +00006976#if defined(MBEDTLS_SSL_SRV_C)
6977 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6978 {
6979 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006980
6981 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006982 ret = 0;
6983 else
6984 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006985
Hanno Becker6bdfab22019-02-05 13:11:17 +00006986 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006987 }
6988#endif /* MBEDTLS_SSL_SRV_C */
6989
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006990 /* Clear existing peer CRT structure in case we tried to
6991 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006992 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006993
6994 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6995 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006996 {
6997 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6998 sizeof( mbedtls_x509_crt ) ) );
6999 mbedtls_ssl_send_alert_message( ssl,
7000 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7001 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007002
Hanno Becker3dad3112019-02-05 17:19:52 +00007003 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7004 goto exit;
7005 }
7006 mbedtls_x509_crt_init( chain );
7007
7008 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007009 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007010 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007011
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007012#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7013 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007014 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007015
7016crt_verify:
7017 if( ssl->handshake->ecrs_enabled)
7018 rs_ctx = &ssl->handshake->ecrs_ctx;
7019#endif
7020
Hanno Becker68636192019-02-05 14:36:34 +00007021 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007022 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007023 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007024 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007025
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007026#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007027 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007028 unsigned char *crt_start, *pk_start;
7029 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007030
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007031 /* We parse the CRT chain without copying, so
7032 * these pointers point into the input buffer,
7033 * and are hence still valid after freeing the
7034 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007035
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007036 crt_start = chain->raw.p;
7037 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007038
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007039 pk_start = chain->pk_raw.p;
7040 pk_len = chain->pk_raw.len;
7041
7042 /* Free the CRT structures before computing
7043 * digest and copying the peer's public key. */
7044 mbedtls_x509_crt_free( chain );
7045 mbedtls_free( chain );
7046 chain = NULL;
7047
7048 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007049 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007050 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007051
7052 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7053 if( ret != 0 )
7054 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007055 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007056#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7057 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007058 ssl->session_negotiate->peer_cert = chain;
7059 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007060#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
Hanno Becker6bdfab22019-02-05 13:11:17 +00007064exit:
7065
Hanno Becker3dad3112019-02-05 17:19:52 +00007066 if( ret == 0 )
7067 ssl->state++;
7068
7069#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7070 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7071 {
7072 ssl->handshake->ecrs_peer_cert = chain;
7073 chain = NULL;
7074 }
7075#endif
7076
7077 if( chain != NULL )
7078 {
7079 mbedtls_x509_crt_free( chain );
7080 mbedtls_free( chain );
7081 }
7082
Paul Bakker5121ce52009-01-03 21:22:43 +00007083 return( ret );
7084}
Hanno Becker21489932019-02-05 13:20:55 +00007085#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007087int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007088{
7089 int ret;
7090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007094 ssl->out_msglen = 1;
7095 ssl->out_msg[0] = 1;
7096
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 ssl->state++;
7098
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007099 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007100 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007101 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 return( ret );
7103 }
7104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007105 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007106
7107 return( 0 );
7108}
7109
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007110int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007111{
7112 int ret;
7113
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007114 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007115
Hanno Becker327c93b2018-08-15 13:56:18 +01007116 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007117 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007118 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 return( ret );
7120 }
7121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007122 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007123 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007125 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7126 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007127 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007128 }
7129
Hanno Beckere678eaa2018-08-21 14:57:46 +01007130 /* CCS records are only accepted if they have length 1 and content '1',
7131 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007132
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007133 /*
7134 * Switch to our negotiated transform and session parameters for inbound
7135 * data.
7136 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007138 ssl->transform_in = ssl->transform_negotiate;
7139 ssl->session_in = ssl->session_negotiate;
7140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007142 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007143 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007144#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 ssl_dtls_replay_reset( ssl );
7146#endif
7147
7148 /* Increment epoch */
7149 if( ++ssl->in_epoch == 0 )
7150 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007152 /* This is highly unlikely to happen for legitimate reasons, so
7153 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007154 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007155 }
7156 }
7157 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007158#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007159 memset( ssl->in_ctr, 0, 8 );
7160
Hanno Becker79594fd2019-05-08 09:38:41 +01007161 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7164 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007166 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007169 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7170 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007171 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007172 }
7173 }
7174#endif
7175
Paul Bakker5121ce52009-01-03 21:22:43 +00007176 ssl->state++;
7177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007179
7180 return( 0 );
7181}
7182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7184 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007185{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007186 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007188#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7189 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7190 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007191 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007192 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007193#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007194#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7195#if defined(MBEDTLS_SHA512_C)
7196 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007197 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7198 else
7199#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007200#if defined(MBEDTLS_SHA256_C)
7201 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007202 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007203 else
7204#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007205#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007206 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007208 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007209 }
Paul Bakker380da532012-04-18 16:10:25 +00007210}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007212void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007213{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7215 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007216 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7217 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007218#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007219#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7220#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007222 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007223 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7224#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007225 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007226#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007227#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007229#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007230 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007231 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007233 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007234#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007235#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007237}
7238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007240 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007241{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007242#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7243 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007244 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7245 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007246#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007247#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7248#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007249#if defined(MBEDTLS_USE_PSA_CRYPTO)
7250 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7251#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007252 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007253#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007254#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007255#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007256#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007257 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007258#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007259 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007260#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007261#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007263}
7264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007265#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7266 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7267static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007268 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007269{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007270 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7271 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007272}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007273#endif
Paul Bakker380da532012-04-18 16:10:25 +00007274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007275#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7276#if defined(MBEDTLS_SHA256_C)
7277static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007278 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007279{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007280#if defined(MBEDTLS_USE_PSA_CRYPTO)
7281 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7282#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007283 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007284#endif
Paul Bakker380da532012-04-18 16:10:25 +00007285}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007286#endif
Paul Bakker380da532012-04-18 16:10:25 +00007287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007288#if defined(MBEDTLS_SHA512_C)
7289static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007290 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007291{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007292#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007293 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007294#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007295 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007296#endif
Paul Bakker380da532012-04-18 16:10:25 +00007297}
Paul Bakker769075d2012-11-24 11:26:46 +01007298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007299#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007304{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007305 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007306 mbedtls_md5_context md5;
7307 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007308
Paul Bakker5121ce52009-01-03 21:22:43 +00007309 unsigned char padbuf[48];
7310 unsigned char md5sum[16];
7311 unsigned char sha1sum[20];
7312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007313 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007314 if( !session )
7315 session = ssl->session;
7316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007318
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007319 mbedtls_md5_init( &md5 );
7320 mbedtls_sha1_init( &sha1 );
7321
7322 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7323 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007324
7325 /*
7326 * SSLv3:
7327 * hash =
7328 * MD5( master + pad2 +
7329 * MD5( handshake + sender + master + pad1 ) )
7330 * + SHA1( master + pad2 +
7331 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007332 */
7333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007334#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007335 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7336 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007337#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007339#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007340 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7341 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007342#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007344 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007345 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007346
Paul Bakker1ef83d62012-04-11 12:09:53 +00007347 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007349 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7350 mbedtls_md5_update_ret( &md5, session->master, 48 );
7351 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7352 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007353
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007354 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7355 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7356 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7357 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007358
Paul Bakker1ef83d62012-04-11 12:09:53 +00007359 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007361 mbedtls_md5_starts_ret( &md5 );
7362 mbedtls_md5_update_ret( &md5, session->master, 48 );
7363 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7364 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7365 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007366
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007367 mbedtls_sha1_starts_ret( &sha1 );
7368 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7369 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7370 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7371 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007373 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007375 mbedtls_md5_free( &md5 );
7376 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007377
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007378 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7379 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7380 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007387static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007389{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007390 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007391 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007392 mbedtls_md5_context md5;
7393 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007396 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007397 if( !session )
7398 session = ssl->session;
7399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007400 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007401
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007402 mbedtls_md5_init( &md5 );
7403 mbedtls_sha1_init( &sha1 );
7404
7405 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7406 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007407
Paul Bakker1ef83d62012-04-11 12:09:53 +00007408 /*
7409 * TLSv1:
7410 * hash = PRF( master, finished_label,
7411 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7412 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007414#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007415 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7416 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007417#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007419#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007420 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7421 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007422#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007424 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007425 ? "client finished"
7426 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007427
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007428 mbedtls_md5_finish_ret( &md5, padbuf );
7429 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007431 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007432 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007434 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007435
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007436 mbedtls_md5_free( &md5 );
7437 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007438
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007439 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007441 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007444
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7446#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007447static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007448 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007449{
7450 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007451 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007452 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007453#if defined(MBEDTLS_USE_PSA_CRYPTO)
7454 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007455 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007456 psa_status_t status;
7457#else
7458 mbedtls_sha256_context sha256;
7459#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007462 if( !session )
7463 session = ssl->session;
7464
Andrzej Kurekeb342242019-01-29 09:14:33 -05007465 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7466 ? "client finished"
7467 : "server finished";
7468
7469#if defined(MBEDTLS_USE_PSA_CRYPTO)
7470 sha256_psa = psa_hash_operation_init();
7471
7472 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7473
7474 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7475 if( status != PSA_SUCCESS )
7476 {
7477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7478 return;
7479 }
7480
7481 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7482 if( status != PSA_SUCCESS )
7483 {
7484 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7485 return;
7486 }
7487 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7488#else
7489
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007490 mbedtls_sha256_init( &sha256 );
7491
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007493
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007494 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007495
7496 /*
7497 * TLSv1.2:
7498 * hash = PRF( master, finished_label,
7499 * Hash( handshake ) )[0.11]
7500 */
7501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007502#if !defined(MBEDTLS_SHA256_ALT)
7503 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007504 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007505#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007506
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007507 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007508 mbedtls_sha256_free( &sha256 );
7509#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007510
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007511 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007512 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007514 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007515
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007516 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007518 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007521
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007523static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007525{
7526 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007527 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007528 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007529#if defined(MBEDTLS_USE_PSA_CRYPTO)
7530 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007531 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007532 psa_status_t status;
7533#else
7534 mbedtls_sha512_context sha512;
7535#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007537 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007538 if( !session )
7539 session = ssl->session;
7540
Andrzej Kurekeb342242019-01-29 09:14:33 -05007541 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7542 ? "client finished"
7543 : "server finished";
7544
7545#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007546 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007547
7548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7549
Andrzej Kurek972fba52019-01-30 03:29:12 -05007550 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007551 if( status != PSA_SUCCESS )
7552 {
7553 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7554 return;
7555 }
7556
Andrzej Kurek972fba52019-01-30 03:29:12 -05007557 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007558 if( status != PSA_SUCCESS )
7559 {
7560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7561 return;
7562 }
7563 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7564#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007565 mbedtls_sha512_init( &sha512 );
7566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007567 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007568
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007569 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007570
7571 /*
7572 * TLSv1.2:
7573 * hash = PRF( master, finished_label,
7574 * Hash( handshake ) )[0.11]
7575 */
7576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007577#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007578 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7579 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007580#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007581
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007582 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007583 mbedtls_sha512_free( &sha512 );
7584#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007585
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007586 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007587 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007589 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007590
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007591 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007594}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595#endif /* MBEDTLS_SHA512_C */
7596#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007597
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007598static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007599{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007601
7602 /*
7603 * Free our handshake params
7604 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007605 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007607 ssl->handshake = NULL;
7608
7609 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007610 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007611 */
7612 if( ssl->transform )
7613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007614 mbedtls_ssl_transform_free( ssl->transform );
7615 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007616 }
7617 ssl->transform = ssl->transform_negotiate;
7618 ssl->transform_negotiate = NULL;
7619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007621}
7622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007623void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007624{
7625 int resume = ssl->handshake->resume;
7626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007627 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629#if defined(MBEDTLS_SSL_RENEGOTIATION)
7630 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007632 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633 ssl->renego_records_seen = 0;
7634 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007635#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007636
7637 /*
7638 * Free the previous session and switch in the current one
7639 */
Paul Bakker0a597072012-09-25 21:55:46 +00007640 if( ssl->session )
7641 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007642#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007643 /* RFC 7366 3.1: keep the EtM state */
7644 ssl->session_negotiate->encrypt_then_mac =
7645 ssl->session->encrypt_then_mac;
7646#endif
7647
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007648 mbedtls_ssl_session_free( ssl->session );
7649 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007650 }
7651 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007652 ssl->session_negotiate = NULL;
7653
Paul Bakker0a597072012-09-25 21:55:46 +00007654 /*
7655 * Add cache entry
7656 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007657 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007658 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007659 resume == 0 )
7660 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007661 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007663 }
Paul Bakker0a597072012-09-25 21:55:46 +00007664
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007665#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007666 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007667 ssl->handshake->flight != NULL )
7668 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007669 /* Cancel handshake timer */
7670 ssl_set_timer( ssl, 0 );
7671
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007672 /* Keep last flight around in case we need to resend it:
7673 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007674 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007675 }
7676 else
7677#endif
7678 ssl_handshake_wrapup_free_hs_transform( ssl );
7679
Paul Bakker48916f92012-09-16 19:57:18 +00007680 ssl->state++;
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007683}
7684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007685int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007686{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007687 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007691 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007692
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007693 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007694
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007695 /*
7696 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7697 * may define some other value. Currently (early 2016), no defined
7698 * ciphersuite does this (and this is unlikely to change as activity has
7699 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007701 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007704 ssl->verify_data_len = hash_len;
7705 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007706#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007707
Paul Bakker5121ce52009-01-03 21:22:43 +00007708 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7710 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007711
7712 /*
7713 * In case of session resuming, invert the client and server
7714 * ChangeCipherSpec messages order.
7715 */
Paul Bakker0a597072012-09-25 21:55:46 +00007716 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007717 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007719 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007721#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007723 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007725#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007726 }
7727 else
7728 ssl->state++;
7729
Paul Bakker48916f92012-09-16 19:57:18 +00007730 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007731 * Switch to our negotiated transform and session parameters for outbound
7732 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007733 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007737 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007738 {
7739 unsigned char i;
7740
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007741 /* Remember current epoch settings for resending */
7742 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007743 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007744
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007745 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007746 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007747
7748 /* Increment epoch */
7749 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007750 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007751 break;
7752
7753 /* The loop goes to its end iff the counter is wrapping */
7754 if( i == 0 )
7755 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7757 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007758 }
7759 }
7760 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007761#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007762 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007763
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007764 ssl->transform_out = ssl->transform_negotiate;
7765 ssl->session_out = ssl->session_negotiate;
7766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7768 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007769 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007770 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7773 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007774 }
7775 }
7776#endif
7777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007778#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007779 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007781#endif
7782
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007783 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007784 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007786 return( ret );
7787 }
7788
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007789#if defined(MBEDTLS_SSL_PROTO_DTLS)
7790 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7791 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7792 {
7793 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7794 return( ret );
7795 }
7796#endif
7797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007799
7800 return( 0 );
7801}
7802
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007803#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007804#define SSL_MAX_HASH_LEN 36
7805#else
7806#define SSL_MAX_HASH_LEN 12
7807#endif
7808
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007809int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007810{
Paul Bakker23986e52011-04-24 08:57:21 +00007811 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007812 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007813 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007816
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007817 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007818
Hanno Becker327c93b2018-08-15 13:56:18 +01007819 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007822 return( ret );
7823 }
7824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007825 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007828 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7829 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007830 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007831 }
7832
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007833 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007834#if defined(MBEDTLS_SSL_PROTO_SSL3)
7835 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007836 hash_len = 36;
7837 else
7838#endif
7839 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007840
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7842 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007843 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007845 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7846 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007848 }
7849
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007851 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007854 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7855 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007857 }
7858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007859#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007860 ssl->verify_data_len = hash_len;
7861 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007862#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007863
Paul Bakker0a597072012-09-25 21:55:46 +00007864 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007865 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007866#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007867 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007871 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007873#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007874 }
7875 else
7876 ssl->state++;
7877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007878#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007879 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007881#endif
7882
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007883 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007884
7885 return( 0 );
7886}
7887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007888static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007889{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007891
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7893 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7894 mbedtls_md5_init( &handshake->fin_md5 );
7895 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007896 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7897 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007898#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007899#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7900#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007901#if defined(MBEDTLS_USE_PSA_CRYPTO)
7902 handshake->fin_sha256_psa = psa_hash_operation_init();
7903 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7904#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007906 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007907#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007908#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007910#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007911 handshake->fin_sha384_psa = psa_hash_operation_init();
7912 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007913#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007914 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007915 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007916#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007917#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007918#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007919
7920 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007921
7922#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7923 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7924 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7925#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_DHM_C)
7928 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007929#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007930#if defined(MBEDTLS_ECDH_C)
7931 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007932#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007933#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007934 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007935#if defined(MBEDTLS_SSL_CLI_C)
7936 handshake->ecjpake_cache = NULL;
7937 handshake->ecjpake_cache_len = 0;
7938#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007939#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007940
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007941#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007942 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007943#endif
7944
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007945#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7946 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7947#endif
Hanno Becker75173122019-02-06 16:18:31 +00007948
7949#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7950 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7951 mbedtls_pk_init( &handshake->peer_pubkey );
7952#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007953}
7954
Hanno Beckera18d1322018-01-03 14:27:32 +00007955void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007956{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007957 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7960 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007961
Hanno Beckerd56ed242018-01-03 15:32:51 +00007962#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007963 mbedtls_md_init( &transform->md_ctx_enc );
7964 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007965#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966}
7967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007968void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007969{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007971}
7972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007973static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007974{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007976 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007977 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007978 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007981 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982
7983 /*
7984 * Either the pointers are now NULL or cleared properly and can be freed.
7985 * Now allocate missing structures.
7986 */
7987 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007988 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007989 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007990 }
Paul Bakker48916f92012-09-16 19:57:18 +00007991
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007992 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007993 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007994 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007995 }
Paul Bakker48916f92012-09-16 19:57:18 +00007996
Paul Bakker82788fb2014-10-20 13:59:19 +02007997 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007998 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007999 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008000 }
Paul Bakker48916f92012-09-16 19:57:18 +00008001
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008002 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008003 if( ssl->handshake == NULL ||
8004 ssl->transform_negotiate == NULL ||
8005 ssl->session_negotiate == NULL )
8006 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008007 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008009 mbedtls_free( ssl->handshake );
8010 mbedtls_free( ssl->transform_negotiate );
8011 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008012
8013 ssl->handshake = NULL;
8014 ssl->transform_negotiate = NULL;
8015 ssl->session_negotiate = NULL;
8016
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008017 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008018 }
8019
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008020 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008021 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008022 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008023 ssl_handshake_params_init( ssl->handshake );
8024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8027 {
8028 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008029
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008030 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8031 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8032 else
8033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008034
8035 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008036 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008037#endif
8038
Paul Bakker48916f92012-09-16 19:57:18 +00008039 return( 0 );
8040}
8041
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008042#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008043/* Dummy cookie callbacks for defaults */
8044static int ssl_cookie_write_dummy( void *ctx,
8045 unsigned char **p, unsigned char *end,
8046 const unsigned char *cli_id, size_t cli_id_len )
8047{
8048 ((void) ctx);
8049 ((void) p);
8050 ((void) end);
8051 ((void) cli_id);
8052 ((void) cli_id_len);
8053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008054 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008055}
8056
8057static int ssl_cookie_check_dummy( void *ctx,
8058 const unsigned char *cookie, size_t cookie_len,
8059 const unsigned char *cli_id, size_t cli_id_len )
8060{
8061 ((void) ctx);
8062 ((void) cookie);
8063 ((void) cookie_len);
8064 ((void) cli_id);
8065 ((void) cli_id_len);
8066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008067 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008068}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008069#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008070
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008071/* Once ssl->out_hdr as the address of the beginning of the
8072 * next outgoing record is set, deduce the other pointers.
8073 *
8074 * Note: For TLS, we save the implicit record sequence number
8075 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8076 * and the caller has to make sure there's space for this.
8077 */
8078
8079static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8080 mbedtls_ssl_transform *transform )
8081{
8082#if defined(MBEDTLS_SSL_PROTO_DTLS)
8083 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8084 {
8085 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008086#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008087 ssl->out_cid = ssl->out_ctr + 8;
8088 ssl->out_len = ssl->out_cid;
8089 if( transform != NULL )
8090 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008091#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008092 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008093#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008094 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008095 }
8096 else
8097#endif
8098 {
8099 ssl->out_ctr = ssl->out_hdr - 8;
8100 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008101#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008102 ssl->out_cid = ssl->out_len;
8103#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008104 ssl->out_iv = ssl->out_hdr + 5;
8105 }
8106
8107 /* Adjust out_msg to make space for explicit IV, if used. */
8108 if( transform != NULL &&
8109 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8110 {
8111 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8112 }
8113 else
8114 ssl->out_msg = ssl->out_iv;
8115}
8116
8117/* Once ssl->in_hdr as the address of the beginning of the
8118 * next incoming record is set, deduce the other pointers.
8119 *
8120 * Note: For TLS, we save the implicit record sequence number
8121 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8122 * and the caller has to make sure there's space for this.
8123 */
8124
Hanno Becker79594fd2019-05-08 09:38:41 +01008125static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008126{
Hanno Becker79594fd2019-05-08 09:38:41 +01008127 /* This function sets the pointers to match the case
8128 * of unprotected TLS/DTLS records, with both ssl->in_iv
8129 * and ssl->in_msg pointing to the beginning of the record
8130 * content.
8131 *
8132 * When decrypting a protected record, ssl->in_msg
8133 * will be shifted to point to the beginning of the
8134 * record plaintext.
8135 */
8136
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008137#if defined(MBEDTLS_SSL_PROTO_DTLS)
8138 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8139 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008140 /* This sets the header pointers to match records
8141 * without CID. When we receive a record containing
8142 * a CID, the fields are shifted accordingly in
8143 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008144 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008145#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008146 ssl->in_cid = ssl->in_ctr + 8;
8147 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008148#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008149 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008150#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008151 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008152 }
8153 else
8154#endif
8155 {
8156 ssl->in_ctr = ssl->in_hdr - 8;
8157 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008158#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008159 ssl->in_cid = ssl->in_len;
8160#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008161 ssl->in_iv = ssl->in_hdr + 5;
8162 }
8163
Hanno Becker79594fd2019-05-08 09:38:41 +01008164 /* This will be adjusted at record decryption time. */
8165 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008166}
8167
Paul Bakker5121ce52009-01-03 21:22:43 +00008168/*
8169 * Initialize an SSL context
8170 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008171void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8172{
8173 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8174}
8175
8176/*
8177 * Setup an SSL context
8178 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008179
8180static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8181{
8182 /* Set the incoming and outgoing record pointers. */
8183#if defined(MBEDTLS_SSL_PROTO_DTLS)
8184 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8185 {
8186 ssl->out_hdr = ssl->out_buf;
8187 ssl->in_hdr = ssl->in_buf;
8188 }
8189 else
8190#endif /* MBEDTLS_SSL_PROTO_DTLS */
8191 {
8192 ssl->out_hdr = ssl->out_buf + 8;
8193 ssl->in_hdr = ssl->in_buf + 8;
8194 }
8195
8196 /* Derive other internal pointers. */
8197 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008198 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008199}
8200
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008201int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008202 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008203{
Paul Bakker48916f92012-09-16 19:57:18 +00008204 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008205
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008206 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008207
8208 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008209 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008210 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008211
8212 /* Set to NULL in case of an error condition */
8213 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008214
Angus Grattond8213d02016-05-25 20:56:48 +10008215 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8216 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008217 {
Angus Grattond8213d02016-05-25 20:56:48 +10008218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008219 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008220 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008221 }
8222
8223 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8224 if( ssl->out_buf == NULL )
8225 {
8226 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008227 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008228 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008229 }
8230
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008231 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008232
Paul Bakker48916f92012-09-16 19:57:18 +00008233 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008234 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008235
8236 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008237
8238error:
8239 mbedtls_free( ssl->in_buf );
8240 mbedtls_free( ssl->out_buf );
8241
8242 ssl->conf = NULL;
8243
8244 ssl->in_buf = NULL;
8245 ssl->out_buf = NULL;
8246
8247 ssl->in_hdr = NULL;
8248 ssl->in_ctr = NULL;
8249 ssl->in_len = NULL;
8250 ssl->in_iv = NULL;
8251 ssl->in_msg = NULL;
8252
8253 ssl->out_hdr = NULL;
8254 ssl->out_ctr = NULL;
8255 ssl->out_len = NULL;
8256 ssl->out_iv = NULL;
8257 ssl->out_msg = NULL;
8258
k-stachowiak9f7798e2018-07-31 16:52:32 +02008259 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008260}
8261
8262/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008263 * Reset an initialized and used SSL context for re-use while retaining
8264 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008265 *
8266 * If partial is non-zero, keep data in the input buffer and client ID.
8267 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008268 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008269static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008270{
Paul Bakker48916f92012-09-16 19:57:18 +00008271 int ret;
8272
Hanno Becker7e772132018-08-10 12:38:21 +01008273#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8274 !defined(MBEDTLS_SSL_SRV_C)
8275 ((void) partial);
8276#endif
8277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008278 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008279
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008280 /* Cancel any possibly running timer */
8281 ssl_set_timer( ssl, 0 );
8282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008283#if defined(MBEDTLS_SSL_RENEGOTIATION)
8284 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008285 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008286
8287 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8289 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008290#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008291 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008292
Paul Bakker7eb013f2011-10-06 12:37:39 +00008293 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008294 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008295
8296 ssl->in_msgtype = 0;
8297 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008299 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008300 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008301#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008302#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008303 ssl_dtls_replay_reset( ssl );
8304#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008305
8306 ssl->in_hslen = 0;
8307 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008308
8309 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008310
8311 ssl->out_msgtype = 0;
8312 ssl->out_msglen = 0;
8313 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008314#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8315 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008316 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008317#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008318
Hanno Becker19859472018-08-06 09:40:20 +01008319 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8320
Paul Bakker48916f92012-09-16 19:57:18 +00008321 ssl->transform_in = NULL;
8322 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008323
Hanno Becker78640902018-08-13 16:35:15 +01008324 ssl->session_in = NULL;
8325 ssl->session_out = NULL;
8326
Angus Grattond8213d02016-05-25 20:56:48 +10008327 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008328
8329#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008330 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008331#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8332 {
8333 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008334 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008335 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8338 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8341 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008342 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008343 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8344 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008345 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008346 }
8347#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008348
Paul Bakker48916f92012-09-16 19:57:18 +00008349 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008351 mbedtls_ssl_transform_free( ssl->transform );
8352 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008353 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008354 }
Paul Bakker48916f92012-09-16 19:57:18 +00008355
Paul Bakkerc0463502013-02-14 11:19:38 +01008356 if( ssl->session )
8357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008358 mbedtls_ssl_session_free( ssl->session );
8359 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008360 ssl->session = NULL;
8361 }
8362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008364 ssl->alpn_chosen = NULL;
8365#endif
8366
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008367#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008368#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008369 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008370#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008371 {
8372 mbedtls_free( ssl->cli_id );
8373 ssl->cli_id = NULL;
8374 ssl->cli_id_len = 0;
8375 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008376#endif
8377
Paul Bakker48916f92012-09-16 19:57:18 +00008378 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8379 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008380
8381 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008382}
8383
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008384/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008385 * Reset an initialized and used SSL context for re-use while retaining
8386 * all application-set variables, function pointers and data.
8387 */
8388int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8389{
8390 return( ssl_session_reset_int( ssl, 0 ) );
8391}
8392
8393/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008394 * SSL set accessors
8395 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008396void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008397{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008398 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008399}
8400
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008401void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008402{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008403 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008404}
8405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008406#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008407void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008408{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008409 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008410}
8411#endif
8412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008413#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008414void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008415{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008416 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008417}
8418#endif
8419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008421
Hanno Becker1841b0a2018-08-24 11:13:57 +01008422void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8423 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008424{
8425 ssl->disable_datagram_packing = !allow_packing;
8426}
8427
8428void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8429 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008430{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008431 conf->hs_timeout_min = min;
8432 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008433}
8434#endif
8435
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008436void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008437{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008438 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008439}
8440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008441#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008442void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008443 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008444 void *p_vrfy )
8445{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008446 conf->f_vrfy = f_vrfy;
8447 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008448}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008449#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008450
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008451void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008452 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008453 void *p_rng )
8454{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008455 conf->f_rng = f_rng;
8456 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008457}
8458
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008459void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008460 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008461 void *p_dbg )
8462{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008463 conf->f_dbg = f_dbg;
8464 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008465}
8466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008467void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008468 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008469 mbedtls_ssl_send_t *f_send,
8470 mbedtls_ssl_recv_t *f_recv,
8471 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008472{
8473 ssl->p_bio = p_bio;
8474 ssl->f_send = f_send;
8475 ssl->f_recv = f_recv;
8476 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008477}
8478
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008479#if defined(MBEDTLS_SSL_PROTO_DTLS)
8480void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8481{
8482 ssl->mtu = mtu;
8483}
8484#endif
8485
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008486void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008487{
8488 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008489}
8490
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008491void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8492 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008493 mbedtls_ssl_set_timer_t *f_set_timer,
8494 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008495{
8496 ssl->p_timer = p_timer;
8497 ssl->f_set_timer = f_set_timer;
8498 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008499
8500 /* Make sure we start with no timer running */
8501 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008502}
8503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008504#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008505void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008506 void *p_cache,
8507 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8508 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008509{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008510 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008511 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008512 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008513}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008514#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#if defined(MBEDTLS_SSL_CLI_C)
8517int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008518{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008519 int ret;
8520
8521 if( ssl == NULL ||
8522 session == NULL ||
8523 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008524 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008526 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008527 }
8528
Hanno Becker52055ae2019-02-06 14:30:46 +00008529 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8530 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008531 return( ret );
8532
Paul Bakker0a597072012-09-25 21:55:46 +00008533 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008534
8535 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008536}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008537#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008538
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008539void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008540 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008541{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008542 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8543 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8544 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8545 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008546}
8547
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008548void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008549 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008550 int major, int minor )
8551{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008553 return;
8554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008555 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008556 return;
8557
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008558 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008559}
8560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008561#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008562void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008563 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008564{
8565 conf->cert_profile = profile;
8566}
8567
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008568/* Append a new keycert entry to a (possibly empty) list */
8569static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8570 mbedtls_x509_crt *cert,
8571 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008572{
niisato8ee24222018-06-25 19:05:48 +09008573 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008574
niisato8ee24222018-06-25 19:05:48 +09008575 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8576 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008577 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008578
niisato8ee24222018-06-25 19:05:48 +09008579 new_cert->cert = cert;
8580 new_cert->key = key;
8581 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008582
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008583 /* Update head is the list was null, else add to the end */
8584 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008585 {
niisato8ee24222018-06-25 19:05:48 +09008586 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008587 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008588 else
8589 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008590 mbedtls_ssl_key_cert *cur = *head;
8591 while( cur->next != NULL )
8592 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008593 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008594 }
8595
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008596 return( 0 );
8597}
8598
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008599int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008600 mbedtls_x509_crt *own_cert,
8601 mbedtls_pk_context *pk_key )
8602{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008603 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008604}
8605
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008606void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008607 mbedtls_x509_crt *ca_chain,
8608 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008609{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008610 conf->ca_chain = ca_chain;
8611 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008612
8613#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8614 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8615 * cannot be used together. */
8616 conf->f_ca_cb = NULL;
8617 conf->p_ca_cb = NULL;
8618#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008619}
Hanno Becker5adaad92019-03-27 16:54:37 +00008620
8621#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8622void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008623 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008624 void *p_ca_cb )
8625{
8626 conf->f_ca_cb = f_ca_cb;
8627 conf->p_ca_cb = p_ca_cb;
8628
8629 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8630 * cannot be used together. */
8631 conf->ca_chain = NULL;
8632 conf->ca_crl = NULL;
8633}
8634#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008635#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008636
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008637#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8638int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8639 mbedtls_x509_crt *own_cert,
8640 mbedtls_pk_context *pk_key )
8641{
8642 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8643 own_cert, pk_key ) );
8644}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008645
8646void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8647 mbedtls_x509_crt *ca_chain,
8648 mbedtls_x509_crl *ca_crl )
8649{
8650 ssl->handshake->sni_ca_chain = ca_chain;
8651 ssl->handshake->sni_ca_crl = ca_crl;
8652}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008653
8654void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8655 int authmode )
8656{
8657 ssl->handshake->sni_authmode = authmode;
8658}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008659#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8660
Hanno Becker8927c832019-04-03 12:52:50 +01008661#if defined(MBEDTLS_X509_CRT_PARSE_C)
8662void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8663 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8664 void *p_vrfy )
8665{
8666 ssl->f_vrfy = f_vrfy;
8667 ssl->p_vrfy = p_vrfy;
8668}
8669#endif
8670
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008671#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008672/*
8673 * Set EC J-PAKE password for current handshake
8674 */
8675int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8676 const unsigned char *pw,
8677 size_t pw_len )
8678{
8679 mbedtls_ecjpake_role role;
8680
Janos Follath8eb64132016-06-03 15:40:57 +01008681 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008682 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8683
8684 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8685 role = MBEDTLS_ECJPAKE_SERVER;
8686 else
8687 role = MBEDTLS_ECJPAKE_CLIENT;
8688
8689 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8690 role,
8691 MBEDTLS_MD_SHA256,
8692 MBEDTLS_ECP_DP_SECP256R1,
8693 pw, pw_len ) );
8694}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008695#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008697#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008698
8699static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8700{
8701 /* Remove reference to existing PSK, if any. */
8702#if defined(MBEDTLS_USE_PSA_CRYPTO)
8703 if( conf->psk_opaque != 0 )
8704 {
8705 /* The maintenance of the PSK key slot is the
8706 * user's responsibility. */
8707 conf->psk_opaque = 0;
8708 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008709 /* This and the following branch should never
8710 * be taken simultaenously as we maintain the
8711 * invariant that raw and opaque PSKs are never
8712 * configured simultaneously. As a safeguard,
8713 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008714#endif /* MBEDTLS_USE_PSA_CRYPTO */
8715 if( conf->psk != NULL )
8716 {
8717 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8718
8719 mbedtls_free( conf->psk );
8720 conf->psk = NULL;
8721 conf->psk_len = 0;
8722 }
8723
8724 /* Remove reference to PSK identity, if any. */
8725 if( conf->psk_identity != NULL )
8726 {
8727 mbedtls_free( conf->psk_identity );
8728 conf->psk_identity = NULL;
8729 conf->psk_identity_len = 0;
8730 }
8731}
8732
Hanno Becker7390c712018-11-15 13:33:04 +00008733/* This function assumes that PSK identity in the SSL config is unset.
8734 * It checks that the provided identity is well-formed and attempts
8735 * to make a copy of it in the SSL config.
8736 * On failure, the PSK identity in the config remains unset. */
8737static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8738 unsigned char const *psk_identity,
8739 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008740{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008741 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008742 if( psk_identity == NULL ||
8743 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008744 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008745 {
8746 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8747 }
8748
Hanno Becker7390c712018-11-15 13:33:04 +00008749 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8750 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008751 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008752
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008753 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008754 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008755
8756 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008757}
8758
Hanno Becker7390c712018-11-15 13:33:04 +00008759int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8760 const unsigned char *psk, size_t psk_len,
8761 const unsigned char *psk_identity, size_t psk_identity_len )
8762{
8763 int ret;
8764 /* Remove opaque/raw PSK + PSK Identity */
8765 ssl_conf_remove_psk( conf );
8766
8767 /* Check and set raw PSK */
8768 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8769 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8770 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8771 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8772 conf->psk_len = psk_len;
8773 memcpy( conf->psk, psk, conf->psk_len );
8774
8775 /* Check and set PSK Identity */
8776 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8777 if( ret != 0 )
8778 ssl_conf_remove_psk( conf );
8779
8780 return( ret );
8781}
8782
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008783static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8784{
8785#if defined(MBEDTLS_USE_PSA_CRYPTO)
8786 if( ssl->handshake->psk_opaque != 0 )
8787 {
8788 ssl->handshake->psk_opaque = 0;
8789 }
8790 else
8791#endif /* MBEDTLS_USE_PSA_CRYPTO */
8792 if( ssl->handshake->psk != NULL )
8793 {
8794 mbedtls_platform_zeroize( ssl->handshake->psk,
8795 ssl->handshake->psk_len );
8796 mbedtls_free( ssl->handshake->psk );
8797 ssl->handshake->psk_len = 0;
8798 }
8799}
8800
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008801int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8802 const unsigned char *psk, size_t psk_len )
8803{
8804 if( psk == NULL || ssl->handshake == NULL )
8805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8806
8807 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8809
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008810 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008811
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008812 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008813 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008814
8815 ssl->handshake->psk_len = psk_len;
8816 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8817
8818 return( 0 );
8819}
8820
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008821#if defined(MBEDTLS_USE_PSA_CRYPTO)
8822int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008823 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008824 const unsigned char *psk_identity,
8825 size_t psk_identity_len )
8826{
Hanno Becker7390c712018-11-15 13:33:04 +00008827 int ret;
8828 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008829 ssl_conf_remove_psk( conf );
8830
Hanno Becker7390c712018-11-15 13:33:04 +00008831 /* Check and set opaque PSK */
8832 if( psk_slot == 0 )
8833 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008834 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008835
8836 /* Check and set PSK Identity */
8837 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8838 psk_identity_len );
8839 if( ret != 0 )
8840 ssl_conf_remove_psk( conf );
8841
8842 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008843}
8844
8845int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008846 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008847{
8848 if( psk_slot == 0 || ssl->handshake == NULL )
8849 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8850
8851 ssl_remove_psk( ssl );
8852 ssl->handshake->psk_opaque = psk_slot;
8853 return( 0 );
8854}
8855#endif /* MBEDTLS_USE_PSA_CRYPTO */
8856
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008857void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008858 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008859 size_t),
8860 void *p_psk )
8861{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008862 conf->f_psk = f_psk;
8863 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008864}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008865#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008866
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008867#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008868
8869#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008870int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008871{
8872 int ret;
8873
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008874 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8875 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8876 {
8877 mbedtls_mpi_free( &conf->dhm_P );
8878 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008879 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008880 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008881
8882 return( 0 );
8883}
Hanno Becker470a8c42017-10-04 15:28:46 +01008884#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008885
Hanno Beckera90658f2017-10-04 15:29:08 +01008886int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8887 const unsigned char *dhm_P, size_t P_len,
8888 const unsigned char *dhm_G, size_t G_len )
8889{
8890 int ret;
8891
8892 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8893 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8894 {
8895 mbedtls_mpi_free( &conf->dhm_P );
8896 mbedtls_mpi_free( &conf->dhm_G );
8897 return( ret );
8898 }
8899
8900 return( 0 );
8901}
Paul Bakker5121ce52009-01-03 21:22:43 +00008902
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008903int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008904{
8905 int ret;
8906
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008907 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8908 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8909 {
8910 mbedtls_mpi_free( &conf->dhm_P );
8911 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008912 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008913 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008914
8915 return( 0 );
8916}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008917#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008918
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008919#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8920/*
8921 * Set the minimum length for Diffie-Hellman parameters
8922 */
8923void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8924 unsigned int bitlen )
8925{
8926 conf->dhm_min_bitlen = bitlen;
8927}
8928#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8929
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008930#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008931/*
8932 * Set allowed/preferred hashes for handshake signatures
8933 */
8934void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8935 const int *hashes )
8936{
8937 conf->sig_hashes = hashes;
8938}
Hanno Becker947194e2017-04-07 13:25:49 +01008939#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008940
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008941#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008942/*
8943 * Set the allowed elliptic curves
8944 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008945void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008946 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008947{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008948 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008949}
Hanno Becker947194e2017-04-07 13:25:49 +01008950#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008951
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008952#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008953int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008954{
Hanno Becker947194e2017-04-07 13:25:49 +01008955 /* Initialize to suppress unnecessary compiler warning */
8956 size_t hostname_len = 0;
8957
8958 /* Check if new hostname is valid before
8959 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008960 if( hostname != NULL )
8961 {
8962 hostname_len = strlen( hostname );
8963
8964 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8966 }
8967
8968 /* Now it's clear that we will overwrite the old hostname,
8969 * so we can free it safely */
8970
8971 if( ssl->hostname != NULL )
8972 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008973 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008974 mbedtls_free( ssl->hostname );
8975 }
8976
8977 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008978
Paul Bakker5121ce52009-01-03 21:22:43 +00008979 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008980 {
8981 ssl->hostname = NULL;
8982 }
8983 else
8984 {
8985 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008986 if( ssl->hostname == NULL )
8987 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008988
Hanno Becker947194e2017-04-07 13:25:49 +01008989 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008990
Hanno Becker947194e2017-04-07 13:25:49 +01008991 ssl->hostname[hostname_len] = '\0';
8992 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008993
8994 return( 0 );
8995}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008996#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008997
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008998#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008999void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009001 const unsigned char *, size_t),
9002 void *p_sni )
9003{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009004 conf->f_sni = f_sni;
9005 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009006}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009007#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009010int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009011{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009012 size_t cur_len, tot_len;
9013 const char **p;
9014
9015 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009016 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9017 * MUST NOT be truncated."
9018 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009019 */
9020 tot_len = 0;
9021 for( p = protos; *p != NULL; p++ )
9022 {
9023 cur_len = strlen( *p );
9024 tot_len += cur_len;
9025
9026 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009027 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009028 }
9029
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009030 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009031
9032 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009033}
9034
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009035const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009036{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009037 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009038}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009039#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009040
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009041void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009042{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009043 conf->max_major_ver = major;
9044 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009045}
9046
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009047void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009048{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009049 conf->min_major_ver = major;
9050 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009051}
9052
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009053#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009054void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009055{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009056 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009057}
9058#endif
9059
Janos Follath088ce432017-04-10 12:42:31 +01009060#if defined(MBEDTLS_SSL_SRV_C)
9061void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9062 char cert_req_ca_list )
9063{
9064 conf->cert_req_ca_list = cert_req_ca_list;
9065}
9066#endif
9067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009068#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009069void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009071 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009072}
9073#endif
9074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009075#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009076void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009078 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009079}
9080#endif
9081
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009082#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009083void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009084{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009085 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009086}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009087#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009090int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009091{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009093 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009094 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009095 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009096 }
9097
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009098 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009099
9100 return( 0 );
9101}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009102#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009105void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009106{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009107 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009108}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009109#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009112void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009113{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009114 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009115}
9116#endif
9117
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009118void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009119{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009120 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009121}
9122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009123#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009124void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009125{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009126 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009127}
9128
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009129void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009131 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009132}
9133
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009134void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009135 const unsigned char period[8] )
9136{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009137 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009138}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009139#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009142#if defined(MBEDTLS_SSL_CLI_C)
9143void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009144{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009145 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009146}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009147#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009148
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009149#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009150void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9151 mbedtls_ssl_ticket_write_t *f_ticket_write,
9152 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9153 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009154{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009155 conf->f_ticket_write = f_ticket_write;
9156 conf->f_ticket_parse = f_ticket_parse;
9157 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009158}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009159#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009161
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009162#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9163void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9164 mbedtls_ssl_export_keys_t *f_export_keys,
9165 void *p_export_keys )
9166{
9167 conf->f_export_keys = f_export_keys;
9168 conf->p_export_keys = p_export_keys;
9169}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009170
9171void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9172 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9173 void *p_export_keys )
9174{
9175 conf->f_export_keys_ext = f_export_keys_ext;
9176 conf->p_export_keys = p_export_keys;
9177}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009178#endif
9179
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009180#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009181void mbedtls_ssl_conf_async_private_cb(
9182 mbedtls_ssl_config *conf,
9183 mbedtls_ssl_async_sign_t *f_async_sign,
9184 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9185 mbedtls_ssl_async_resume_t *f_async_resume,
9186 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009187 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009188{
9189 conf->f_async_sign_start = f_async_sign;
9190 conf->f_async_decrypt_start = f_async_decrypt;
9191 conf->f_async_resume = f_async_resume;
9192 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009193 conf->p_async_config_data = async_config_data;
9194}
9195
Gilles Peskine8f97af72018-04-26 11:46:10 +02009196void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9197{
9198 return( conf->p_async_config_data );
9199}
9200
Gilles Peskine1febfef2018-04-30 11:54:39 +02009201void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009202{
9203 if( ssl->handshake == NULL )
9204 return( NULL );
9205 else
9206 return( ssl->handshake->user_async_ctx );
9207}
9208
Gilles Peskine1febfef2018-04-30 11:54:39 +02009209void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009210 void *ctx )
9211{
9212 if( ssl->handshake != NULL )
9213 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009214}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009215#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009216
Paul Bakker5121ce52009-01-03 21:22:43 +00009217/*
9218 * SSL get accessors
9219 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009220size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009221{
9222 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9223}
9224
Hanno Becker8b170a02017-10-10 11:51:19 +01009225int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9226{
9227 /*
9228 * Case A: We're currently holding back
9229 * a message for further processing.
9230 */
9231
9232 if( ssl->keep_current_message == 1 )
9233 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009235 return( 1 );
9236 }
9237
9238 /*
9239 * Case B: Further records are pending in the current datagram.
9240 */
9241
9242#if defined(MBEDTLS_SSL_PROTO_DTLS)
9243 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9244 ssl->in_left > ssl->next_record_offset )
9245 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009246 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009247 return( 1 );
9248 }
9249#endif /* MBEDTLS_SSL_PROTO_DTLS */
9250
9251 /*
9252 * Case C: A handshake message is being processed.
9253 */
9254
Hanno Becker8b170a02017-10-10 11:51:19 +01009255 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9256 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009257 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009258 return( 1 );
9259 }
9260
9261 /*
9262 * Case D: An application data message is being processed
9263 */
9264 if( ssl->in_offt != NULL )
9265 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009266 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009267 return( 1 );
9268 }
9269
9270 /*
9271 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009272 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009273 * we implement support for multiple alerts in single records.
9274 */
9275
9276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9277 return( 0 );
9278}
9279
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009280uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009281{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009282 if( ssl->session != NULL )
9283 return( ssl->session->verify_result );
9284
9285 if( ssl->session_negotiate != NULL )
9286 return( ssl->session_negotiate->verify_result );
9287
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009288 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009289}
9290
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009291const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009292{
Paul Bakker926c8e42013-03-06 10:23:34 +01009293 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009294 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009297}
9298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009299const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009300{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009302 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009303 {
9304 switch( ssl->minor_ver )
9305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009307 return( "DTLSv1.0" );
9308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009309 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009310 return( "DTLSv1.2" );
9311
9312 default:
9313 return( "unknown (DTLS)" );
9314 }
9315 }
9316#endif
9317
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318 switch( ssl->minor_ver )
9319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321 return( "SSLv3.0" );
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009324 return( "TLSv1.0" );
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009327 return( "TLSv1.1" );
9328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009329 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009330 return( "TLSv1.2" );
9331
Paul Bakker43ca69c2011-01-15 17:35:19 +00009332 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009333 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009334 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009335}
9336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009338{
Hanno Becker3136ede2018-08-17 15:28:19 +01009339 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009340 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009341 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009342
Hanno Becker5903de42019-05-03 14:46:38 +01009343 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9344
Hanno Becker78640902018-08-13 16:35:15 +01009345 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009346 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009348#if defined(MBEDTLS_ZLIB_SUPPORT)
9349 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9350 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009351#endif
9352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009353 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 case MBEDTLS_MODE_GCM:
9356 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009357 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009358 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009359 transform_expansion = transform->minlen;
9360 break;
9361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009362 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009363
9364 block_size = mbedtls_cipher_get_block_size(
9365 &transform->cipher_ctx_enc );
9366
Hanno Becker3136ede2018-08-17 15:28:19 +01009367 /* Expansion due to the addition of the MAC. */
9368 transform_expansion += transform->maclen;
9369
9370 /* Expansion due to the addition of CBC padding;
9371 * Theoretically up to 256 bytes, but we never use
9372 * more than the block size of the underlying cipher. */
9373 transform_expansion += block_size;
9374
9375 /* For TLS 1.1 or higher, an explicit IV is added
9376 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009377#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9378 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009379 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009380#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009381
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009382 break;
9383
9384 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009385 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009387 }
9388
Hanno Beckera0e20d02019-05-15 14:03:01 +01009389#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009390 if( transform->out_cid_len != 0 )
9391 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009392#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009393
Hanno Becker5903de42019-05-03 14:46:38 +01009394 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009395}
9396
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009397#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9398size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9399{
9400 size_t max_len;
9401
9402 /*
9403 * Assume mfl_code is correct since it was checked when set
9404 */
Angus Grattond8213d02016-05-25 20:56:48 +10009405 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009406
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009407 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009408 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009409 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009410 {
Angus Grattond8213d02016-05-25 20:56:48 +10009411 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009412 }
9413
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009414 /* During a handshake, use the value being negotiated */
9415 if( ssl->session_negotiate != NULL &&
9416 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9417 {
9418 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9419 }
9420
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009421 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009422}
9423#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9424
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009425#if defined(MBEDTLS_SSL_PROTO_DTLS)
9426static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9427{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009428 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9429 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9430 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9431 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9432 return ( 0 );
9433
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009434 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9435 return( ssl->mtu );
9436
9437 if( ssl->mtu == 0 )
9438 return( ssl->handshake->mtu );
9439
9440 return( ssl->mtu < ssl->handshake->mtu ?
9441 ssl->mtu : ssl->handshake->mtu );
9442}
9443#endif /* MBEDTLS_SSL_PROTO_DTLS */
9444
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009445int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9446{
9447 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9448
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009449#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9450 !defined(MBEDTLS_SSL_PROTO_DTLS)
9451 (void) ssl;
9452#endif
9453
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009454#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9455 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9456
9457 if( max_len > mfl )
9458 max_len = mfl;
9459#endif
9460
9461#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009462 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009463 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009464 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009465 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9466 const size_t overhead = (size_t) ret;
9467
9468 if( ret < 0 )
9469 return( ret );
9470
9471 if( mtu <= overhead )
9472 {
9473 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9474 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9475 }
9476
9477 if( max_len > mtu - overhead )
9478 max_len = mtu - overhead;
9479 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009480#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009481
Hanno Becker0defedb2018-08-10 12:35:02 +01009482#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9483 !defined(MBEDTLS_SSL_PROTO_DTLS)
9484 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009485#endif
9486
9487 return( (int) max_len );
9488}
9489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009490#if defined(MBEDTLS_X509_CRT_PARSE_C)
9491const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009492{
9493 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009494 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009495
Hanno Beckere6824572019-02-07 13:18:46 +00009496#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009497 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009498#else
9499 return( NULL );
9500#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009501}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009505int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9506 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009507{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009508 if( ssl == NULL ||
9509 dst == NULL ||
9510 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009511 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009514 }
9515
Hanno Becker52055ae2019-02-06 14:30:46 +00009516 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009517}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009518#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009519
Paul Bakker5121ce52009-01-03 21:22:43 +00009520/*
Paul Bakker1961b702013-01-25 14:49:24 +01009521 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009522 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009523int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009524{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009526
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009527 if( ssl == NULL || ssl->conf == NULL )
9528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009530#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009531 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009533#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009535 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009537#endif
9538
Paul Bakker1961b702013-01-25 14:49:24 +01009539 return( ret );
9540}
9541
9542/*
9543 * Perform the SSL handshake
9544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009545int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009546{
9547 int ret = 0;
9548
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009549 if( ssl == NULL || ssl->conf == NULL )
9550 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009552 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009557
9558 if( ret != 0 )
9559 break;
9560 }
9561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009563
9564 return( ret );
9565}
9566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009567#if defined(MBEDTLS_SSL_RENEGOTIATION)
9568#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009569/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009570 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009572static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009573{
9574 int ret;
9575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009577
9578 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009579 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9580 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009581
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009582 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009583 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009584 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009585 return( ret );
9586 }
9587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009588 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009589
9590 return( 0 );
9591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009593
9594/*
9595 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009596 * - any side: calling mbedtls_ssl_renegotiate(),
9597 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9598 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009599 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009600 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009601 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009602 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009604{
9605 int ret;
9606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009607 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009608
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009609 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9610 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009611
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009612 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9613 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009615 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009617 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009618 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009619 ssl->handshake->out_msg_seq = 1;
9620 else
9621 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009622 }
9623#endif
9624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9626 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009628 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009629 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009631 return( ret );
9632 }
9633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009634 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009635
9636 return( 0 );
9637}
9638
9639/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009640 * Renegotiate current connection on client,
9641 * or request renegotiation on server
9642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009643int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009644{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009646
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009647 if( ssl == NULL || ssl->conf == NULL )
9648 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009650#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009651 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009652 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009656
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009657 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009658
9659 /* Did we already try/start sending HelloRequest? */
9660 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009661 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009662
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009663 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009664 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009665#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009668 /*
9669 * On client, either start the renegotiation process or,
9670 * if already in progress, continue the handshake
9671 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009672 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009673 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9675 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009676
9677 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9678 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009679 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009680 return( ret );
9681 }
9682 }
9683 else
9684 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009688 return( ret );
9689 }
9690 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009691#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009692
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009693 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009694}
9695
9696/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009697 * Check record counters and renegotiate if they're above the limit.
9698 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009700{
Andres AG2196c7f2016-12-15 17:01:16 +00009701 size_t ep_len = ssl_ep_len( ssl );
9702 int in_ctr_cmp;
9703 int out_ctr_cmp;
9704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9706 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009707 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009708 {
9709 return( 0 );
9710 }
9711
Andres AG2196c7f2016-12-15 17:01:16 +00009712 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9713 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009714 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009715 ssl->conf->renego_period + ep_len, 8 - ep_len );
9716
9717 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009718 {
9719 return( 0 );
9720 }
9721
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009722 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009723 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009724}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009726
9727/*
9728 * Receive application data decrypted from the SSL layer
9729 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009730int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009731{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009732 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009733 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009734
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009735 if( ssl == NULL || ssl->conf == NULL )
9736 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009738 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009741 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009742 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009743 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009744 return( ret );
9745
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009746 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009747 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009748 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009749 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009750 return( ret );
9751 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009752 }
9753#endif
9754
Hanno Becker4a810fb2017-05-24 16:27:30 +01009755 /*
9756 * Check if renegotiation is necessary and/or handshake is
9757 * in process. If yes, perform/continue, and fall through
9758 * if an unexpected packet is received while the client
9759 * is waiting for the ServerHello.
9760 *
9761 * (There is no equivalent to the last condition on
9762 * the server-side as it is not treated as within
9763 * a handshake while waiting for the ClientHello
9764 * after a renegotiation request.)
9765 */
9766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009767#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009768 ret = ssl_check_ctr_renegotiate( ssl );
9769 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9770 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009772 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009773 return( ret );
9774 }
9775#endif
9776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009777 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009780 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9781 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009783 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009784 return( ret );
9785 }
9786 }
9787
Hanno Beckere41158b2017-10-23 13:30:32 +01009788 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009789 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009790 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009791 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009792 if( ssl->f_get_timer != NULL &&
9793 ssl->f_get_timer( ssl->p_timer ) == -1 )
9794 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009795 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009796 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009797
Hanno Becker327c93b2018-08-15 13:56:18 +01009798 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009799 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009800 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9801 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009802
Hanno Becker4a810fb2017-05-24 16:27:30 +01009803 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9804 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009805 }
9806
9807 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009809 {
9810 /*
9811 * OpenSSL sends empty messages to randomize the IV
9812 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009813 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009814 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009816 return( 0 );
9817
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009819 return( ret );
9820 }
9821 }
9822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009823 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009826
Hanno Becker4a810fb2017-05-24 16:27:30 +01009827 /*
9828 * - For client-side, expect SERVER_HELLO_REQUEST.
9829 * - For server-side, expect CLIENT_HELLO.
9830 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9831 */
9832
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009833#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009834 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009836 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009837 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009839
9840 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009841#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009842 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009843 {
9844 continue;
9845 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009846#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009847 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009848 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009849#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009850
Hanno Becker4a810fb2017-05-24 16:27:30 +01009851#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009852 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009853 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009856
9857 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009858#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009859 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009860 {
9861 continue;
9862 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009863#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009864 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009865 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009866#endif /* MBEDTLS_SSL_SRV_C */
9867
Hanno Becker21df7f92017-10-17 11:03:26 +01009868#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009869 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009870 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9871 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9872 ssl->conf->allow_legacy_renegotiation ==
9873 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9874 {
9875 /*
9876 * Accept renegotiation request
9877 */
Paul Bakker48916f92012-09-16 19:57:18 +00009878
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009879 /* DTLS clients need to know renego is server-initiated */
9880#if defined(MBEDTLS_SSL_PROTO_DTLS)
9881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9882 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9883 {
9884 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9885 }
9886#endif
9887 ret = ssl_start_renegotiation( ssl );
9888 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9889 ret != 0 )
9890 {
9891 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9892 return( ret );
9893 }
9894 }
9895 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009896#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009897 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009898 /*
9899 * Refuse renegotiation
9900 */
9901
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009902 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904#if defined(MBEDTLS_SSL_PROTO_SSL3)
9905 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009906 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009907 /* SSLv3 does not have a "no_renegotiation" warning, so
9908 we send a fatal alert and abort the connection. */
9909 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9910 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9911 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009912 }
9913 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009914#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9915#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9916 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9917 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009918 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009919 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9920 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9921 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009922 {
9923 return( ret );
9924 }
Paul Bakker48916f92012-09-16 19:57:18 +00009925 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009926 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9928 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9931 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009932 }
Paul Bakker48916f92012-09-16 19:57:18 +00009933 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009934
Hanno Becker90333da2017-10-10 11:27:13 +01009935 /* At this point, we don't know whether the renegotiation has been
9936 * completed or not. The cases to consider are the following:
9937 * 1) The renegotiation is complete. In this case, no new record
9938 * has been read yet.
9939 * 2) The renegotiation is incomplete because the client received
9940 * an application data record while awaiting the ServerHello.
9941 * 3) The renegotiation is incomplete because the client received
9942 * a non-handshake, non-application data message while awaiting
9943 * the ServerHello.
9944 * In each of these case, looping will be the proper action:
9945 * - For 1), the next iteration will read a new record and check
9946 * if it's application data.
9947 * - For 2), the loop condition isn't satisfied as application data
9948 * is present, hence continue is the same as break
9949 * - For 3), the loop condition is satisfied and read_record
9950 * will re-deliver the message that was held back by the client
9951 * when expecting the ServerHello.
9952 */
9953 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009954 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009955#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009956 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009957 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009958 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009959 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009960 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009961 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009962 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009963 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009965 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009966 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009968#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9971 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009972 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009974 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009975 }
9976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009977 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9980 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009981 }
9982
9983 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009984
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009985 /* We're going to return something now, cancel timer,
9986 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009987 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009988 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009989
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009990#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009991 /* If we requested renego but received AppData, resend HelloRequest.
9992 * Do it now, after setting in_offt, to avoid taking this branch
9993 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009994#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009995 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009997 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009998 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010000 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010001 return( ret );
10002 }
10003 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010004#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010005#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010006 }
10007
10008 n = ( len < ssl->in_msglen )
10009 ? len : ssl->in_msglen;
10010
10011 memcpy( buf, ssl->in_offt, n );
10012 ssl->in_msglen -= n;
10013
10014 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010015 {
10016 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010017 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010018 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010019 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010020 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010022 /* more data available */
10023 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010024 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010026 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010027
Paul Bakker23986e52011-04-24 08:57:21 +000010028 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010029}
10030
10031/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010032 * Send application data to be encrypted by the SSL layer, taking care of max
10033 * fragment length and buffer size.
10034 *
10035 * According to RFC 5246 Section 6.2.1:
10036 *
10037 * Zero-length fragments of Application data MAY be sent as they are
10038 * potentially useful as a traffic analysis countermeasure.
10039 *
10040 * Therefore, it is possible that the input message length is 0 and the
10041 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010042 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010043static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010044 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010045{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010046 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10047 const size_t max_len = (size_t) ret;
10048
10049 if( ret < 0 )
10050 {
10051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10052 return( ret );
10053 }
10054
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010055 if( len > max_len )
10056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010058 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010061 "maximum fragment length: %d > %d",
10062 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010063 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010064 }
10065 else
10066#endif
10067 len = max_len;
10068 }
Paul Bakker887bd502011-06-08 13:10:54 +000010069
Paul Bakker5121ce52009-01-03 21:22:43 +000010070 if( ssl->out_left != 0 )
10071 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010072 /*
10073 * The user has previously tried to send the data and
10074 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10075 * written. In this case, we expect the high-level write function
10076 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10077 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010078 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 return( ret );
10082 }
10083 }
Paul Bakker887bd502011-06-08 13:10:54 +000010084 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010085 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010086 /*
10087 * The user is trying to send a message the first time, so we need to
10088 * copy the data into the internal buffers and setup the data structure
10089 * to keep track of partial writes
10090 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010091 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010092 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010093 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010094
Hanno Becker67bc7c32018-08-06 11:33:50 +010010095 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010097 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010098 return( ret );
10099 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010100 }
10101
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010102 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010103}
10104
10105/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010106 * Write application data, doing 1/n-1 splitting if necessary.
10107 *
10108 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010109 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010110 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010112#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010113static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010114 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010115{
10116 int ret;
10117
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010118 if( ssl->conf->cbc_record_splitting ==
10119 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010120 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010121 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10122 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10123 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010124 {
10125 return( ssl_write_real( ssl, buf, len ) );
10126 }
10127
10128 if( ssl->split_done == 0 )
10129 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010130 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010131 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010132 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010133 }
10134
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010135 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10136 return( ret );
10137 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010138
10139 return( ret + 1 );
10140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010141#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010142
10143/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010144 * Write application data (public-facing wrapper)
10145 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010146int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010147{
10148 int ret;
10149
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010150 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010151
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010152 if( ssl == NULL || ssl->conf == NULL )
10153 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10154
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010155#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010156 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10157 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010158 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010159 return( ret );
10160 }
10161#endif
10162
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010163 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010164 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010165 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010166 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010167 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010168 return( ret );
10169 }
10170 }
10171
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010172#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010173 ret = ssl_write_split( ssl, buf, len );
10174#else
10175 ret = ssl_write_real( ssl, buf, len );
10176#endif
10177
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010178 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010179
10180 return( ret );
10181}
10182
10183/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010184 * Notify the peer that the connection is being closed
10185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010186int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010187{
10188 int ret;
10189
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010190 if( ssl == NULL || ssl->conf == NULL )
10191 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010194
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010195 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010196 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010199 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10201 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10202 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010204 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010205 return( ret );
10206 }
10207 }
10208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010210
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010211 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010212}
10213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010214void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010215{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010216 if( transform == NULL )
10217 return;
10218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010219#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010220 deflateEnd( &transform->ctx_deflate );
10221 inflateEnd( &transform->ctx_inflate );
10222#endif
10223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010224 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10225 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010226
Hanno Beckerd56ed242018-01-03 15:32:51 +000010227#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010228 mbedtls_md_free( &transform->md_ctx_enc );
10229 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010230#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010231
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010232 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010233}
10234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235#if defined(MBEDTLS_X509_CRT_PARSE_C)
10236static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010237{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010238 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010239
10240 while( cur != NULL )
10241 {
10242 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010243 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010244 cur = next;
10245 }
10246}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010247#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010248
Hanno Becker0271f962018-08-16 13:23:47 +010010249#if defined(MBEDTLS_SSL_PROTO_DTLS)
10250
10251static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10252{
10253 unsigned offset;
10254 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10255
10256 if( hs == NULL )
10257 return;
10258
Hanno Becker283f5ef2018-08-24 09:34:47 +010010259 ssl_free_buffered_record( ssl );
10260
Hanno Becker0271f962018-08-16 13:23:47 +010010261 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010262 ssl_buffering_free_slot( ssl, offset );
10263}
10264
10265static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10266 uint8_t slot )
10267{
10268 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10269 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010270
10271 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10272 return;
10273
Hanno Beckere605b192018-08-21 15:59:07 +010010274 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010275 {
Hanno Beckere605b192018-08-21 15:59:07 +010010276 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010277 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010278 mbedtls_free( hs_buf->data );
10279 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010280 }
10281}
10282
10283#endif /* MBEDTLS_SSL_PROTO_DTLS */
10284
Gilles Peskine9b562d52018-04-25 20:32:43 +020010285void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010286{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010287 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10288
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010289 if( handshake == NULL )
10290 return;
10291
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010292#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10293 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10294 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010295 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010296 handshake->async_in_progress = 0;
10297 }
10298#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10299
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10301 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10302 mbedtls_md5_free( &handshake->fin_md5 );
10303 mbedtls_sha1_free( &handshake->fin_sha1 );
10304#endif
10305#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10306#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010307#if defined(MBEDTLS_USE_PSA_CRYPTO)
10308 psa_hash_abort( &handshake->fin_sha256_psa );
10309#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010310 mbedtls_sha256_free( &handshake->fin_sha256 );
10311#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010312#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010313#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010314#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010315 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010316#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010317 mbedtls_sha512_free( &handshake->fin_sha512 );
10318#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010319#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010320#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_DHM_C)
10323 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010324#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010325#if defined(MBEDTLS_ECDH_C)
10326 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010327#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010328#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010329 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010330#if defined(MBEDTLS_SSL_CLI_C)
10331 mbedtls_free( handshake->ecjpake_cache );
10332 handshake->ecjpake_cache = NULL;
10333 handshake->ecjpake_cache_len = 0;
10334#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010335#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010336
Janos Follath4ae5c292016-02-10 11:27:43 +000010337#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10338 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010339 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010341#endif
10342
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010343#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10344 if( handshake->psk != NULL )
10345 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010346 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010347 mbedtls_free( handshake->psk );
10348 }
10349#endif
10350
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010351#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10352 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010353 /*
10354 * Free only the linked list wrapper, not the keys themselves
10355 * since the belong to the SNI callback
10356 */
10357 if( handshake->sni_key_cert != NULL )
10358 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010359 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010360
10361 while( cur != NULL )
10362 {
10363 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010364 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010365 cur = next;
10366 }
10367 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010368#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010369
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010370#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010371 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010372 if( handshake->ecrs_peer_cert != NULL )
10373 {
10374 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10375 mbedtls_free( handshake->ecrs_peer_cert );
10376 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010377#endif
10378
Hanno Becker75173122019-02-06 16:18:31 +000010379#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10380 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10381 mbedtls_pk_free( &handshake->peer_pubkey );
10382#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010384#if defined(MBEDTLS_SSL_PROTO_DTLS)
10385 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010386 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010387 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010388#endif
10389
Hanno Becker4a63ed42019-01-08 11:39:35 +000010390#if defined(MBEDTLS_ECDH_C) && \
10391 defined(MBEDTLS_USE_PSA_CRYPTO)
10392 psa_destroy_key( handshake->ecdh_psa_privkey );
10393#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10394
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010395 mbedtls_platform_zeroize( handshake,
10396 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010397}
10398
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010399void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010400{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010401 if( session == NULL )
10402 return;
10403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010405 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010406#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010407
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010408#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010409 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010410#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010411
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010412 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010413}
10414
Paul Bakker5121ce52009-01-03 21:22:43 +000010415/*
10416 * Free an SSL context
10417 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010418void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010419{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010420 if( ssl == NULL )
10421 return;
10422
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010423 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010424
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010425 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010426 {
Angus Grattond8213d02016-05-25 20:56:48 +100010427 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010428 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010429 }
10430
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010431 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010432 {
Angus Grattond8213d02016-05-25 20:56:48 +100010433 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010435 }
10436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010437#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010438 if( ssl->compress_buf != NULL )
10439 {
Angus Grattond8213d02016-05-25 20:56:48 +100010440 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010441 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010442 }
10443#endif
10444
Paul Bakker48916f92012-09-16 19:57:18 +000010445 if( ssl->transform )
10446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010447 mbedtls_ssl_transform_free( ssl->transform );
10448 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010449 }
10450
10451 if( ssl->handshake )
10452 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010453 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10455 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010456
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010457 mbedtls_free( ssl->handshake );
10458 mbedtls_free( ssl->transform_negotiate );
10459 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010460 }
10461
Paul Bakkerc0463502013-02-14 11:19:38 +010010462 if( ssl->session )
10463 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010464 mbedtls_ssl_session_free( ssl->session );
10465 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010466 }
10467
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010468#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010469 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010470 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010471 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010472 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010473 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010474#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10477 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010478 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10480 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010481 }
10482#endif
10483
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010484#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010486#endif
10487
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010488 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010489
Paul Bakker86f04f42013-02-14 11:20:09 +010010490 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010491 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010492}
10493
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010494/*
10495 * Initialze mbedtls_ssl_config
10496 */
10497void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10498{
10499 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10500}
10501
Simon Butcherc97b6972015-12-27 23:48:17 +000010502#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010503static int ssl_preset_default_hashes[] = {
10504#if defined(MBEDTLS_SHA512_C)
10505 MBEDTLS_MD_SHA512,
10506 MBEDTLS_MD_SHA384,
10507#endif
10508#if defined(MBEDTLS_SHA256_C)
10509 MBEDTLS_MD_SHA256,
10510 MBEDTLS_MD_SHA224,
10511#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010512#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010513 MBEDTLS_MD_SHA1,
10514#endif
10515 MBEDTLS_MD_NONE
10516};
Simon Butcherc97b6972015-12-27 23:48:17 +000010517#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010518
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010519static int ssl_preset_suiteb_ciphersuites[] = {
10520 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10521 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10522 0
10523};
10524
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010525#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010526static int ssl_preset_suiteb_hashes[] = {
10527 MBEDTLS_MD_SHA256,
10528 MBEDTLS_MD_SHA384,
10529 MBEDTLS_MD_NONE
10530};
10531#endif
10532
10533#if defined(MBEDTLS_ECP_C)
10534static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010535#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010536 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010537#endif
10538#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010539 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010540#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010541 MBEDTLS_ECP_DP_NONE
10542};
10543#endif
10544
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010545/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010546 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010548int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010549 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010550{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010551#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010552 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010553#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010555 /* Use the functions here so that they are covered in tests,
10556 * but otherwise access member directly for efficiency */
10557 mbedtls_ssl_conf_endpoint( conf, endpoint );
10558 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010559
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010560 /*
10561 * Things that are common to all presets
10562 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010563#if defined(MBEDTLS_SSL_CLI_C)
10564 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10565 {
10566 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10567#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10568 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10569#endif
10570 }
10571#endif
10572
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010573#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010574 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010575#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010576
10577#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10578 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10579#endif
10580
10581#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10582 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10583#endif
10584
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010585#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10586 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10587#endif
10588
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010589#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010590 conf->f_cookie_write = ssl_cookie_write_dummy;
10591 conf->f_cookie_check = ssl_cookie_check_dummy;
10592#endif
10593
10594#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10595 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10596#endif
10597
Janos Follath088ce432017-04-10 12:42:31 +010010598#if defined(MBEDTLS_SSL_SRV_C)
10599 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10600#endif
10601
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010602#if defined(MBEDTLS_SSL_PROTO_DTLS)
10603 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10604 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10605#endif
10606
10607#if defined(MBEDTLS_SSL_RENEGOTIATION)
10608 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010609 memset( conf->renego_period, 0x00, 2 );
10610 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010611#endif
10612
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010613#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10614 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10615 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010616 const unsigned char dhm_p[] =
10617 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10618 const unsigned char dhm_g[] =
10619 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10620
Hanno Beckera90658f2017-10-04 15:29:08 +010010621 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10622 dhm_p, sizeof( dhm_p ),
10623 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010624 {
10625 return( ret );
10626 }
10627 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010628#endif
10629
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010630 /*
10631 * Preset-specific defaults
10632 */
10633 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010634 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010635 /*
10636 * NSA Suite B
10637 */
10638 case MBEDTLS_SSL_PRESET_SUITEB:
10639 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10640 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10641 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10642 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10643
10644 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10645 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10646 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10647 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10648 ssl_preset_suiteb_ciphersuites;
10649
10650#if defined(MBEDTLS_X509_CRT_PARSE_C)
10651 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010652#endif
10653
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010654#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010655 conf->sig_hashes = ssl_preset_suiteb_hashes;
10656#endif
10657
10658#if defined(MBEDTLS_ECP_C)
10659 conf->curve_list = ssl_preset_suiteb_curves;
10660#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010661 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010662
10663 /*
10664 * Default
10665 */
10666 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010667 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10668 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10669 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10670 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10671 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10672 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10673 MBEDTLS_SSL_MIN_MINOR_VERSION :
10674 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010675 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10676 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10677
10678#if defined(MBEDTLS_SSL_PROTO_DTLS)
10679 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10680 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10681#endif
10682
10683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10684 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10685 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10686 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10687 mbedtls_ssl_list_ciphersuites();
10688
10689#if defined(MBEDTLS_X509_CRT_PARSE_C)
10690 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10691#endif
10692
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010693#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010694 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010695#endif
10696
10697#if defined(MBEDTLS_ECP_C)
10698 conf->curve_list = mbedtls_ecp_grp_id_list();
10699#endif
10700
10701#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10702 conf->dhm_min_bitlen = 1024;
10703#endif
10704 }
10705
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010706 return( 0 );
10707}
10708
10709/*
10710 * Free mbedtls_ssl_config
10711 */
10712void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10713{
10714#if defined(MBEDTLS_DHM_C)
10715 mbedtls_mpi_free( &conf->dhm_P );
10716 mbedtls_mpi_free( &conf->dhm_G );
10717#endif
10718
10719#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10720 if( conf->psk != NULL )
10721 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010722 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010723 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010724 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010725 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010726 }
10727
10728 if( conf->psk_identity != NULL )
10729 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010730 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010731 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010732 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010733 conf->psk_identity_len = 0;
10734 }
10735#endif
10736
10737#if defined(MBEDTLS_X509_CRT_PARSE_C)
10738 ssl_key_cert_free( conf->key_cert );
10739#endif
10740
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010741 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010742}
10743
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010744#if defined(MBEDTLS_PK_C) && \
10745 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010746/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010747 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751#if defined(MBEDTLS_RSA_C)
10752 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10753 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010754#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010755#if defined(MBEDTLS_ECDSA_C)
10756 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10757 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010758#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010759 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010760}
10761
Hanno Becker7e5437a2017-04-28 17:15:26 +010010762unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10763{
10764 switch( type ) {
10765 case MBEDTLS_PK_RSA:
10766 return( MBEDTLS_SSL_SIG_RSA );
10767 case MBEDTLS_PK_ECDSA:
10768 case MBEDTLS_PK_ECKEY:
10769 return( MBEDTLS_SSL_SIG_ECDSA );
10770 default:
10771 return( MBEDTLS_SSL_SIG_ANON );
10772 }
10773}
10774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010775mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010776{
10777 switch( sig )
10778 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010779#if defined(MBEDTLS_RSA_C)
10780 case MBEDTLS_SSL_SIG_RSA:
10781 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010783#if defined(MBEDTLS_ECDSA_C)
10784 case MBEDTLS_SSL_SIG_ECDSA:
10785 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010786#endif
10787 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010788 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010789 }
10790}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010791#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010792
Hanno Becker7e5437a2017-04-28 17:15:26 +010010793#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10794 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10795
10796/* Find an entry in a signature-hash set matching a given hash algorithm. */
10797mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10798 mbedtls_pk_type_t sig_alg )
10799{
10800 switch( sig_alg )
10801 {
10802 case MBEDTLS_PK_RSA:
10803 return( set->rsa );
10804 case MBEDTLS_PK_ECDSA:
10805 return( set->ecdsa );
10806 default:
10807 return( MBEDTLS_MD_NONE );
10808 }
10809}
10810
10811/* Add a signature-hash-pair to a signature-hash set */
10812void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10813 mbedtls_pk_type_t sig_alg,
10814 mbedtls_md_type_t md_alg )
10815{
10816 switch( sig_alg )
10817 {
10818 case MBEDTLS_PK_RSA:
10819 if( set->rsa == MBEDTLS_MD_NONE )
10820 set->rsa = md_alg;
10821 break;
10822
10823 case MBEDTLS_PK_ECDSA:
10824 if( set->ecdsa == MBEDTLS_MD_NONE )
10825 set->ecdsa = md_alg;
10826 break;
10827
10828 default:
10829 break;
10830 }
10831}
10832
10833/* Allow exactly one hash algorithm for each signature. */
10834void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10835 mbedtls_md_type_t md_alg )
10836{
10837 set->rsa = md_alg;
10838 set->ecdsa = md_alg;
10839}
10840
10841#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10842 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10843
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010844/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010845 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010846 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010847mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010848{
10849 switch( hash )
10850 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010851#if defined(MBEDTLS_MD5_C)
10852 case MBEDTLS_SSL_HASH_MD5:
10853 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010855#if defined(MBEDTLS_SHA1_C)
10856 case MBEDTLS_SSL_HASH_SHA1:
10857 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010858#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010859#if defined(MBEDTLS_SHA256_C)
10860 case MBEDTLS_SSL_HASH_SHA224:
10861 return( MBEDTLS_MD_SHA224 );
10862 case MBEDTLS_SSL_HASH_SHA256:
10863 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010864#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865#if defined(MBEDTLS_SHA512_C)
10866 case MBEDTLS_SSL_HASH_SHA384:
10867 return( MBEDTLS_MD_SHA384 );
10868 case MBEDTLS_SSL_HASH_SHA512:
10869 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010870#endif
10871 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010872 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010873 }
10874}
10875
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010876/*
10877 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10878 */
10879unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10880{
10881 switch( md )
10882 {
10883#if defined(MBEDTLS_MD5_C)
10884 case MBEDTLS_MD_MD5:
10885 return( MBEDTLS_SSL_HASH_MD5 );
10886#endif
10887#if defined(MBEDTLS_SHA1_C)
10888 case MBEDTLS_MD_SHA1:
10889 return( MBEDTLS_SSL_HASH_SHA1 );
10890#endif
10891#if defined(MBEDTLS_SHA256_C)
10892 case MBEDTLS_MD_SHA224:
10893 return( MBEDTLS_SSL_HASH_SHA224 );
10894 case MBEDTLS_MD_SHA256:
10895 return( MBEDTLS_SSL_HASH_SHA256 );
10896#endif
10897#if defined(MBEDTLS_SHA512_C)
10898 case MBEDTLS_MD_SHA384:
10899 return( MBEDTLS_SSL_HASH_SHA384 );
10900 case MBEDTLS_MD_SHA512:
10901 return( MBEDTLS_SSL_HASH_SHA512 );
10902#endif
10903 default:
10904 return( MBEDTLS_SSL_HASH_NONE );
10905 }
10906}
10907
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010908#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010909/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010910 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010911 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010912 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010913int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010914{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010915 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010916
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010917 if( ssl->conf->curve_list == NULL )
10918 return( -1 );
10919
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010920 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010921 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010922 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010923
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010924 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010925}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010926#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010927
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010928#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010929/*
10930 * Check if a hash proposed by the peer is in our list.
10931 * Return 0 if we're willing to use it, -1 otherwise.
10932 */
10933int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10934 mbedtls_md_type_t md )
10935{
10936 const int *cur;
10937
10938 if( ssl->conf->sig_hashes == NULL )
10939 return( -1 );
10940
10941 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10942 if( *cur == (int) md )
10943 return( 0 );
10944
10945 return( -1 );
10946}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010947#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010948
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010949#if defined(MBEDTLS_X509_CRT_PARSE_C)
10950int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10951 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010952 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010953 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010954{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010955 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010957 int usage = 0;
10958#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010959#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010960 const char *ext_oid;
10961 size_t ext_len;
10962#endif
10963
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010964#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10965 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010966 ((void) cert);
10967 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010968 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010969#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010971#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10972 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010973 {
10974 /* Server part of the key exchange */
10975 switch( ciphersuite->key_exchange )
10976 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010977 case MBEDTLS_KEY_EXCHANGE_RSA:
10978 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010979 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010980 break;
10981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010982 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10983 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10984 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10985 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010986 break;
10987
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010988 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10989 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010990 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010991 break;
10992
10993 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010994 case MBEDTLS_KEY_EXCHANGE_NONE:
10995 case MBEDTLS_KEY_EXCHANGE_PSK:
10996 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10997 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010998 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010999 usage = 0;
11000 }
11001 }
11002 else
11003 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011004 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11005 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011006 }
11007
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011008 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011009 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011010 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011011 ret = -1;
11012 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011013#else
11014 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011015#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11018 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011019 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011020 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11021 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011022 }
11023 else
11024 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011025 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11026 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011027 }
11028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011029 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011030 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011031 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011032 ret = -1;
11033 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011034#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011035
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011036 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011038#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011039
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011040/*
11041 * Convert version numbers to/from wire format
11042 * and, for DTLS, to/from TLS equivalent.
11043 *
11044 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011045 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011046 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11047 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11048 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011050 unsigned char ver[2] )
11051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052#if defined(MBEDTLS_SSL_PROTO_DTLS)
11053 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011054 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011055 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011056 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11057
11058 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11059 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11060 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011061 else
11062#else
11063 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011064#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011065 {
11066 ver[0] = (unsigned char) major;
11067 ver[1] = (unsigned char) minor;
11068 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011069}
11070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011072 const unsigned char ver[2] )
11073{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011074#if defined(MBEDTLS_SSL_PROTO_DTLS)
11075 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011076 {
11077 *major = 255 - ver[0] + 2;
11078 *minor = 255 - ver[1] + 1;
11079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011080 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011081 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11082 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011083 else
11084#else
11085 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011086#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011087 {
11088 *major = ver[0];
11089 *minor = ver[1];
11090 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011091}
11092
Simon Butcher99000142016-10-13 17:21:01 +010011093int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11094{
11095#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11096 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11097 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11098
11099 switch( md )
11100 {
11101#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11102#if defined(MBEDTLS_MD5_C)
11103 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011104 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011105#endif
11106#if defined(MBEDTLS_SHA1_C)
11107 case MBEDTLS_SSL_HASH_SHA1:
11108 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11109 break;
11110#endif
11111#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11112#if defined(MBEDTLS_SHA512_C)
11113 case MBEDTLS_SSL_HASH_SHA384:
11114 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11115 break;
11116#endif
11117#if defined(MBEDTLS_SHA256_C)
11118 case MBEDTLS_SSL_HASH_SHA256:
11119 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11120 break;
11121#endif
11122 default:
11123 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11124 }
11125
11126 return 0;
11127#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11128 (void) ssl;
11129 (void) md;
11130
11131 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11132#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11133}
11134
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11136 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11137int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11138 unsigned char *output,
11139 unsigned char *data, size_t data_len )
11140{
11141 int ret = 0;
11142 mbedtls_md5_context mbedtls_md5;
11143 mbedtls_sha1_context mbedtls_sha1;
11144
11145 mbedtls_md5_init( &mbedtls_md5 );
11146 mbedtls_sha1_init( &mbedtls_sha1 );
11147
11148 /*
11149 * digitally-signed struct {
11150 * opaque md5_hash[16];
11151 * opaque sha_hash[20];
11152 * };
11153 *
11154 * md5_hash
11155 * MD5(ClientHello.random + ServerHello.random
11156 * + ServerParams);
11157 * sha_hash
11158 * SHA(ClientHello.random + ServerHello.random
11159 * + ServerParams);
11160 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011161 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011162 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 goto exit;
11165 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011166 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011167 ssl->handshake->randbytes, 64 ) ) != 0 )
11168 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011170 goto exit;
11171 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011172 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011173 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 goto exit;
11176 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011177 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011178 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 goto exit;
11181 }
11182
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011183 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011184 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 goto exit;
11187 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 ssl->handshake->randbytes, 64 ) ) != 0 )
11190 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011191 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011192 goto exit;
11193 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011194 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011195 data_len ) ) != 0 )
11196 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011197 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011198 goto exit;
11199 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011200 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011201 output + 16 ) ) != 0 )
11202 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011203 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011204 goto exit;
11205 }
11206
11207exit:
11208 mbedtls_md5_free( &mbedtls_md5 );
11209 mbedtls_sha1_free( &mbedtls_sha1 );
11210
11211 if( ret != 0 )
11212 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11213 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11214
11215 return( ret );
11216
11217}
11218#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11219 MBEDTLS_SSL_PROTO_TLS1_1 */
11220
11221#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11222 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011223
11224#if defined(MBEDTLS_USE_PSA_CRYPTO)
11225int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11226 unsigned char *hash, size_t *hashlen,
11227 unsigned char *data, size_t data_len,
11228 mbedtls_md_type_t md_alg )
11229{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011230 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011231 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011232 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11233
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011234 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011235
11236 if( ( status = psa_hash_setup( &hash_operation,
11237 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011238 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011239 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011240 goto exit;
11241 }
11242
Andrzej Kurek814feff2019-01-14 04:35:19 -050011243 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11244 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011245 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011246 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 goto exit;
11248 }
11249
Andrzej Kurek814feff2019-01-14 04:35:19 -050011250 if( ( status = psa_hash_update( &hash_operation,
11251 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011252 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011253 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 goto exit;
11255 }
11256
Andrzej Kurek814feff2019-01-14 04:35:19 -050011257 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11258 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011259 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011260 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011261 goto exit;
11262 }
11263
11264exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011265 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011266 {
11267 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11268 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011269 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011270 {
11271 case PSA_ERROR_NOT_SUPPORTED:
11272 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011273 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011274 case PSA_ERROR_BUFFER_TOO_SMALL:
11275 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11276 case PSA_ERROR_INSUFFICIENT_MEMORY:
11277 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11278 default:
11279 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11280 }
11281 }
11282 return( 0 );
11283}
11284
11285#else
11286
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011287int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011288 unsigned char *hash, size_t *hashlen,
11289 unsigned char *data, size_t data_len,
11290 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011291{
11292 int ret = 0;
11293 mbedtls_md_context_t ctx;
11294 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011295 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011297 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011298
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011299 mbedtls_md_init( &ctx );
11300
11301 /*
11302 * digitally-signed struct {
11303 * opaque client_random[32];
11304 * opaque server_random[32];
11305 * ServerDHParams params;
11306 * };
11307 */
11308 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11309 {
11310 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11311 goto exit;
11312 }
11313 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11314 {
11315 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11316 goto exit;
11317 }
11318 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11319 {
11320 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11321 goto exit;
11322 }
11323 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11324 {
11325 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11326 goto exit;
11327 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011328 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011329 {
11330 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11331 goto exit;
11332 }
11333
11334exit:
11335 mbedtls_md_free( &ctx );
11336
11337 if( ret != 0 )
11338 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11339 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11340
11341 return( ret );
11342}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011343#endif /* MBEDTLS_USE_PSA_CRYPTO */
11344
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011345#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11346 MBEDTLS_SSL_PROTO_TLS1_2 */
11347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011348#endif /* MBEDTLS_SSL_TLS_C */