blob: 465ca060576e382ea9c2b55e65dd112239c628d3 [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 Follath8dee8772019-07-30 12:53:32 +0100635 psa_key_derivation_operation_t generator =
636 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500637
Andrzej Kurek2f760752019-01-28 08:08:15 -0500638 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500639 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500640
Andrzej Kurekc929a822019-01-14 03:51:11 -0500641 if( md_type == MBEDTLS_MD_SHA384 )
642 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
643 else
644 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
645
Andrzej Kurek2f760752019-01-28 08:08:15 -0500646 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500647 psa_key_policy_set_usage( &policy,
648 PSA_KEY_USAGE_DERIVE,
649 alg );
650 status = psa_set_key_policy( master_slot, &policy );
651 if( status != PSA_SUCCESS )
652 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
653
654 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
655 if( status != PSA_SUCCESS )
656 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
657
658 status = psa_key_derivation( &generator,
659 master_slot, alg,
660 random, rlen,
661 (unsigned char const *) label,
662 (size_t) strlen( label ),
663 dlen );
664 if( status != PSA_SUCCESS )
665 {
666 psa_generator_abort( &generator );
667 psa_destroy_key( master_slot );
668 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
669 }
670
671 status = psa_generator_read( &generator, dstbuf, dlen );
672 if( status != PSA_SUCCESS )
673 {
674 psa_generator_abort( &generator );
675 psa_destroy_key( master_slot );
676 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
677 }
678
679 status = psa_generator_abort( &generator );
680 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500681 {
682 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500683 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500684 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500685
686 status = psa_destroy_key( master_slot );
687 if( status != PSA_SUCCESS )
688 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
689
Andrzej Kurek33171262019-01-15 03:25:18 -0500690 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500691}
692
693#else /* MBEDTLS_USE_PSA_CRYPTO */
694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200695static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100696 const unsigned char *secret, size_t slen,
697 const char *label,
698 const unsigned char *random, size_t rlen,
699 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000700{
701 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100702 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300703 unsigned char *tmp;
704 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200705 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
706 const mbedtls_md_info_t *md_info;
707 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100708 int ret;
709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200710 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000711
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200712 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
713 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200715 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100716
Ron Eldor3b350852019-05-07 18:31:49 +0300717 tmp_len = md_len + strlen( label ) + rlen;
718 tmp = mbedtls_calloc( 1, tmp_len );
719 if( tmp == NULL )
720 {
721 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
722 goto exit;
723 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000724
725 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100726 memcpy( tmp + md_len, label, nb );
727 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000728 nb += rlen;
729
730 /*
731 * Compute P_<hash>(secret, label + random)[0..dlen]
732 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200733 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300734 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
737 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
738 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100739
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100740 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000741 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200742 mbedtls_md_hmac_reset ( &md_ctx );
743 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
744 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100745
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200746 mbedtls_md_hmac_reset ( &md_ctx );
747 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
748 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000749
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100750 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000751
752 for( j = 0; j < k; j++ )
753 dstbuf[i + j] = h_i[j];
754 }
755
Ron Eldor3b350852019-05-07 18:31:49 +0300756exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200757 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100758
Ron Eldor3b350852019-05-07 18:31:49 +0300759 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500760 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000761
Ron Eldor3b350852019-05-07 18:31:49 +0300762 mbedtls_free( tmp );
763
764 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000765}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500766#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100768static int tls_prf_sha256( const unsigned char *secret, size_t slen,
769 const char *label,
770 const unsigned char *random, size_t rlen,
771 unsigned char *dstbuf, size_t dlen )
772{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200773 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100774 label, random, rlen, dstbuf, dlen ) );
775}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200776#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200779static int tls_prf_sha384( const unsigned char *secret, size_t slen,
780 const char *label,
781 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000782 unsigned char *dstbuf, size_t dlen )
783{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100785 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000786}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200787#endif /* MBEDTLS_SHA512_C */
788#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000789
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200790static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
793 defined(MBEDTLS_SSL_PROTO_TLS1_1)
794static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200795#endif
Paul Bakker380da532012-04-18 16:10:25 +0000796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#if defined(MBEDTLS_SSL_PROTO_SSL3)
798static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
799static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200800#endif
801
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200802#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
803static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
804static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200805#endif
806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200807#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
808#if defined(MBEDTLS_SHA256_C)
809static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
810static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
811static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200812#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100813
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200814#if defined(MBEDTLS_SHA512_C)
815static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
816static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
817static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100818#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200819#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000820
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100821#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100822 defined(MBEDTLS_USE_PSA_CRYPTO)
823static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
824{
825 if( ssl->conf->f_psk != NULL )
826 {
827 /* If we've used a callback to select the PSK,
828 * the static configuration is irrelevant. */
829 if( ssl->handshake->psk_opaque != 0 )
830 return( 1 );
831
832 return( 0 );
833 }
834
835 if( ssl->conf->psk_opaque != 0 )
836 return( 1 );
837
838 return( 0 );
839}
840#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100841 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100842
Ron Eldorcf280092019-05-14 20:19:13 +0300843#if defined(MBEDTLS_SSL_EXPORT_KEYS)
844static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
845{
846#if defined(MBEDTLS_SSL_PROTO_SSL3)
847 if( tls_prf == ssl3_prf )
848 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300849 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300850 }
851 else
852#endif
853#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
854 if( tls_prf == tls1_prf )
855 {
856 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
857 }
858 else
859#endif
860#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
861#if defined(MBEDTLS_SHA512_C)
862 if( tls_prf == tls_prf_sha384 )
863 {
864 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
865 }
866 else
867#endif
868#if defined(MBEDTLS_SHA256_C)
869 if( tls_prf == tls_prf_sha256 )
870 {
871 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
872 }
873 else
874#endif
875#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
876 return( MBEDTLS_SSL_TLS_PRF_NONE );
877}
878#endif /* MBEDTLS_SSL_EXPORT_KEYS */
879
Ron Eldor51d3ab52019-05-12 14:54:30 +0300880int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
881 const unsigned char *secret, size_t slen,
882 const char *label,
883 const unsigned char *random, size_t rlen,
884 unsigned char *dstbuf, size_t dlen )
885{
886 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
887
888 switch( prf )
889 {
890#if defined(MBEDTLS_SSL_PROTO_SSL3)
891 case MBEDTLS_SSL_TLS_PRF_SSL3:
892 tls_prf = ssl3_prf;
893 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300894#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300895#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
896 case MBEDTLS_SSL_TLS_PRF_TLS1:
897 tls_prf = tls1_prf;
898 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300899#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
900
901#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300902#if defined(MBEDTLS_SHA512_C)
903 case MBEDTLS_SSL_TLS_PRF_SHA384:
904 tls_prf = tls_prf_sha384;
905 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300906#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300907#if defined(MBEDTLS_SHA256_C)
908 case MBEDTLS_SSL_TLS_PRF_SHA256:
909 tls_prf = tls_prf_sha256;
910 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300911#endif /* MBEDTLS_SHA256_C */
912#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300913 default:
914 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
915 }
916
917 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
918}
919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200920int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000921{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200922 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000923#if defined(MBEDTLS_USE_PSA_CRYPTO)
924 int psa_fallthrough;
925#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000926 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000927 unsigned char keyblk[256];
928 unsigned char *key1;
929 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100930 unsigned char *mac_enc;
931 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000932 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200933 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000934 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000935 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200936 const mbedtls_cipher_info_t *cipher_info;
937 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100938
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000939 /* cf. RFC 5246, Section 8.1:
940 * "The master secret is always exactly 48 bytes in length." */
941 size_t const master_secret_len = 48;
942
Hanno Becker35b23c72018-10-23 12:10:41 +0100943#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
944 unsigned char session_hash[48];
945#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200947 mbedtls_ssl_session *session = ssl->session_negotiate;
948 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
949 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000952
Jaeden Amero2de07f12019-06-05 13:32:08 +0100953#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
954 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000955 transform->encrypt_then_mac = session->encrypt_then_mac;
956#endif
957 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000958
959 ciphersuite_info = handshake->ciphersuite_info;
960 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100961 if( cipher_info == NULL )
962 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000964 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100966 }
967
Hanno Beckere694c3e2017-12-27 21:34:08 +0000968 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100969 if( md_info == NULL )
970 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200971 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000972 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200973 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100974 }
975
Hanno Beckera0e20d02019-05-15 14:03:01 +0100976#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100977 /* Copy own and peer's CID if the use of the CID
978 * extension has been negotiated. */
979 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
980 {
981 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100982
Hanno Becker05154c32019-05-03 15:23:51 +0100983 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100984 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100985 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100986 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100987
988 transform->out_cid_len = ssl->handshake->peer_cid_len;
989 memcpy( transform->out_cid, ssl->handshake->peer_cid,
990 ssl->handshake->peer_cid_len );
991 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
992 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100993 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100994#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +0100995
Paul Bakker5121ce52009-01-03 21:22:43 +0000996 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000997 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000998 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200999#if defined(MBEDTLS_SSL_PROTO_SSL3)
1000 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001001 {
Paul Bakker48916f92012-09-16 19:57:18 +00001002 handshake->tls_prf = ssl3_prf;
1003 handshake->calc_verify = ssl_calc_verify_ssl;
1004 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001005 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001006 else
1007#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001008#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1009 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001010 {
Paul Bakker48916f92012-09-16 19:57:18 +00001011 handshake->tls_prf = tls1_prf;
1012 handshake->calc_verify = ssl_calc_verify_tls;
1013 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001014 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001015 else
1016#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001017#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1018#if defined(MBEDTLS_SHA512_C)
1019 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001020 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001021 {
Paul Bakker48916f92012-09-16 19:57:18 +00001022 handshake->tls_prf = tls_prf_sha384;
1023 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1024 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001025 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001026 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001027#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001028#if defined(MBEDTLS_SHA256_C)
1029 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001030 {
Paul Bakker48916f92012-09-16 19:57:18 +00001031 handshake->tls_prf = tls_prf_sha256;
1032 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1033 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001034 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001035 else
1036#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001037#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001038 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001039 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1040 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001041 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001042
1043 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001044 * SSLv3:
1045 * master =
1046 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1047 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1048 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001049 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001050 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001051 * master = PRF( premaster, "master secret", randbytes )[0..47]
1052 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001053 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001054 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001055 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1056 }
1057 else
1058 {
1059 /* The label for the KDF used for key expansion.
1060 * This is either "master secret" or "extended master secret"
1061 * depending on whether the Extended Master Secret extension
1062 * is used. */
1063 char const *lbl = "master secret";
1064
1065 /* The salt for the KDF used for key expansion.
1066 * - If the Extended Master Secret extension is not used,
1067 * this is ClientHello.Random + ServerHello.Random
1068 * (see Sect. 8.1 in RFC 5246).
1069 * - If the Extended Master Secret extension is used,
1070 * this is the transcript of the handshake so far.
1071 * (see Sect. 4 in RFC 7627). */
1072 unsigned char const *salt = handshake->randbytes;
1073 size_t salt_len = 64;
1074
1075#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001076 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001077 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001078 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079
Hanno Becker35b23c72018-10-23 12:10:41 +01001080 lbl = "extended master secret";
1081 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001082 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1084 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001086#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001087 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1088 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001089 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001090 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001091 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001092#endif /* MBEDTLS_SHA512_C */
1093 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094 }
1095 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001096#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001097 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001098
Hanno Becker35b23c72018-10-23 12:10:41 +01001099 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001100 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001101#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1102
Hanno Becker7d0a5692018-10-23 15:26:22 +01001103#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1104 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1105 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1106 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1107 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001108 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001109 /* Perform PSK-to-MS expansion in a single step. */
1110 psa_status_t status;
1111 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001112 psa_key_handle_t psk;
Janos Follath8dee8772019-07-30 12:53:32 +01001113 psa_key_derivation_operation_t generator =
1114 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001115
1116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1117
1118 psk = ssl->conf->psk_opaque;
1119 if( ssl->handshake->psk_opaque != 0 )
1120 psk = ssl->handshake->psk_opaque;
1121
Hanno Becker22bf1452019-04-05 11:21:08 +01001122 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001123 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1124 else
1125 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1126
1127 status = psa_key_derivation( &generator, psk, alg,
1128 salt, salt_len,
1129 (unsigned char const *) lbl,
1130 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001131 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001132 if( status != PSA_SUCCESS )
1133 {
1134 psa_generator_abort( &generator );
1135 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1136 }
1137
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001138 status = psa_generator_read( &generator, session->master,
1139 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001140 if( status != PSA_SUCCESS )
1141 {
1142 psa_generator_abort( &generator );
1143 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1144 }
1145
1146 status = psa_generator_abort( &generator );
1147 if( status != PSA_SUCCESS )
1148 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001149 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001150 else
1151#endif
1152 {
1153 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1154 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001155 session->master,
1156 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001157 if( ret != 0 )
1158 {
1159 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1160 return( ret );
1161 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001162
Hanno Becker7d0a5692018-10-23 15:26:22 +01001163 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1164 handshake->premaster,
1165 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001166
Hanno Becker7d0a5692018-10-23 15:26:22 +01001167 mbedtls_platform_zeroize( handshake->premaster,
1168 sizeof(handshake->premaster) );
1169 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001170 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001171
1172 /*
1173 * Swap the client and server random values.
1174 */
Paul Bakker48916f92012-09-16 19:57:18 +00001175 memcpy( tmp, handshake->randbytes, 64 );
1176 memcpy( handshake->randbytes, tmp + 32, 32 );
1177 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001178 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001179
1180 /*
1181 * SSLv3:
1182 * key block =
1183 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1184 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1185 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1186 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1187 * ...
1188 *
1189 * TLSv1:
1190 * key block = PRF( master, "key expansion", randbytes )
1191 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001192 ret = handshake->tls_prf( session->master, 48, "key expansion",
1193 handshake->randbytes, 64, keyblk, 256 );
1194 if( ret != 0 )
1195 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001196 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001197 return( ret );
1198 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001200 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1201 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1202 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1203 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1204 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001205
Paul Bakker5121ce52009-01-03 21:22:43 +00001206 /*
1207 * Determine the appropriate key, IV and MAC length.
1208 */
Paul Bakker68884e32013-01-07 18:20:04 +01001209
Hanno Becker88aaf652017-12-27 08:17:40 +00001210 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001211
Hanno Becker8031d062018-01-03 15:32:31 +00001212#if defined(MBEDTLS_GCM_C) || \
1213 defined(MBEDTLS_CCM_C) || \
1214 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001215 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001216 cipher_info->mode == MBEDTLS_MODE_CCM ||
1217 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001218 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001219 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001220
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001221 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001222 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001223 transform->taglen =
1224 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001225
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001226 /* All modes haves 96-bit IVs;
1227 * GCM and CCM has 4 implicit and 8 explicit bytes
1228 * ChachaPoly has all 12 bytes implicit
1229 */
Paul Bakker68884e32013-01-07 18:20:04 +01001230 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001231 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1232 transform->fixed_ivlen = 12;
1233 else
1234 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001236 /* Minimum length of encrypted record */
1237 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001238 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001239 }
1240 else
Hanno Becker8031d062018-01-03 15:32:31 +00001241#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1242#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1243 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1244 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001245 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001246 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001247 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1248 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001250 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001251 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001252 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001253
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001254 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001255 mac_key_len = mbedtls_md_get_size( md_info );
1256 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001258#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001259 /*
1260 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1261 * (rfc 6066 page 13 or rfc 2104 section 4),
1262 * so we only need to adjust the length here.
1263 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001264 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001266 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001267
1268#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1269 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001270 * HMAC implementation which also truncates the key
1271 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001272 mac_key_len = transform->maclen;
1273#endif
1274 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001275#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001276
1277 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001278 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001279
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001280 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001281 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001282 transform->minlen = transform->maclen;
1283 else
Paul Bakker68884e32013-01-07 18:20:04 +01001284 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001285 /*
1286 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001287 * 1. if EtM is in use: one block plus MAC
1288 * otherwise: * first multiple of blocklen greater than maclen
1289 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001290 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001291#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1292 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001293 {
1294 transform->minlen = transform->maclen
1295 + cipher_info->block_size;
1296 }
1297 else
1298#endif
1299 {
1300 transform->minlen = transform->maclen
1301 + cipher_info->block_size
1302 - transform->maclen % cipher_info->block_size;
1303 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001305#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1306 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1307 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001308 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001309 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001310#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001311#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1312 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1313 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001314 {
1315 transform->minlen += transform->ivlen;
1316 }
1317 else
1318#endif
1319 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001321 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1322 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001323 }
Paul Bakker68884e32013-01-07 18:20:04 +01001324 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001325 }
Hanno Becker8031d062018-01-03 15:32:31 +00001326 else
1327#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1328 {
1329 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1330 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1331 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001332
Hanno Becker88aaf652017-12-27 08:17:40 +00001333 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1334 (unsigned) keylen,
1335 (unsigned) transform->minlen,
1336 (unsigned) transform->ivlen,
1337 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001338
1339 /*
1340 * Finally setup the cipher contexts, IVs and MAC secrets.
1341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001342#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001343 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001344 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001345 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001346 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001347
Paul Bakker68884e32013-01-07 18:20:04 +01001348 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001349 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001351 /*
1352 * This is not used in TLS v1.1.
1353 */
Paul Bakker48916f92012-09-16 19:57:18 +00001354 iv_copy_len = ( transform->fixed_ivlen ) ?
1355 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001356 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1357 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001358 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 }
1360 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361#endif /* MBEDTLS_SSL_CLI_C */
1362#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001363 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001364 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001365 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001366 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001367
Hanno Becker81c7b182017-11-09 18:39:33 +00001368 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001369 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001370
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001371 /*
1372 * This is not used in TLS v1.1.
1373 */
Paul Bakker48916f92012-09-16 19:57:18 +00001374 iv_copy_len = ( transform->fixed_ivlen ) ?
1375 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001376 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1377 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001378 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001379 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001382 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001383 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001384 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1385 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001386 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001387
Hanno Beckerd56ed242018-01-03 15:32:51 +00001388#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389#if defined(MBEDTLS_SSL_PROTO_SSL3)
1390 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001391 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001392 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001393 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001394 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001395 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1396 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001397 }
1398
Hanno Becker81c7b182017-11-09 18:39:33 +00001399 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1400 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001401 }
1402 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1404#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1405 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1406 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001407 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001408 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1409 For AEAD-based ciphersuites, there is nothing to do here. */
1410 if( mac_key_len != 0 )
1411 {
1412 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1413 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1414 }
Paul Bakker68884e32013-01-07 18:20:04 +01001415 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001416 else
1417#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001418 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001420 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1421 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001422 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001423#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1426 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001427 {
1428 int ret = 0;
1429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001431
Hanno Becker88aaf652017-12-27 08:17:40 +00001432 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001433 transform->iv_enc, transform->iv_dec,
1434 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001435 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001436 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001438 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001439 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1440 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001441 }
1442 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001443#else
1444 ((void) mac_dec);
1445 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001446#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001447
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001448#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1449 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001450 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001451 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1452 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001453 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001454 iv_copy_len );
1455 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001456
1457 if( ssl->conf->f_export_keys_ext != NULL )
1458 {
1459 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1460 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001461 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001462 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001463 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001464 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001465 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001466 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001467#endif
1468
Hanno Beckerf704bef2018-11-16 15:21:18 +00001469#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001470
1471 /* Only use PSA-based ciphers for TLS-1.2.
1472 * That's relevant at least for TLS-1.0, where
1473 * we assume that mbedtls_cipher_crypt() updates
1474 * the structure field for the IV, which the PSA-based
1475 * implementation currently doesn't. */
1476#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1477 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001478 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001479 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001480 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001481 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1482 {
1483 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001484 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001485 }
1486
1487 if( ret == 0 )
1488 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001489 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001490 psa_fallthrough = 0;
1491 }
1492 else
1493 {
1494 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1495 psa_fallthrough = 1;
1496 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001497 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 psa_fallthrough = 1;
1500#else
1501 psa_fallthrough = 1;
1502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001503
Hanno Beckercb1cc802018-11-17 22:27:38 +00001504 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001505#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001506 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001507 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001508 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001509 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001510 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001511 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001512
Hanno Beckerf704bef2018-11-16 15:21:18 +00001513#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001514 /* Only use PSA-based ciphers for TLS-1.2.
1515 * That's relevant at least for TLS-1.0, where
1516 * we assume that mbedtls_cipher_crypt() updates
1517 * the structure field for the IV, which the PSA-based
1518 * implementation currently doesn't. */
1519#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1520 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001521 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001522 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001523 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001524 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1525 {
1526 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001527 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001528 }
1529
1530 if( ret == 0 )
1531 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001532 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001533 psa_fallthrough = 0;
1534 }
1535 else
1536 {
1537 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1538 psa_fallthrough = 1;
1539 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001540 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 psa_fallthrough = 1;
1543#else
1544 psa_fallthrough = 1;
1545#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001546
Hanno Beckercb1cc802018-11-17 22:27:38 +00001547 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001548#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001549 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001550 cipher_info ) ) != 0 )
1551 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001552 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001553 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001557 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001559 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001561 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001564 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001565 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001567 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001568 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001569 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001570 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001571
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001572#if defined(MBEDTLS_CIPHER_MODE_CBC)
1573 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001574 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1576 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001577 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001578 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001579 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001580 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001581
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001582 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1583 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001584 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001585 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001586 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001587 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001588 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001589#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001590
Paul Bakker5121ce52009-01-03 21:22:43 +00001591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001592#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001593 // Initialize compression
1594 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001595 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596 {
Paul Bakker16770332013-10-11 09:59:44 +02001597 if( ssl->compress_buf == NULL )
1598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001599 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001600 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001601 if( ssl->compress_buf == NULL )
1602 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001603 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001604 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001605 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1606 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001607 }
1608 }
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001611
Paul Bakker48916f92012-09-16 19:57:18 +00001612 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1613 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001614
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001615 if( deflateInit( &transform->ctx_deflate,
1616 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001617 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001618 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001620 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1621 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001622 }
1623 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001624#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001627end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001628 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1629 mbedtls_platform_zeroize( handshake->randbytes,
1630 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001631 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001632}
1633
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001634#if defined(MBEDTLS_SSL_PROTO_SSL3)
1635void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001636{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001637 mbedtls_md5_context md5;
1638 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001639 unsigned char pad_1[48];
1640 unsigned char pad_2[48];
1641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001643
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001644 mbedtls_md5_init( &md5 );
1645 mbedtls_sha1_init( &sha1 );
1646
1647 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1648 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Paul Bakker380da532012-04-18 16:10:25 +00001650 memset( pad_1, 0x36, 48 );
1651 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001652
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001653 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1654 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1655 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001656
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001657 mbedtls_md5_starts_ret( &md5 );
1658 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1659 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1660 mbedtls_md5_update_ret( &md5, hash, 16 );
1661 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001663 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1664 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1665 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001666
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001667 mbedtls_sha1_starts_ret( &sha1 );
1668 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1669 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1670 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1671 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001673 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1674 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001675
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001676 mbedtls_md5_free( &md5 );
1677 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001678
Paul Bakker380da532012-04-18 16:10:25 +00001679 return;
1680}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001681#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001683#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1684void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001685{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001686 mbedtls_md5_context md5;
1687 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001689 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001690
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001691 mbedtls_md5_init( &md5 );
1692 mbedtls_sha1_init( &sha1 );
1693
1694 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1695 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001696
Andrzej Kurekeb342242019-01-29 09:14:33 -05001697 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001698 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001700 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001702
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001703 mbedtls_md5_free( &md5 );
1704 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001705
Paul Bakker380da532012-04-18 16:10:25 +00001706 return;
1707}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001708#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001709
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001710#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1711#if defined(MBEDTLS_SHA256_C)
1712void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001713{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001714#if defined(MBEDTLS_USE_PSA_CRYPTO)
1715 size_t hash_size;
1716 psa_status_t status;
1717 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1718
1719 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1720 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1721 if( status != PSA_SUCCESS )
1722 {
1723 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1724 return;
1725 }
1726
1727 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1728 if( status != PSA_SUCCESS )
1729 {
1730 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1731 return;
1732 }
1733 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1734 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1735#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001736 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001737
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001738 mbedtls_sha256_init( &sha256 );
1739
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001741
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001742 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001743 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001744
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001745 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001747
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001748 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001749#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001750 return;
1751}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001752#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001754#if defined(MBEDTLS_SHA512_C)
1755void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001756{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001757#if defined(MBEDTLS_USE_PSA_CRYPTO)
1758 size_t hash_size;
1759 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001760 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001761
1762 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001763 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001764 if( status != PSA_SUCCESS )
1765 {
1766 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1767 return;
1768 }
1769
Andrzej Kurek972fba52019-01-30 03:29:12 -05001770 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001771 if( status != PSA_SUCCESS )
1772 {
1773 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1774 return;
1775 }
1776 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1777 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1778#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001779 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001780
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001781 mbedtls_sha512_init( &sha512 );
1782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001784
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001785 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001786 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1789 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001790
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001791 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001792#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001793 return;
1794}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001795#endif /* MBEDTLS_SHA512_C */
1796#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001797
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1799int 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 +02001800{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001801 unsigned char *p = ssl->handshake->premaster;
1802 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001803 const unsigned char *psk = ssl->conf->psk;
1804 size_t psk_len = ssl->conf->psk_len;
1805
1806 /* If the psk callback was called, use its result */
1807 if( ssl->handshake->psk != NULL )
1808 {
1809 psk = ssl->handshake->psk;
1810 psk_len = ssl->handshake->psk_len;
1811 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001812
1813 /*
1814 * PMS = struct {
1815 * opaque other_secret<0..2^16-1>;
1816 * opaque psk<0..2^16-1>;
1817 * };
1818 * with "other_secret" depending on the particular key exchange
1819 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001820#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1821 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001822 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001823 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001825
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001826 *(p++) = (unsigned char)( psk_len >> 8 );
1827 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001828
1829 if( end < p || (size_t)( end - p ) < psk_len )
1830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1831
1832 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001833 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001834 }
1835 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001836#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1837#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1838 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001839 {
1840 /*
1841 * other_secret already set by the ClientKeyExchange message,
1842 * and is 48 bytes long
1843 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001844 if( end - p < 2 )
1845 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1846
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001847 *p++ = 0;
1848 *p++ = 48;
1849 p += 48;
1850 }
1851 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1853#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1854 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001855 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001856 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001857 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001858
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001859 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001860 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001861 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001862 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001863 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001864 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001865 return( ret );
1866 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001867 *(p++) = (unsigned char)( len >> 8 );
1868 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001869 p += len;
1870
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001871 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872 }
1873 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001874#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1875#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1876 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001877 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001878 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 size_t zlen;
1880
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001881 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001882 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001883 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001885 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001886 return( ret );
1887 }
1888
1889 *(p++) = (unsigned char)( zlen >> 8 );
1890 *(p++) = (unsigned char)( zlen );
1891 p += zlen;
1892
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001893 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1894 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001895 }
1896 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001897#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1900 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001901 }
1902
1903 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001904 if( end - p < 2 )
1905 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001906
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001907 *(p++) = (unsigned char)( psk_len >> 8 );
1908 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001909
1910 if( end < p || (size_t)( end - p ) < psk_len )
1911 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1912
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001913 memcpy( p, psk, psk_len );
1914 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001915
1916 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1917
1918 return( 0 );
1919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001920#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001921
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001922#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001923/*
1924 * SSLv3.0 MAC functions
1925 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001926#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001927static void ssl_mac( mbedtls_md_context_t *md_ctx,
1928 const unsigned char *secret,
1929 const unsigned char *buf, size_t len,
1930 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001931 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001932{
1933 unsigned char header[11];
1934 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001935 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001936 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1937 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001938
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001939 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001940 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001941 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001942 else
Paul Bakker68884e32013-01-07 18:20:04 +01001943 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001944
1945 memcpy( header, ctr, 8 );
1946 header[ 8] = (unsigned char) type;
1947 header[ 9] = (unsigned char)( len >> 8 );
1948 header[10] = (unsigned char)( len );
1949
Paul Bakker68884e32013-01-07 18:20:04 +01001950 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001951 mbedtls_md_starts( md_ctx );
1952 mbedtls_md_update( md_ctx, secret, md_size );
1953 mbedtls_md_update( md_ctx, padding, padlen );
1954 mbedtls_md_update( md_ctx, header, 11 );
1955 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001956 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001957
Paul Bakker68884e32013-01-07 18:20:04 +01001958 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001959 mbedtls_md_starts( md_ctx );
1960 mbedtls_md_update( md_ctx, secret, md_size );
1961 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001962 mbedtls_md_update( md_ctx, out, md_size );
1963 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001964}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001965#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001966
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001967/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001968 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001969#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001970 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1971 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1972 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1973/* This function makes sure every byte in the memory region is accessed
1974 * (in ascending addresses order) */
1975static void ssl_read_memory( unsigned char *p, size_t len )
1976{
1977 unsigned char acc = 0;
1978 volatile unsigned char force;
1979
1980 for( ; len != 0; p++, len-- )
1981 acc ^= *p;
1982
1983 force = acc;
1984 (void) force;
1985}
1986#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1987
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001988/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001989 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001990 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001991
Hanno Beckera0e20d02019-05-15 14:03:01 +01001992#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001993/* This functions transforms a DTLS plaintext fragment and a record content
1994 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001995 *
1996 * struct {
1997 * opaque content[DTLSPlaintext.length];
1998 * ContentType real_type;
1999 * uint8 zeros[length_of_padding];
2000 * } DTLSInnerPlaintext;
2001 *
2002 * Input:
2003 * - `content`: The beginning of the buffer holding the
2004 * plaintext to be wrapped.
2005 * - `*content_size`: The length of the plaintext in Bytes.
2006 * - `max_len`: The number of Bytes available starting from
2007 * `content`. This must be `>= *content_size`.
2008 * - `rec_type`: The desired record content type.
2009 *
2010 * Output:
2011 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2012 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2013 *
2014 * Returns:
2015 * - `0` on success.
2016 * - A negative error code if `max_len` didn't offer enough space
2017 * for the expansion.
2018 */
2019static int ssl_cid_build_inner_plaintext( unsigned char *content,
2020 size_t *content_size,
2021 size_t remaining,
2022 uint8_t rec_type )
2023{
2024 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002025 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2026 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2027 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002028
2029 /* Write real content type */
2030 if( remaining == 0 )
2031 return( -1 );
2032 content[ len ] = rec_type;
2033 len++;
2034 remaining--;
2035
2036 if( remaining < pad )
2037 return( -1 );
2038 memset( content + len, 0, pad );
2039 len += pad;
2040 remaining -= pad;
2041
2042 *content_size = len;
2043 return( 0 );
2044}
2045
Hanno Becker07dc97d2019-05-20 15:08:01 +01002046/* This function parses a DTLSInnerPlaintext structure.
2047 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002048static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2049 size_t *content_size,
2050 uint8_t *rec_type )
2051{
2052 size_t remaining = *content_size;
2053
2054 /* Determine length of padding by skipping zeroes from the back. */
2055 do
2056 {
2057 if( remaining == 0 )
2058 return( -1 );
2059 remaining--;
2060 } while( content[ remaining ] == 0 );
2061
2062 *content_size = remaining;
2063 *rec_type = content[ remaining ];
2064
2065 return( 0 );
2066}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002067#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002068
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002069/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002070 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002071static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002072 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002073 mbedtls_record *rec )
2074{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002075 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002076 *
2077 * additional_data = seq_num + TLSCompressed.type +
2078 * TLSCompressed.version + TLSCompressed.length;
2079 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002080 * For the CID extension, this is extended as follows
2081 * (quoting draft-ietf-tls-dtls-connection-id-05,
2082 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002083 *
2084 * additional_data = seq_num + DTLSPlaintext.type +
2085 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002086 * cid +
2087 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002088 * length_of_DTLSInnerPlaintext;
2089 */
2090
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002091 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2092 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002093 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002094
Hanno Beckera0e20d02019-05-15 14:03:01 +01002095#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002096 if( rec->cid_len != 0 )
2097 {
2098 memcpy( add_data + 11, rec->cid, rec->cid_len );
2099 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2100 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2101 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2102 *add_data_len = 13 + 1 + rec->cid_len;
2103 }
2104 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002105#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002106 {
2107 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2108 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2109 *add_data_len = 13;
2110 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002111}
2112
Hanno Beckera18d1322018-01-03 14:27:32 +00002113int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2114 mbedtls_ssl_transform *transform,
2115 mbedtls_record *rec,
2116 int (*f_rng)(void *, unsigned char *, size_t),
2117 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002118{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002119 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002120 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002121 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002122 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002123 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002124 size_t post_avail;
2125
2126 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002127#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002128 ((void) ssl);
2129#endif
2130
2131 /* The PRNG is used for dynamic IV generation that's used
2132 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2133#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2134 ( defined(MBEDTLS_AES_C) || \
2135 defined(MBEDTLS_ARIA_C) || \
2136 defined(MBEDTLS_CAMELLIA_C) ) && \
2137 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2138 ((void) f_rng);
2139 ((void) p_rng);
2140#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002143
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002144 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002145 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2147 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2148 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002149 if( rec == NULL
2150 || rec->buf == NULL
2151 || rec->buf_len < rec->data_offset
2152 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002153#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002154 || rec->cid_len != 0
2155#endif
2156 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002157 {
2158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002160 }
2161
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002162 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002163 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002164 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002165 data, rec->data_len );
2166
2167 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2168
2169 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2170 {
2171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2172 (unsigned) rec->data_len,
2173 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2174 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2175 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002176
Hanno Beckera0e20d02019-05-15 14:03:01 +01002177#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002178 /*
2179 * Add CID information
2180 */
2181 rec->cid_len = transform->out_cid_len;
2182 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2183 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002184
2185 if( rec->cid_len != 0 )
2186 {
2187 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002188 * Wrap plaintext into DTLSInnerPlaintext structure.
2189 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002190 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002191 * Note that this changes `rec->data_len`, and hence
2192 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002193 */
2194 if( ssl_cid_build_inner_plaintext( data,
2195 &rec->data_len,
2196 post_avail,
2197 rec->type ) != 0 )
2198 {
2199 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2200 }
2201
2202 rec->type = MBEDTLS_SSL_MSG_CID;
2203 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002204#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002205
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002206 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2207
Paul Bakker5121ce52009-01-03 21:22:43 +00002208 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002209 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002210 */
Hanno Becker52344c22018-01-03 15:24:20 +00002211#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212 if( mode == MBEDTLS_MODE_STREAM ||
2213 ( mode == MBEDTLS_MODE_CBC
2214#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002215 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002216#endif
2217 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002218 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002219 if( post_avail < transform->maclen )
2220 {
2221 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2222 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2223 }
2224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002225#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002226 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002228 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002229 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2230 data, rec->data_len, rec->ctr, rec->type, mac );
2231 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002232 }
2233 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002234#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002235#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2236 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002237 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002238 {
Hanno Becker992b6872017-11-09 18:57:39 +00002239 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2240
Hanno Beckercab87e62019-04-29 13:52:53 +01002241 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002242
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002243 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002244 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002245 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2246 data, rec->data_len );
2247 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2248 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2249
2250 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002251 }
2252 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002253#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002254 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002255 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2256 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002257 }
2258
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002259 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2260 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002261
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 rec->data_len += transform->maclen;
2263 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002264 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002265 }
Hanno Becker52344c22018-01-03 15:24:20 +00002266#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002267
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002268 /*
2269 * Encrypt
2270 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2272 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002273 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002274 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002275 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002276 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002277 "including %d bytes of padding",
2278 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002279
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002280 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2281 transform->iv_enc, transform->ivlen,
2282 data, rec->data_len,
2283 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002284 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002285 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002286 return( ret );
2287 }
2288
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002289 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002290 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2292 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002293 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002294 }
Paul Bakker68884e32013-01-07 18:20:04 +01002295 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002297
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002298#if defined(MBEDTLS_GCM_C) || \
2299 defined(MBEDTLS_CCM_C) || \
2300 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002301 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002302 mode == MBEDTLS_MODE_CCM ||
2303 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002304 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002305 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002306 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002307 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002308
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002309 /* Check that there's space for both the authentication tag
2310 * and the explicit IV before and after the record content. */
2311 if( post_avail < transform->taglen ||
2312 rec->data_offset < explicit_iv_len )
2313 {
2314 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2315 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2316 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002317
Paul Bakker68884e32013-01-07 18:20:04 +01002318 /*
2319 * Generate IV
2320 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002321 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2322 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002323 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002324 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002325 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2326 explicit_iv_len );
2327 /* Prefix record content with explicit IV. */
2328 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002329 }
2330 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2331 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002332 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002333 unsigned char i;
2334
2335 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2336
2337 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002338 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002339 }
2340 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002341 {
2342 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002343 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2344 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002345 }
2346
Hanno Beckercab87e62019-04-29 13:52:53 +01002347 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002348
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002349 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2350 iv, transform->ivlen );
2351 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 data - explicit_iv_len, explicit_iv_len );
2353 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002354 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002355 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002356 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002358
Paul Bakker68884e32013-01-07 18:20:04 +01002359 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002360 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002361 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002362
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002363 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002364 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002365 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002366 data, rec->data_len, /* source */
2367 data, &rec->data_len, /* destination */
2368 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002369 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002371 return( ret );
2372 }
2373
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002374 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2375 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002376
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002377 rec->data_len += transform->taglen + explicit_iv_len;
2378 rec->data_offset -= explicit_iv_len;
2379 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002380 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002383#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2384#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002385 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002386 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002387 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002388 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002389 size_t padlen, i;
2390 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002391
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002392 /* Currently we're always using minimal padding
2393 * (up to 255 bytes would be allowed). */
2394 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2395 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002396 padlen = 0;
2397
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002398 /* Check there's enough space in the buffer for the padding. */
2399 if( post_avail < padlen + 1 )
2400 {
2401 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2402 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2403 }
2404
Paul Bakker5121ce52009-01-03 21:22:43 +00002405 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002406 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002407
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002408 rec->data_len += padlen + 1;
2409 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002411#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002412 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002413 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2414 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002415 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002416 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002418 if( f_rng == NULL )
2419 {
2420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2421 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2422 }
2423
2424 if( rec->data_offset < transform->ivlen )
2425 {
2426 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2427 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2428 }
2429
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002430 /*
2431 * Generate IV
2432 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002433 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002434 if( ret != 0 )
2435 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002436
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002437 memcpy( data - transform->ivlen, transform->iv_enc,
2438 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002439
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002440 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002443 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002444 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002446 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002447
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002448 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2449 transform->iv_enc,
2450 transform->ivlen,
2451 data, rec->data_len,
2452 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002454 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002455 return( ret );
2456 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002457
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002458 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002459 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002460 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2461 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002462 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002464#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002466 {
2467 /*
2468 * Save IV in SSL3 and TLS1
2469 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2471 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002472 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002473 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002474#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002475 {
2476 data -= transform->ivlen;
2477 rec->data_offset -= transform->ivlen;
2478 rec->data_len += transform->ivlen;
2479 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002481#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002482 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002483 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002484 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2485
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 /*
2487 * MAC(MAC_write_key, seq_num +
2488 * TLSCipherText.type +
2489 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002490 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002491 * IV + // except for TLS 1.0
2492 * ENC(content + padding + padding_length));
2493 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494
2495 if( post_avail < transform->maclen)
2496 {
2497 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2498 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2499 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500
Hanno Beckercab87e62019-04-29 13:52:53 +01002501 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002502
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002503 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002504 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002505 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002506
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002507 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002508 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2510 data, rec->data_len );
2511 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2512 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002513
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002514 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002515
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002516 rec->data_len += transform->maclen;
2517 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002518 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002519 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002521 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002522 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002523#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002524 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002526 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2527 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002528 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002529
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002530 /* Make extra sure authentication was performed, exactly once */
2531 if( auth_done != 1 )
2532 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2534 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002535 }
2536
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002537 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002538
2539 return( 0 );
2540}
2541
Hanno Beckera18d1322018-01-03 14:27:32 +00002542int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2543 mbedtls_ssl_transform *transform,
2544 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002545{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002546 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002548 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002549#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002550 size_t padlen = 0, correct = 1;
2551#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002552 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002553 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002554 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002555
Hanno Beckera18d1322018-01-03 14:27:32 +00002556#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002557 ((void) ssl);
2558#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002560 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002561 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002562 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2564 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2565 }
2566 if( rec == NULL ||
2567 rec->buf == NULL ||
2568 rec->buf_len < rec->data_offset ||
2569 rec->buf_len - rec->data_offset < rec->data_len )
2570 {
2571 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002572 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002573 }
2574
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002575 data = rec->buf + rec->data_offset;
2576 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002577
Hanno Beckera0e20d02019-05-15 14:03:01 +01002578#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002579 /*
2580 * Match record's CID with incoming CID.
2581 */
Hanno Becker938489a2019-05-08 13:02:22 +01002582 if( rec->cid_len != transform->in_cid_len ||
2583 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2584 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002585 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002586 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002587#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002589#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2590 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002591 {
2592 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002593 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2594 transform->iv_dec,
2595 transform->ivlen,
2596 data, rec->data_len,
2597 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002600 return( ret );
2601 }
2602
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002603 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002604 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2606 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002607 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002608 }
Paul Bakker68884e32013-01-07 18:20:04 +01002609 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002610#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002611#if defined(MBEDTLS_GCM_C) || \
2612 defined(MBEDTLS_CCM_C) || \
2613 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002614 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 mode == MBEDTLS_MODE_CCM ||
2616 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002617 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002618 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002619 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002620
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002621 /*
2622 * Compute and update sizes
2623 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002624 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002625 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002626 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002627 "+ taglen (%d)", rec->data_len,
2628 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002629 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002630 }
Paul Bakker68884e32013-01-07 18:20:04 +01002631
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 /*
2633 * Prepare IV
2634 */
2635 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2636 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002637 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002638 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002639 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002640
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002641 }
2642 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2643 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002644 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002645 unsigned char i;
2646
2647 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2648
2649 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002650 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002651 }
2652 else
2653 {
2654 /* Reminder if we ever add an AEAD mode with a different size */
2655 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2656 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2657 }
2658
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 data += explicit_iv_len;
2660 rec->data_offset += explicit_iv_len;
2661 rec->data_len -= explicit_iv_len + transform->taglen;
2662
Hanno Beckercab87e62019-04-29 13:52:53 +01002663 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002664 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002665 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666
2667 memcpy( transform->iv_dec + transform->fixed_ivlen,
2668 data - explicit_iv_len, explicit_iv_len );
2669
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002670 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002671 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002672 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002673
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002674
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002675 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002676 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002677 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002678 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2679 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002680 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002681 data, rec->data_len,
2682 data, &olen,
2683 data + rec->data_len,
2684 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002685 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002686 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002688 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2689 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002690
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002691 return( ret );
2692 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002693 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002694
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002695 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002696 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2698 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002699 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002700 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002702#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2703#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002704 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002705 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002707 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002708
Paul Bakker5121ce52009-01-03 21:22:43 +00002709 /*
Paul Bakker45829992013-01-03 14:52:21 +01002710 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002711 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002712#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002713 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2714 {
2715 /* The ciphertext is prefixed with the CBC IV. */
2716 minlen += transform->ivlen;
2717 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002718#endif
Paul Bakker45829992013-01-03 14:52:21 +01002719
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002720 /* Size considerations:
2721 *
2722 * - The CBC cipher text must not be empty and hence
2723 * at least of size transform->ivlen.
2724 *
2725 * Together with the potential IV-prefix, this explains
2726 * the first of the two checks below.
2727 *
2728 * - The record must contain a MAC, either in plain or
2729 * encrypted, depending on whether Encrypt-then-MAC
2730 * is used or not.
2731 * - If it is, the message contains the IV-prefix,
2732 * the CBC ciphertext, and the MAC.
2733 * - If it is not, the padded plaintext, and hence
2734 * the CBC ciphertext, has at least length maclen + 1
2735 * because there is at least the padding length byte.
2736 *
2737 * As the CBC ciphertext is not empty, both cases give the
2738 * lower bound minlen + maclen + 1 on the record size, which
2739 * we test for in the second check below.
2740 */
2741 if( rec->data_len < minlen + transform->ivlen ||
2742 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002743 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002744 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002745 "+ 1 ) ( + expl IV )", rec->data_len,
2746 transform->ivlen,
2747 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002748 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002749 }
2750
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002751 /*
2752 * Authenticate before decrypt if enabled
2753 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002755 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002756 {
Hanno Becker992b6872017-11-09 18:57:39 +00002757 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002759 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002760
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002761 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2762 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002763
Hanno Beckercab87e62019-04-29 13:52:53 +01002764 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002765
Hanno Beckercab87e62019-04-29 13:52:53 +01002766 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2767 add_data_len );
2768 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2769 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2771 data, rec->data_len );
2772 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2773 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002774
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2776 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002777 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002778 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002779
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002780 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2781 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002784 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002785 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002786 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002787 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002788#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002789
2790 /*
2791 * Check length sanity
2792 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002793 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002794 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002796 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002797 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002798 }
2799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002800#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002801 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002802 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002803 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002804 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 /* This is safe because data_len >= minlen + maclen + 1 initially,
2807 * and at this point we have at most subtracted maclen (note that
2808 * minlen == transform->ivlen here). */
2809 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002810
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002811 data += transform->ivlen;
2812 rec->data_offset += transform->ivlen;
2813 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002814 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002815#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002816
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002817 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2818 transform->iv_dec, transform->ivlen,
2819 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002820 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002821 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002822 return( ret );
2823 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002824
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002825 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2828 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002829 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002830
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002831#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002833 {
2834 /*
2835 * Save IV in SSL3 and TLS1
2836 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002837 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2838 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002839 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002840#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002841
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002842 /* Safe since data_len >= minlen + maclen + 1, so after having
2843 * subtracted at most minlen and maclen up to this point,
2844 * data_len > 0. */
2845 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002846
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002847 if( auth_done == 1 )
2848 {
2849 correct *= ( rec->data_len >= padlen + 1 );
2850 padlen *= ( rec->data_len >= padlen + 1 );
2851 }
2852 else
Paul Bakker45829992013-01-03 14:52:21 +01002853 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002854#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002855 if( rec->data_len < transform->maclen + padlen + 1 )
2856 {
2857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2858 rec->data_len,
2859 transform->maclen,
2860 padlen + 1 ) );
2861 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002862#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863
2864 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2865 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002866 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002867
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002868 padlen++;
2869
2870 /* Regardless of the validity of the padding,
2871 * we have data_len >= padlen here. */
2872
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002874 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002875 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002876 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002877 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002878#if defined(MBEDTLS_SSL_DEBUG_ALL)
2879 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002880 "should be no more than %d",
2881 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002882#endif
Paul Bakker45829992013-01-03 14:52:21 +01002883 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002884 }
2885 }
2886 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002887#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2888#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2889 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002890 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002891 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002892 /* The padding check involves a series of up to 256
2893 * consecutive memory reads at the end of the record
2894 * plaintext buffer. In order to hide the length and
2895 * validity of the padding, always perform exactly
2896 * `min(256,plaintext_len)` reads (but take into account
2897 * only the last `padlen` bytes for the padding check). */
2898 size_t pad_count = 0;
2899 size_t real_count = 0;
2900 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002901
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002902 /* Index of first padding byte; it has been ensured above
2903 * that the subtraction is safe. */
2904 size_t const padding_idx = rec->data_len - padlen;
2905 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2906 size_t const start_idx = rec->data_len - num_checks;
2907 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002908
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002910 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002911 real_count |= ( idx >= padding_idx );
2912 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002913 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002914 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002915
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002917 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002919#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002920 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002921 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002922 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002923#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2924 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002925 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002926 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2927 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002928 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002929
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002930 /* If the padding was found to be invalid, padlen == 0
2931 * and the subtraction is safe. If the padding was found valid,
2932 * padlen hasn't been changed and the previous assertion
2933 * data_len >= padlen still holds. */
2934 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002935 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002936 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002937#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002938 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2941 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002942 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002944#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002945 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002946 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002947#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002948
2949 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002950 * Authenticate if not done yet.
2951 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002952 */
Hanno Becker52344c22018-01-03 15:24:20 +00002953#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002954 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002955 {
Hanno Becker992b6872017-11-09 18:57:39 +00002956 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002957
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002958 /* If the initial value of padlen was such that
2959 * data_len < maclen + padlen + 1, then padlen
2960 * got reset to 1, and the initial check
2961 * data_len >= minlen + maclen + 1
2962 * guarantees that at this point we still
2963 * have at least data_len >= maclen.
2964 *
2965 * If the initial value of padlen was such that
2966 * data_len >= maclen + padlen + 1, then we have
2967 * subtracted either padlen + 1 (if the padding was correct)
2968 * or 0 (if the padding was incorrect) since then,
2969 * hence data_len >= maclen in any case.
2970 */
2971 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002972
Hanno Beckercab87e62019-04-29 13:52:53 +01002973 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002975#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002976 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002977 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002978 ssl_mac( &transform->md_ctx_dec,
2979 transform->mac_dec,
2980 data, rec->data_len,
2981 rec->ctr, rec->type,
2982 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002983 }
2984 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2986#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2987 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002988 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002989 {
2990 /*
2991 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002992 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002993 *
2994 * Known timing attacks:
2995 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2996 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002997 * To compensate for different timings for the MAC calculation
2998 * depending on how much padding was removed (which is determined
2999 * by padlen), process extra_run more blocks through the hash
3000 * function.
3001 *
3002 * The formula in the paper is
3003 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
3004 * where L1 is the size of the header plus the decrypted message
3005 * plus CBC padding and L2 is the size of the header plus the
3006 * decrypted message. This is for an underlying hash function
3007 * with 64-byte blocks.
3008 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3009 * correctly. We round down instead of up, so -56 is the correct
3010 * value for our calculations instead of -55.
3011 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003012 * Repeat the formula rather than defining a block_size variable.
3013 * This avoids requiring division by a variable at runtime
3014 * (which would be marginally less efficient and would require
3015 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003016 */
3017 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003018 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003019
3020 /*
3021 * The next two sizes are the minimum and maximum values of
3022 * in_msglen over all padlen values.
3023 *
3024 * They're independent of padlen, since we previously did
3025 * in_msglen -= padlen.
3026 *
3027 * Note that max_len + maclen is never more than the buffer
3028 * length, as we previously did in_msglen -= maclen too.
3029 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003030 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003031 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3032
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003033 memset( tmp, 0, sizeof( tmp ) );
3034
3035 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003036 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003037#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3038 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003039 case MBEDTLS_MD_MD5:
3040 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003041 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003042 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003043 extra_run =
3044 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3045 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003046 break;
3047#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003048#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003049 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003050 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003051 extra_run =
3052 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3053 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003054 break;
3055#endif
3056 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003058 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3059 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003060
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003061 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003062
Hanno Beckercab87e62019-04-29 13:52:53 +01003063 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3064 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3066 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003067 /* Make sure we access everything even when padlen > 0. This
3068 * makes the synchronisation requirements for just-in-time
3069 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003070 ssl_read_memory( data + rec->data_len, padlen );
3071 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003072
3073 /* Call mbedtls_md_process at least once due to cache attacks
3074 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003075 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003076 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003077
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003078 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003079
3080 /* Make sure we access all the memory that could contain the MAC,
3081 * before we check it in the next code block. This makes the
3082 * synchronisation requirements for just-in-time Prime+Probe
3083 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003084 ssl_read_memory( data + min_len,
3085 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003086 }
3087 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003088#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3089 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3092 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003093 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003095#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003096 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3097 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003098#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003099
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003100 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3101 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003102 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#if defined(MBEDTLS_SSL_DEBUG_ALL)
3104 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003105#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003106 correct = 0;
3107 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003108 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003109 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003110
3111 /*
3112 * Finally check the correct flag
3113 */
3114 if( correct == 0 )
3115 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003116#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003117
3118 /* Make extra sure authentication was performed, exactly once */
3119 if( auth_done != 1 )
3120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3122 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003123 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003124
Hanno Beckera0e20d02019-05-15 14:03:01 +01003125#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003126 if( rec->cid_len != 0 )
3127 {
3128 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3129 &rec->type );
3130 if( ret != 0 )
3131 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3132 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003133#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003134
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003136
3137 return( 0 );
3138}
3139
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003140#undef MAC_NONE
3141#undef MAC_PLAINTEXT
3142#undef MAC_CIPHERTEXT
3143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003145/*
3146 * Compression/decompression functions
3147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003149{
3150 int ret;
3151 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003152 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003153 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003154 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003156 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003157
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003158 if( len_pre == 0 )
3159 return( 0 );
3160
Paul Bakker2770fbd2012-07-03 13:30:23 +00003161 memcpy( msg_pre, ssl->out_msg, len_pre );
3162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003163 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003164 ssl->out_msglen ) );
3165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003166 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003167 ssl->out_msg, ssl->out_msglen );
3168
Paul Bakker48916f92012-09-16 19:57:18 +00003169 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3170 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3171 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003172 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003173
Paul Bakker48916f92012-09-16 19:57:18 +00003174 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003175 if( ret != Z_OK )
3176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3178 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003179 }
3180
Angus Grattond8213d02016-05-25 20:56:48 +10003181 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003182 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003184 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003185 ssl->out_msglen ) );
3186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003187 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003188 ssl->out_msg, ssl->out_msglen );
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191
3192 return( 0 );
3193}
3194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003195static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003196{
3197 int ret;
3198 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003199 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003200 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003201 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003204
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003205 if( len_pre == 0 )
3206 return( 0 );
3207
Paul Bakker2770fbd2012-07-03 13:30:23 +00003208 memcpy( msg_pre, ssl->in_msg, len_pre );
3209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003210 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003211 ssl->in_msglen ) );
3212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003213 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003214 ssl->in_msg, ssl->in_msglen );
3215
Paul Bakker48916f92012-09-16 19:57:18 +00003216 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3217 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3218 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003219 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003220 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003221
Paul Bakker48916f92012-09-16 19:57:18 +00003222 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003223 if( ret != Z_OK )
3224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003225 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3226 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003227 }
3228
Angus Grattond8213d02016-05-25 20:56:48 +10003229 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003230 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003232 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003233 ssl->in_msglen ) );
3234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003235 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003236 ssl->in_msg, ssl->in_msglen );
3237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003238 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003239
3240 return( 0 );
3241}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003244#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3245static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003247#if defined(MBEDTLS_SSL_PROTO_DTLS)
3248static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003249{
3250 /* If renegotiation is not enforced, retransmit until we would reach max
3251 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003252 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003253 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003254 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003255 unsigned char doublings = 1;
3256
3257 while( ratio != 0 )
3258 {
3259 ++doublings;
3260 ratio >>= 1;
3261 }
3262
3263 if( ++ssl->renego_records_seen > doublings )
3264 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003266 return( 0 );
3267 }
3268 }
3269
3270 return( ssl_write_hello_request( ssl ) );
3271}
3272#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003274
Paul Bakker5121ce52009-01-03 21:22:43 +00003275/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003276 * Fill the input message buffer by appending data to it.
3277 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003278 *
3279 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3280 * available (from this read and/or a previous one). Otherwise, an error code
3281 * is returned (possibly EOF or WANT_READ).
3282 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003283 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3284 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3285 * since we always read a whole datagram at once.
3286 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003287 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003288 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003289 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003291{
Paul Bakker23986e52011-04-24 08:57:21 +00003292 int ret;
3293 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003295 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003296
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003297 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003299 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003300 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003302 }
3303
Angus Grattond8213d02016-05-25 20:56:48 +10003304 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003305 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003306 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3307 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003308 }
3309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003310#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003311 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003312 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003313 uint32_t timeout;
3314
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003315 /* Just to be sure */
3316 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3317 {
3318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3319 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3320 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3321 }
3322
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003323 /*
3324 * The point is, we need to always read a full datagram at once, so we
3325 * sometimes read more then requested, and handle the additional data.
3326 * It could be the rest of the current record (while fetching the
3327 * header) and/or some other records in the same datagram.
3328 */
3329
3330 /*
3331 * Move to the next record in the already read datagram if applicable
3332 */
3333 if( ssl->next_record_offset != 0 )
3334 {
3335 if( ssl->in_left < ssl->next_record_offset )
3336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003337 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3338 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003339 }
3340
3341 ssl->in_left -= ssl->next_record_offset;
3342
3343 if( ssl->in_left != 0 )
3344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003346 ssl->next_record_offset ) );
3347 memmove( ssl->in_hdr,
3348 ssl->in_hdr + ssl->next_record_offset,
3349 ssl->in_left );
3350 }
3351
3352 ssl->next_record_offset = 0;
3353 }
3354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003355 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003356 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003357
3358 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003359 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003360 */
3361 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003362 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003364 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003365 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003366
3367 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003368 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003369 * are not at the beginning of a new record, the caller did something
3370 * wrong.
3371 */
3372 if( ssl->in_left != 0 )
3373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3375 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003376 }
3377
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003378 /*
3379 * Don't even try to read if time's out already.
3380 * This avoids by-passing the timer when repeatedly receiving messages
3381 * that will end up being dropped.
3382 */
3383 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003384 {
3385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003386 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003387 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003388 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003389 {
Angus Grattond8213d02016-05-25 20:56:48 +10003390 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003393 timeout = ssl->handshake->retransmit_timeout;
3394 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003395 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003397 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003398
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003399 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003400 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3401 timeout );
3402 else
3403 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3404
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003405 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003406
3407 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003409 }
3410
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003411 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003412 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003413 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003414 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003416 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003417 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003418 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3419 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003420 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003421 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003422 }
3423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003425 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003427 return( ret );
3428 }
3429
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003430 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003431 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003432#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003433 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003434 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003435 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003436 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003437 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003438 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003439 return( ret );
3440 }
3441
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003442 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003443 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003444#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003445 }
3446
Paul Bakker5121ce52009-01-03 21:22:43 +00003447 if( ret < 0 )
3448 return( ret );
3449
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003450 ssl->in_left = ret;
3451 }
3452 else
3453#endif
3454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003456 ssl->in_left, nb_want ) );
3457
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003458 while( ssl->in_left < nb_want )
3459 {
3460 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003461
3462 if( ssl_check_timer( ssl ) != 0 )
3463 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3464 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003465 {
3466 if( ssl->f_recv_timeout != NULL )
3467 {
3468 ret = ssl->f_recv_timeout( ssl->p_bio,
3469 ssl->in_hdr + ssl->in_left, len,
3470 ssl->conf->read_timeout );
3471 }
3472 else
3473 {
3474 ret = ssl->f_recv( ssl->p_bio,
3475 ssl->in_hdr + ssl->in_left, len );
3476 }
3477 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003478
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003480 ssl->in_left, nb_want ) );
3481 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003482
3483 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003484 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003485
3486 if( ret < 0 )
3487 return( ret );
3488
mohammad160352aecb92018-03-28 23:41:40 -07003489 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003490 {
Darryl Green11999bb2018-03-13 15:22:58 +00003491 MBEDTLS_SSL_DEBUG_MSG( 1,
3492 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003493 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003494 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3495 }
3496
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003497 ssl->in_left += ret;
3498 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003499 }
3500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003501 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003502
3503 return( 0 );
3504}
3505
3506/*
3507 * Flush any data not yet written
3508 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003510{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003511 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003512 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003513
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003514 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003515
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003516 if( ssl->f_send == NULL )
3517 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003519 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003520 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003521 }
3522
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003523 /* Avoid incrementing counter if data is flushed */
3524 if( ssl->out_left == 0 )
3525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003526 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003527 return( 0 );
3528 }
3529
Paul Bakker5121ce52009-01-03 21:22:43 +00003530 while( ssl->out_left > 0 )
3531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003533 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
Hanno Becker2b1e3542018-08-06 11:19:13 +01003535 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003536 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003537
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003538 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003539
3540 if( ret <= 0 )
3541 return( ret );
3542
mohammad160352aecb92018-03-28 23:41:40 -07003543 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003544 {
Darryl Green11999bb2018-03-13 15:22:58 +00003545 MBEDTLS_SSL_DEBUG_MSG( 1,
3546 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003547 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003548 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3549 }
3550
Paul Bakker5121ce52009-01-03 21:22:43 +00003551 ssl->out_left -= ret;
3552 }
3553
Hanno Becker2b1e3542018-08-06 11:19:13 +01003554#if defined(MBEDTLS_SSL_PROTO_DTLS)
3555 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003556 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003557 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003558 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003559 else
3560#endif
3561 {
3562 ssl->out_hdr = ssl->out_buf + 8;
3563 }
3564 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003566 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003567
3568 return( 0 );
3569}
3570
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003571/*
3572 * Functions to handle the DTLS retransmission state machine
3573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003574#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003575/*
3576 * Append current handshake message to current outgoing flight
3577 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003578static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003580 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003581 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3582 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3583 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003584
3585 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003586 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003587 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003588 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003589 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003590 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003591 }
3592
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003593 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003594 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003595 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003596 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003597 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003598 }
3599
3600 /* Copy current handshake message with headers */
3601 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3602 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 msg->next = NULL;
3605
3606 /* Append to the current flight */
3607 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003608 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003609 else
3610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003611 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003612 while( cur->next != NULL )
3613 cur = cur->next;
3614 cur->next = msg;
3615 }
3616
Hanno Becker3b235902018-08-06 09:54:53 +01003617 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003618 return( 0 );
3619}
3620
3621/*
3622 * Free the current flight of handshake messages
3623 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003624static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003625{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003626 mbedtls_ssl_flight_item *cur = flight;
3627 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003628
3629 while( cur != NULL )
3630 {
3631 next = cur->next;
3632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003633 mbedtls_free( cur->p );
3634 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003635
3636 cur = next;
3637 }
3638}
3639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003640#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3641static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003642#endif
3643
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003644/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003645 * Swap transform_out and out_ctr with the alternative ones
3646 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003647static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003648{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650 unsigned char tmp_out_ctr[8];
3651
3652 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003654 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003655 return;
3656 }
3657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003658 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003659
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003660 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003661 tmp_transform = ssl->transform_out;
3662 ssl->transform_out = ssl->handshake->alt_transform_out;
3663 ssl->handshake->alt_transform_out = tmp_transform;
3664
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003665 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003666 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3667 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003668 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003669
3670 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003671 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3674 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003676 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003677 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003678 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3679 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003680 }
3681 }
3682#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003683}
3684
3685/*
3686 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003687 */
3688int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3689{
3690 int ret = 0;
3691
3692 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3693
3694 ret = mbedtls_ssl_flight_transmit( ssl );
3695
3696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3697
3698 return( ret );
3699}
3700
3701/*
3702 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003703 *
3704 * Need to remember the current message in case flush_output returns
3705 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003706 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003707 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003708int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003709{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003710 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003711 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003712
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003713 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003714 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003715 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003716
3717 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003718 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003719 ssl_swap_epochs( ssl );
3720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003721 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003722 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003723
3724 while( ssl->handshake->cur_msg != NULL )
3725 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003726 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003727 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003728
Hanno Beckere1dcb032018-08-17 16:47:58 +01003729 int const is_finished =
3730 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3731 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3732
Hanno Becker04da1892018-08-14 13:22:10 +01003733 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3734 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3735
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003736 /* Swap epochs before sending Finished: we can't do it after
3737 * sending ChangeCipherSpec, in case write returns WANT_READ.
3738 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003739 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003740 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003742 ssl_swap_epochs( ssl );
3743 }
3744
Hanno Becker67bc7c32018-08-06 11:33:50 +01003745 ret = ssl_get_remaining_payload_in_datagram( ssl );
3746 if( ret < 0 )
3747 return( ret );
3748 max_frag_len = (size_t) ret;
3749
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003750 /* CCS is copied as is, while HS messages may need fragmentation */
3751 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3752 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003753 if( max_frag_len == 0 )
3754 {
3755 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3756 return( ret );
3757
3758 continue;
3759 }
3760
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003761 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003762 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003763 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003764
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003765 /* Update position inside current message */
3766 ssl->handshake->cur_msg_p += cur->len;
3767 }
3768 else
3769 {
3770 const unsigned char * const p = ssl->handshake->cur_msg_p;
3771 const size_t hs_len = cur->len - 12;
3772 const size_t frag_off = p - ( cur->p + 12 );
3773 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003774 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003775
Hanno Beckere1dcb032018-08-17 16:47:58 +01003776 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003777 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003778 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003779 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003780
Hanno Becker67bc7c32018-08-06 11:33:50 +01003781 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3782 return( ret );
3783
3784 continue;
3785 }
3786 max_hs_frag_len = max_frag_len - 12;
3787
3788 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3789 max_hs_frag_len : rem_len;
3790
3791 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003792 {
3793 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003794 (unsigned) cur_hs_frag_len,
3795 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003796 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003797
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003798 /* Messages are stored with handshake headers as if not fragmented,
3799 * copy beginning of headers then fill fragmentation fields.
3800 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3801 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003802
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003803 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3804 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3805 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3806
Hanno Becker67bc7c32018-08-06 11:33:50 +01003807 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3808 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3809 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003810
3811 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3812
Hanno Becker3f7b9732018-08-28 09:53:25 +01003813 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3815 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003816 ssl->out_msgtype = cur->type;
3817
3818 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003819 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003820 }
3821
3822 /* If done with the current message move to the next one if any */
3823 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3824 {
3825 if( cur->next != NULL )
3826 {
3827 ssl->handshake->cur_msg = cur->next;
3828 ssl->handshake->cur_msg_p = cur->next->p + 12;
3829 }
3830 else
3831 {
3832 ssl->handshake->cur_msg = NULL;
3833 ssl->handshake->cur_msg_p = NULL;
3834 }
3835 }
3836
3837 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003838 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003841 return( ret );
3842 }
3843 }
3844
Hanno Becker67bc7c32018-08-06 11:33:50 +01003845 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3846 return( ret );
3847
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003848 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003849 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3850 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003851 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003853 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003854 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3855 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003856
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003857 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003858
3859 return( 0 );
3860}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003861
3862/*
3863 * To be called when the last message of an incoming flight is received.
3864 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003865void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003866{
3867 /* We won't need to resend that one any more */
3868 ssl_flight_free( ssl->handshake->flight );
3869 ssl->handshake->flight = NULL;
3870 ssl->handshake->cur_msg = NULL;
3871
3872 /* The next incoming flight will start with this msg_seq */
3873 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3874
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003875 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003876 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003877
Hanno Becker0271f962018-08-16 13:23:47 +01003878 /* Clear future message buffering structure. */
3879 ssl_buffering_free( ssl );
3880
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003881 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003882 ssl_set_timer( ssl, 0 );
3883
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3885 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003886 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003888 }
3889 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003891}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003892
3893/*
3894 * To be called when the last message of an outgoing flight is send.
3895 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003897{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003898 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003899 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003901 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3902 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003905 }
3906 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003908}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003910
Paul Bakker5121ce52009-01-03 21:22:43 +00003911/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003912 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003913 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003914
3915/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003916 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003917 *
3918 * - fill in handshake headers
3919 * - update handshake checksum
3920 * - DTLS: save message for resending
3921 * - then pass to the record layer
3922 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003923 * DTLS: except for HelloRequest, messages are only queued, and will only be
3924 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003925 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003926 * Inputs:
3927 * - ssl->out_msglen: 4 + actual handshake message len
3928 * (4 is the size of handshake headers for TLS)
3929 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3930 * - ssl->out_msg + 4: the handshake message body
3931 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003932 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003933 * - ssl->out_msglen: the length of the record contents
3934 * (including handshake headers but excluding record headers)
3935 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003936 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003937int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003938{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003939 int ret;
3940 const size_t hs_len = ssl->out_msglen - 4;
3941 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003942
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003943 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3944
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003945 /*
3946 * Sanity checks
3947 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003948 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003949 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3950 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003951 /* In SSLv3, the client might send a NoCertificate alert. */
3952#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3953 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3954 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3955 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3956#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3957 {
3958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3959 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3960 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003961 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003962
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003963 /* Whenever we send anything different from a
3964 * HelloRequest we should be in a handshake - double check. */
3965 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3966 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003967 ssl->handshake == NULL )
3968 {
3969 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3970 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3971 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003973#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003974 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003976 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003977 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003978 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3979 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003980 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003981#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003982
Hanno Beckerb50a2532018-08-06 11:52:54 +01003983 /* Double-check that we did not exceed the bounds
3984 * of the outgoing record buffer.
3985 * This should never fail as the various message
3986 * writing functions must obey the bounds of the
3987 * outgoing record buffer, but better be safe.
3988 *
3989 * Note: We deliberately do not check for the MTU or MFL here.
3990 */
3991 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3992 {
3993 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3994 "size %u, maximum %u",
3995 (unsigned) ssl->out_msglen,
3996 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3997 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3998 }
3999
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004000 /*
4001 * Fill handshake headers
4002 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00004004 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004005 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4006 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4007 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004008
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004009 /*
4010 * DTLS has additional fields in the Handshake layer,
4011 * between the length field and the actual payload:
4012 * uint16 message_seq;
4013 * uint24 fragment_offset;
4014 * uint24 fragment_length;
4015 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004017 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004018 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004019 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004020 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004021 {
4022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4023 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004024 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004025 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004026 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4027 }
4028
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004029 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004030 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004031
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004032 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004033 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004034 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004035 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4036 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4037 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004038 }
4039 else
4040 {
4041 ssl->out_msg[4] = 0;
4042 ssl->out_msg[5] = 0;
4043 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004044
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004045 /* Handshake hashes are computed without fragmentation,
4046 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004047 memset( ssl->out_msg + 6, 0x00, 3 );
4048 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004049 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004050#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004051
Hanno Becker0207e532018-08-28 10:28:28 +01004052 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004053 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4054 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004055 }
4056
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004057 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004059 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004060 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4061 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004062 {
4063 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004065 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004066 return( ret );
4067 }
4068 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004069 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004070#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004071 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004072 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004073 {
4074 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4075 return( ret );
4076 }
4077 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004078
4079 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4080
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004081 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004082}
4083
4084/*
4085 * Record layer functions
4086 */
4087
4088/*
4089 * Write current record.
4090 *
4091 * Uses:
4092 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4093 * - ssl->out_msglen: length of the record content (excl headers)
4094 * - ssl->out_msg: record content
4095 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004096int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004097{
4098 int ret, done = 0;
4099 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004100 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004101
4102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004105 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004106 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004107 {
4108 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4109 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004111 return( ret );
4112 }
4113
4114 len = ssl->out_msglen;
4115 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4119 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004123 ret = mbedtls_ssl_hw_record_write( ssl );
4124 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004126 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4127 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004128 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004129
4130 if( ret == 0 )
4131 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004132 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004133#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004134 if( !done )
4135 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004136 unsigned i;
4137 size_t protected_record_size;
4138
Hanno Becker6430faf2019-05-08 11:57:13 +01004139 /* Skip writing the record content type to after the encryption,
4140 * as it may change when using the CID extension. */
4141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004142 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004143 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004144
Hanno Becker19859472018-08-06 09:40:20 +01004145 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004146 ssl->out_len[0] = (unsigned char)( len >> 8 );
4147 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004148
Paul Bakker48916f92012-09-16 19:57:18 +00004149 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004150 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004151 mbedtls_record rec;
4152
4153 rec.buf = ssl->out_iv;
4154 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4155 ( ssl->out_iv - ssl->out_buf );
4156 rec.data_len = ssl->out_msglen;
4157 rec.data_offset = ssl->out_msg - rec.buf;
4158
4159 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4160 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4161 ssl->conf->transport, rec.ver );
4162 rec.type = ssl->out_msgtype;
4163
Hanno Beckera0e20d02019-05-15 14:03:01 +01004164#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004165 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004166 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004167#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004168
Hanno Beckera18d1322018-01-03 14:27:32 +00004169 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004170 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004172 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004173 return( ret );
4174 }
4175
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004176 if( rec.data_offset != 0 )
4177 {
4178 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4179 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4180 }
4181
Hanno Becker6430faf2019-05-08 11:57:13 +01004182 /* Update the record content type and CID. */
4183 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004184#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004185 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004186#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004187 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004188 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4189 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004190 }
4191
Hanno Becker5903de42019-05-03 14:46:38 +01004192 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004193
4194#if defined(MBEDTLS_SSL_PROTO_DTLS)
4195 /* In case of DTLS, double-check that we don't exceed
4196 * the remaining space in the datagram. */
4197 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4198 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004199 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004200 if( ret < 0 )
4201 return( ret );
4202
4203 if( protected_record_size > (size_t) ret )
4204 {
4205 /* Should never happen */
4206 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4207 }
4208 }
4209#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004210
Hanno Becker6430faf2019-05-08 11:57:13 +01004211 /* Now write the potentially updated record content type. */
4212 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004215 "version = [%d:%d], msglen = %d",
4216 ssl->out_hdr[0], ssl->out_hdr[1],
4217 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004219 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004220 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004221
4222 ssl->out_left += protected_record_size;
4223 ssl->out_hdr += protected_record_size;
4224 ssl_update_out_pointers( ssl, ssl->transform_out );
4225
Hanno Becker04484622018-08-06 09:49:38 +01004226 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4227 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4228 break;
4229
4230 /* The loop goes to its end iff the counter is wrapping */
4231 if( i == ssl_ep_len( ssl ) )
4232 {
4233 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4234 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4235 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004236 }
4237
Hanno Becker67bc7c32018-08-06 11:33:50 +01004238#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004239 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4240 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004241 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004242 size_t remaining;
4243 ret = ssl_get_remaining_payload_in_datagram( ssl );
4244 if( ret < 0 )
4245 {
4246 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4247 ret );
4248 return( ret );
4249 }
4250
4251 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004252 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004253 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004254 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004255 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004256 else
4257 {
Hanno Becker513815a2018-08-20 11:56:09 +01004258 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004259 }
4260 }
4261#endif /* MBEDTLS_SSL_PROTO_DTLS */
4262
4263 if( ( flush == SSL_FORCE_FLUSH ) &&
4264 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004265 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004266 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004267 return( ret );
4268 }
4269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004271
4272 return( 0 );
4273}
4274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004275#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004276
4277static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4278{
4279 if( ssl->in_msglen < ssl->in_hslen ||
4280 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4281 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4282 {
4283 return( 1 );
4284 }
4285 return( 0 );
4286}
Hanno Becker44650b72018-08-16 12:51:11 +01004287
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004288static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004289{
4290 return( ( ssl->in_msg[9] << 16 ) |
4291 ( ssl->in_msg[10] << 8 ) |
4292 ssl->in_msg[11] );
4293}
4294
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004295static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004296{
4297 return( ( ssl->in_msg[6] << 16 ) |
4298 ( ssl->in_msg[7] << 8 ) |
4299 ssl->in_msg[8] );
4300}
4301
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004302static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004303{
4304 uint32_t msg_len, frag_off, frag_len;
4305
4306 msg_len = ssl_get_hs_total_len( ssl );
4307 frag_off = ssl_get_hs_frag_off( ssl );
4308 frag_len = ssl_get_hs_frag_len( ssl );
4309
4310 if( frag_off > msg_len )
4311 return( -1 );
4312
4313 if( frag_len > msg_len - frag_off )
4314 return( -1 );
4315
4316 if( frag_len + 12 > ssl->in_msglen )
4317 return( -1 );
4318
4319 return( 0 );
4320}
4321
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004322/*
4323 * Mark bits in bitmask (used for DTLS HS reassembly)
4324 */
4325static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4326{
4327 unsigned int start_bits, end_bits;
4328
4329 start_bits = 8 - ( offset % 8 );
4330 if( start_bits != 8 )
4331 {
4332 size_t first_byte_idx = offset / 8;
4333
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004334 /* Special case */
4335 if( len <= start_bits )
4336 {
4337 for( ; len != 0; len-- )
4338 mask[first_byte_idx] |= 1 << ( start_bits - len );
4339
4340 /* Avoid potential issues with offset or len becoming invalid */
4341 return;
4342 }
4343
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004344 offset += start_bits; /* Now offset % 8 == 0 */
4345 len -= start_bits;
4346
4347 for( ; start_bits != 0; start_bits-- )
4348 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4349 }
4350
4351 end_bits = len % 8;
4352 if( end_bits != 0 )
4353 {
4354 size_t last_byte_idx = ( offset + len ) / 8;
4355
4356 len -= end_bits; /* Now len % 8 == 0 */
4357
4358 for( ; end_bits != 0; end_bits-- )
4359 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4360 }
4361
4362 memset( mask + offset / 8, 0xFF, len / 8 );
4363}
4364
4365/*
4366 * Check that bitmask is full
4367 */
4368static int ssl_bitmask_check( unsigned char *mask, size_t len )
4369{
4370 size_t i;
4371
4372 for( i = 0; i < len / 8; i++ )
4373 if( mask[i] != 0xFF )
4374 return( -1 );
4375
4376 for( i = 0; i < len % 8; i++ )
4377 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4378 return( -1 );
4379
4380 return( 0 );
4381}
4382
Hanno Becker56e205e2018-08-16 09:06:12 +01004383/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004384static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004385 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004386{
Hanno Becker56e205e2018-08-16 09:06:12 +01004387 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004388
Hanno Becker56e205e2018-08-16 09:06:12 +01004389 alloc_len = 12; /* Handshake header */
4390 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004391
Hanno Beckerd07df862018-08-16 09:14:58 +01004392 if( add_bitmap )
4393 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004394
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004395 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004396}
Hanno Becker56e205e2018-08-16 09:06:12 +01004397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004399
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004400static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004401{
4402 return( ( ssl->in_msg[1] << 16 ) |
4403 ( ssl->in_msg[2] << 8 ) |
4404 ssl->in_msg[3] );
4405}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004406
Simon Butcher99000142016-10-13 17:21:01 +01004407int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004408{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004409 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004410 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004411 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004412 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004414 }
4415
Hanno Becker12555c62018-08-16 12:47:53 +01004416 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004418 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004419 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004420 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004423 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004424 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004425 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004426 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004427
Hanno Becker44650b72018-08-16 12:51:11 +01004428 if( ssl_check_hs_header( ssl ) != 0 )
4429 {
4430 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4431 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4432 }
4433
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004434 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004435 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4436 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4437 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4438 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004439 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004440 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4441 {
4442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4443 recv_msg_seq,
4444 ssl->handshake->in_msg_seq ) );
4445 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4446 }
4447
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004448 /* Retransmit only on last message from previous flight, to avoid
4449 * too many retransmissions.
4450 * Besides, No sane server ever retransmits HelloVerifyRequest */
4451 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004455 "message_seq = %d, start_of_flight = %d",
4456 recv_msg_seq,
4457 ssl->handshake->in_flight_start_seq ) );
4458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004459 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004462 return( ret );
4463 }
4464 }
4465 else
4466 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004468 "message_seq = %d, expected = %d",
4469 recv_msg_seq,
4470 ssl->handshake->in_msg_seq ) );
4471 }
4472
Hanno Becker90333da2017-10-10 11:27:13 +01004473 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004474 }
4475 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004476
Hanno Becker6d97ef52018-08-16 13:09:04 +01004477 /* Message reassembly is handled alongside buffering of future
4478 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004479 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004480 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004481 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004484 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004485 }
4486 }
4487 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004488#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004489 /* With TLS we don't handle fragmentation (for now) */
4490 if( ssl->in_msglen < ssl->in_hslen )
4491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4493 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004494 }
4495
Simon Butcher99000142016-10-13 17:21:01 +01004496 return( 0 );
4497}
4498
4499void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4500{
Hanno Becker0271f962018-08-16 13:23:47 +01004501 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004502
Hanno Becker0271f962018-08-16 13:23:47 +01004503 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004504 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004505 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004506 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004507
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004508 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004509#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004510 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004511 ssl->handshake != NULL )
4512 {
Hanno Becker0271f962018-08-16 13:23:47 +01004513 unsigned offset;
4514 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004515
Hanno Becker0271f962018-08-16 13:23:47 +01004516 /* Increment handshake sequence number */
4517 hs->in_msg_seq++;
4518
4519 /*
4520 * Clear up handshake buffering and reassembly structure.
4521 */
4522
4523 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004524 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004525
4526 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004527 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4528 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004529 offset++, hs_buf++ )
4530 {
4531 *hs_buf = *(hs_buf + 1);
4532 }
4533
4534 /* Create a fresh last entry */
4535 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004536 }
4537#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004538}
4539
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004540/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004541 * DTLS anti-replay: RFC 6347 4.1.2.6
4542 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004543 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4544 * Bit n is set iff record number in_window_top - n has been seen.
4545 *
4546 * Usually, in_window_top is the last record number seen and the lsb of
4547 * in_window is set. The only exception is the initial state (record number 0
4548 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004549 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004550#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4551static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004552{
4553 ssl->in_window_top = 0;
4554 ssl->in_window = 0;
4555}
4556
4557static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4558{
4559 return( ( (uint64_t) buf[0] << 40 ) |
4560 ( (uint64_t) buf[1] << 32 ) |
4561 ( (uint64_t) buf[2] << 24 ) |
4562 ( (uint64_t) buf[3] << 16 ) |
4563 ( (uint64_t) buf[4] << 8 ) |
4564 ( (uint64_t) buf[5] ) );
4565}
4566
4567/*
4568 * Return 0 if sequence number is acceptable, -1 otherwise
4569 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004570int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004571{
4572 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4573 uint64_t bit;
4574
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004575 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004576 return( 0 );
4577
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004578 if( rec_seqnum > ssl->in_window_top )
4579 return( 0 );
4580
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004581 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004582
4583 if( bit >= 64 )
4584 return( -1 );
4585
4586 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4587 return( -1 );
4588
4589 return( 0 );
4590}
4591
4592/*
4593 * Update replay window on new validated record
4594 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004595void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596{
4597 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4598
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004599 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004600 return;
4601
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602 if( rec_seqnum > ssl->in_window_top )
4603 {
4604 /* Update window_top and the contents of the window */
4605 uint64_t shift = rec_seqnum - ssl->in_window_top;
4606
4607 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004608 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004609 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004610 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004611 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004612 ssl->in_window |= 1;
4613 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004614
4615 ssl->in_window_top = rec_seqnum;
4616 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004617 else
4618 {
4619 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004620 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004621
4622 if( bit < 64 ) /* Always true, but be extra sure */
4623 ssl->in_window |= (uint64_t) 1 << bit;
4624 }
4625}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004626#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004627
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004628#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004629/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004630static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4631
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004632/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004633 * Without any SSL context, check if a datagram looks like a ClientHello with
4634 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004635 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004636 *
4637 * - if cookie is valid, return 0
4638 * - if ClientHello looks superficially valid but cookie is not,
4639 * fill obuf and set olen, then
4640 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4641 * - otherwise return a specific error code
4642 */
4643static int ssl_check_dtls_clihlo_cookie(
4644 mbedtls_ssl_cookie_write_t *f_cookie_write,
4645 mbedtls_ssl_cookie_check_t *f_cookie_check,
4646 void *p_cookie,
4647 const unsigned char *cli_id, size_t cli_id_len,
4648 const unsigned char *in, size_t in_len,
4649 unsigned char *obuf, size_t buf_len, size_t *olen )
4650{
4651 size_t sid_len, cookie_len;
4652 unsigned char *p;
4653
4654 if( f_cookie_write == NULL || f_cookie_check == NULL )
4655 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4656
4657 /*
4658 * Structure of ClientHello with record and handshake headers,
4659 * and expected values. We don't need to check a lot, more checks will be
4660 * done when actually parsing the ClientHello - skipping those checks
4661 * avoids code duplication and does not make cookie forging any easier.
4662 *
4663 * 0-0 ContentType type; copied, must be handshake
4664 * 1-2 ProtocolVersion version; copied
4665 * 3-4 uint16 epoch; copied, must be 0
4666 * 5-10 uint48 sequence_number; copied
4667 * 11-12 uint16 length; (ignored)
4668 *
4669 * 13-13 HandshakeType msg_type; (ignored)
4670 * 14-16 uint24 length; (ignored)
4671 * 17-18 uint16 message_seq; copied
4672 * 19-21 uint24 fragment_offset; copied, must be 0
4673 * 22-24 uint24 fragment_length; (ignored)
4674 *
4675 * 25-26 ProtocolVersion client_version; (ignored)
4676 * 27-58 Random random; (ignored)
4677 * 59-xx SessionID session_id; 1 byte len + sid_len content
4678 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4679 * ...
4680 *
4681 * Minimum length is 61 bytes.
4682 */
4683 if( in_len < 61 ||
4684 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4685 in[3] != 0 || in[4] != 0 ||
4686 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4687 {
4688 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4689 }
4690
4691 sid_len = in[59];
4692 if( sid_len > in_len - 61 )
4693 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4694
4695 cookie_len = in[60 + sid_len];
4696 if( cookie_len > in_len - 60 )
4697 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4698
4699 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4700 cli_id, cli_id_len ) == 0 )
4701 {
4702 /* Valid cookie */
4703 return( 0 );
4704 }
4705
4706 /*
4707 * If we get here, we've got an invalid cookie, let's prepare HVR.
4708 *
4709 * 0-0 ContentType type; copied
4710 * 1-2 ProtocolVersion version; copied
4711 * 3-4 uint16 epoch; copied
4712 * 5-10 uint48 sequence_number; copied
4713 * 11-12 uint16 length; olen - 13
4714 *
4715 * 13-13 HandshakeType msg_type; hello_verify_request
4716 * 14-16 uint24 length; olen - 25
4717 * 17-18 uint16 message_seq; copied
4718 * 19-21 uint24 fragment_offset; copied
4719 * 22-24 uint24 fragment_length; olen - 25
4720 *
4721 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4722 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4723 *
4724 * Minimum length is 28.
4725 */
4726 if( buf_len < 28 )
4727 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4728
4729 /* Copy most fields and adapt others */
4730 memcpy( obuf, in, 25 );
4731 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4732 obuf[25] = 0xfe;
4733 obuf[26] = 0xff;
4734
4735 /* Generate and write actual cookie */
4736 p = obuf + 28;
4737 if( f_cookie_write( p_cookie,
4738 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4739 {
4740 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4741 }
4742
4743 *olen = p - obuf;
4744
4745 /* Go back and fill length fields */
4746 obuf[27] = (unsigned char)( *olen - 28 );
4747
4748 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4749 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4750 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4751
4752 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4753 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4754
4755 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4756}
4757
4758/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004759 * Handle possible client reconnect with the same UDP quadruplet
4760 * (RFC 6347 Section 4.2.8).
4761 *
4762 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4763 * that looks like a ClientHello.
4764 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004765 * - if the input looks like a ClientHello without cookies,
4766 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004767 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004768 * - if the input looks like a ClientHello with a valid cookie,
4769 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004770 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004771 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004772 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004773 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004774 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4775 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004776 */
4777static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4778{
4779 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004781
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004782 ret = ssl_check_dtls_clihlo_cookie(
4783 ssl->conf->f_cookie_write,
4784 ssl->conf->f_cookie_check,
4785 ssl->conf->p_cookie,
4786 ssl->cli_id, ssl->cli_id_len,
4787 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004788 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004789
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004790 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4791
4792 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004793 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004794 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004795 * If the error is permanent we'll catch it later,
4796 * if it's not, then hopefully it'll work next time. */
4797 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4798
4799 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004800 }
4801
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004802 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004803 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004804 /* Got a valid cookie, partially reset context */
4805 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4806 {
4807 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4808 return( ret );
4809 }
4810
4811 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004812 }
4813
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004814 return( ret );
4815}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004816#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004817
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004818static int ssl_check_record_type( uint8_t record_type )
4819{
4820 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4821 record_type != MBEDTLS_SSL_MSG_ALERT &&
4822 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4823 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4824 {
4825 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4826 }
4827
4828 return( 0 );
4829}
4830
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004831/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004832 * ContentType type;
4833 * ProtocolVersion version;
4834 * uint16 epoch; // DTLS only
4835 * uint48 sequence_number; // DTLS only
4836 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004837 *
4838 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004839 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004840 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4841 *
4842 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004843 * 1. proceed with the record if this function returns 0
4844 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4845 * 3. return CLIENT_RECONNECT if this function return that value
4846 * 4. drop the whole datagram if this function returns anything else.
4847 * Point 2 is needed when the peer is resending, and we have already received
4848 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004849 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004850static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004851{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004852 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004853 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004854
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004855 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004856
Paul Bakker5121ce52009-01-03 21:22:43 +00004857 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004858 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004859
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004860 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004861#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004862 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4863 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4864 ssl->conf->cid_len != 0 )
4865 {
4866 /* Shift pointers to account for record header including CID
4867 * struct {
4868 * ContentType special_type = tls12_cid;
4869 * ProtocolVersion version;
4870 * uint16 epoch;
4871 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004872 * opaque cid[cid_length]; // Additional field compared to
4873 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004874 * uint16 length;
4875 * opaque enc_content[DTLSCiphertext.length];
4876 * } DTLSCiphertext;
4877 */
4878
4879 /* So far, we only support static CID lengths
4880 * fixed in the configuration. */
4881 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4882 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4883 }
4884 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004885#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004886 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004887 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004888 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004889
4890#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004891 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4892 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004893 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4894#endif /* MBEDTLS_SSL_PROTO_DTLS */
4895 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4896 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004898 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004899 }
4900
4901 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004902 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004903 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004904 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4905 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004906 }
4907
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004908 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004910 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4911 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004912 }
4913
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004914 /* Now that the total length of the record header is known, ensure
4915 * that the current datagram is large enough to hold it.
4916 * This would fail, for example, if we received a datagram of
4917 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4918 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4919 if( ret != 0 )
4920 {
4921 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4922 return( ret );
4923 }
4924 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4925
4926 /* Parse and validate record length
4927 * This must happen after the CID parsing because
4928 * its position in the record header depends on
4929 * the presence of a CID. */
4930
4931 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004932 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004933 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4936 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004937 }
4938
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004939 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004940 "version = [%d:%d], msglen = %d",
4941 ssl->in_msgtype,
4942 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004943
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004944 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004945 * DTLS-related tests.
4946 * Check epoch before checking length constraint because
4947 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4948 * message gets duplicated before the corresponding Finished message,
4949 * the second ChangeCipherSpec should be discarded because it belongs
4950 * to an old epoch, but not because its length is shorter than
4951 * the minimum record length for packets using the new record transform.
4952 * Note that these two kinds of failures are handled differently,
4953 * as an unexpected record is silently skipped but an invalid
4954 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004955 */
4956#if defined(MBEDTLS_SSL_PROTO_DTLS)
4957 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4958 {
4959 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4960
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004961 /* Check epoch (and sequence number) with DTLS */
4962 if( rec_epoch != ssl->in_epoch )
4963 {
4964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4965 "expected %d, received %d",
4966 ssl->in_epoch, rec_epoch ) );
4967
4968#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4969 /*
4970 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4971 * access the first byte of record content (handshake type), as we
4972 * have an active transform (possibly iv_len != 0), so use the
4973 * fact that the record header len is 13 instead.
4974 */
4975 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4976 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4977 rec_epoch == 0 &&
4978 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4979 ssl->in_left > 13 &&
4980 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4981 {
4982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4983 "from the same port" ) );
4984 return( ssl_handle_possible_reconnect( ssl ) );
4985 }
4986 else
4987#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004988 {
4989 /* Consider buffering the record. */
4990 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4991 {
4992 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4993 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4994 }
4995
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004996 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004997 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004998 }
4999
5000#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5001 /* Replay detection only works for the current epoch */
5002 if( rec_epoch == ssl->in_epoch &&
5003 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
5004 {
5005 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5006 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5007 }
5008#endif
5009 }
5010#endif /* MBEDTLS_SSL_PROTO_DTLS */
5011
Hanno Becker52c6dc62017-05-26 16:07:36 +01005012
5013 /* Check length against bounds of the current transform and version */
5014 if( ssl->transform_in == NULL )
5015 {
5016 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005017 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005018 {
5019 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5020 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5021 }
5022 }
5023 else
5024 {
5025 if( ssl->in_msglen < ssl->transform_in->minlen )
5026 {
5027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5028 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5029 }
5030
5031#if defined(MBEDTLS_SSL_PROTO_SSL3)
5032 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005033 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005034 {
5035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5036 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5037 }
5038#endif
5039#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5040 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5041 /*
5042 * TLS encrypted messages can have up to 256 bytes of padding
5043 */
5044 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5045 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005046 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005047 {
5048 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5049 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5050 }
5051#endif
5052 }
5053
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005054 return( 0 );
5055}
Paul Bakker5121ce52009-01-03 21:22:43 +00005056
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005057/*
5058 * If applicable, decrypt (and decompress) record content
5059 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005060static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005061{
5062 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005064 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005065 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5068 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005072 ret = mbedtls_ssl_hw_record_read( ssl );
5073 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005074 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005075 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5076 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005077 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005078
5079 if( ret == 0 )
5080 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005081 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005082#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005083 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005084 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005085 mbedtls_record rec;
5086
5087 rec.buf = ssl->in_iv;
5088 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5089 - ( ssl->in_iv - ssl->in_buf );
5090 rec.data_len = ssl->in_msglen;
5091 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005092#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005093 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005094 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005096
5097 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5098 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5099 ssl->conf->transport, rec.ver );
5100 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005101 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5102 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005103 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005104 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005105
Hanno Beckera0e20d02019-05-15 14:03:01 +01005106#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005107 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5108 ssl->conf->ignore_unexpected_cid
5109 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5110 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005111 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005112 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005113 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005114#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005115
Paul Bakker5121ce52009-01-03 21:22:43 +00005116 return( ret );
5117 }
5118
Hanno Becker6430faf2019-05-08 11:57:13 +01005119 if( ssl->in_msgtype != rec.type )
5120 {
5121 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5122 ssl->in_msgtype, rec.type ) );
5123 }
5124
5125 /* The record content type may change during decryption,
5126 * so re-read it. */
5127 ssl->in_msgtype = rec.type;
5128 /* Also update the input buffer, because unfortunately
5129 * the server-side ssl_parse_client_hello() reparses the
5130 * record header when receiving a ClientHello initiating
5131 * a renegotiation. */
5132 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005133 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005134 ssl->in_msglen = rec.data_len;
5135 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5136 ssl->in_len[1] = (unsigned char)( rec.data_len );
5137
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005138 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5139 ssl->in_msg, ssl->in_msglen );
5140
Hanno Beckera0e20d02019-05-15 14:03:01 +01005141#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005142 /* We have already checked the record content type
5143 * in ssl_parse_record_header(), failing or silently
5144 * dropping the record in the case of an unknown type.
5145 *
5146 * Since with the use of CIDs, the record content type
5147 * might change during decryption, re-check the record
5148 * content type, but treat a failure as fatal this time. */
5149 if( ssl_check_record_type( ssl->in_msgtype ) )
5150 {
5151 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5152 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5153 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005154#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005155
Angus Grattond8213d02016-05-25 20:56:48 +10005156 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005158 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5159 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005160 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005161 else if( ssl->in_msglen == 0 )
5162 {
5163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5164 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5165 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5166 {
5167 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5168 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5169 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5170 }
5171#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5172
5173 ssl->nb_zero++;
5174
5175 /*
5176 * Three or more empty messages may be a DoS attack
5177 * (excessive CPU consumption).
5178 */
5179 if( ssl->nb_zero > 3 )
5180 {
5181 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005182 "messages, possible DoS attack" ) );
5183 /* Treat the records as if they were not properly authenticated,
5184 * thereby failing the connection if we see more than allowed
5185 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005186 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5187 }
5188 }
5189 else
5190 ssl->nb_zero = 0;
5191
5192#if defined(MBEDTLS_SSL_PROTO_DTLS)
5193 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5194 {
5195 ; /* in_ctr read from peer, not maintained internally */
5196 }
5197 else
5198#endif
5199 {
5200 unsigned i;
5201 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5202 if( ++ssl->in_ctr[i - 1] != 0 )
5203 break;
5204
5205 /* The loop goes to its end iff the counter is wrapping */
5206 if( i == ssl_ep_len( ssl ) )
5207 {
5208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5209 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5210 }
5211 }
5212
Paul Bakker5121ce52009-01-03 21:22:43 +00005213 }
5214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005215#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005216 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005217 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005218 {
5219 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005221 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005222 return( ret );
5223 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005224 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005227#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005228 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005230 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005231 }
5232#endif
5233
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005234 return( 0 );
5235}
5236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005237static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005238
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005239/*
5240 * Read a record.
5241 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005242 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5243 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5244 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005245 */
Hanno Becker1097b342018-08-15 14:09:41 +01005246
5247/* Helper functions for mbedtls_ssl_read_record(). */
5248static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005249static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5250static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005251
Hanno Becker327c93b2018-08-15 13:56:18 +01005252int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005253 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005254{
5255 int ret;
5256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005257 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005258
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005259 if( ssl->keep_current_message == 0 )
5260 {
5261 do {
Simon Butcher99000142016-10-13 17:21:01 +01005262
Hanno Becker26994592018-08-15 14:14:59 +01005263 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005264 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005265 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005266
Hanno Beckere74d5562018-08-15 14:26:08 +01005267 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005268 {
Hanno Becker40f50842018-08-15 14:48:01 +01005269#if defined(MBEDTLS_SSL_PROTO_DTLS)
5270 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005271
Hanno Becker40f50842018-08-15 14:48:01 +01005272 /* We only check for buffered messages if the
5273 * current datagram is fully consumed. */
5274 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005275 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005276 {
Hanno Becker40f50842018-08-15 14:48:01 +01005277 if( ssl_load_buffered_message( ssl ) == 0 )
5278 have_buffered = 1;
5279 }
5280
5281 if( have_buffered == 0 )
5282#endif /* MBEDTLS_SSL_PROTO_DTLS */
5283 {
5284 ret = ssl_get_next_record( ssl );
5285 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5286 continue;
5287
5288 if( ret != 0 )
5289 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005290 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005291 return( ret );
5292 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005293 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005294 }
5295
5296 ret = mbedtls_ssl_handle_message_type( ssl );
5297
Hanno Becker40f50842018-08-15 14:48:01 +01005298#if defined(MBEDTLS_SSL_PROTO_DTLS)
5299 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5300 {
5301 /* Buffer future message */
5302 ret = ssl_buffer_message( ssl );
5303 if( ret != 0 )
5304 return( ret );
5305
5306 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5307 }
5308#endif /* MBEDTLS_SSL_PROTO_DTLS */
5309
Hanno Becker90333da2017-10-10 11:27:13 +01005310 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5311 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005312
5313 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005314 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005315 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005316 return( ret );
5317 }
5318
Hanno Becker327c93b2018-08-15 13:56:18 +01005319 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005320 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005321 {
5322 mbedtls_ssl_update_handshake_status( ssl );
5323 }
Simon Butcher99000142016-10-13 17:21:01 +01005324 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005325 else
Simon Butcher99000142016-10-13 17:21:01 +01005326 {
Hanno Becker02f59072018-08-15 14:00:24 +01005327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005328 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005329 }
5330
5331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5332
5333 return( 0 );
5334}
5335
Hanno Becker40f50842018-08-15 14:48:01 +01005336#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005337static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005338{
Hanno Becker40f50842018-08-15 14:48:01 +01005339 if( ssl->in_left > ssl->next_record_offset )
5340 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005341
Hanno Becker40f50842018-08-15 14:48:01 +01005342 return( 0 );
5343}
5344
5345static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5346{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005347 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005348 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005349 int ret = 0;
5350
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005351 if( hs == NULL )
5352 return( -1 );
5353
Hanno Beckere00ae372018-08-20 09:39:42 +01005354 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5355
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005356 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5357 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5358 {
5359 /* Check if we have seen a ChangeCipherSpec before.
5360 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005361 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005362 {
5363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5364 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005365 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005366 }
5367
Hanno Becker39b8bc92018-08-28 17:17:13 +01005368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005369 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5370 ssl->in_msglen = 1;
5371 ssl->in_msg[0] = 1;
5372
5373 /* As long as they are equal, the exact value doesn't matter. */
5374 ssl->in_left = 0;
5375 ssl->next_record_offset = 0;
5376
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005377 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005378 goto exit;
5379 }
Hanno Becker37f95322018-08-16 13:55:32 +01005380
Hanno Beckerb8f50142018-08-28 10:01:34 +01005381#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005382 /* Debug only */
5383 {
5384 unsigned offset;
5385 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5386 {
5387 hs_buf = &hs->buffering.hs[offset];
5388 if( hs_buf->is_valid == 1 )
5389 {
5390 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5391 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005392 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005393 }
5394 }
5395 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005396#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005397
5398 /* Check if we have buffered and/or fully reassembled the
5399 * next handshake message. */
5400 hs_buf = &hs->buffering.hs[0];
5401 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5402 {
5403 /* Synthesize a record containing the buffered HS message. */
5404 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5405 ( hs_buf->data[2] << 8 ) |
5406 hs_buf->data[3];
5407
5408 /* Double-check that we haven't accidentally buffered
5409 * a message that doesn't fit into the input buffer. */
5410 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5411 {
5412 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5413 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5414 }
5415
5416 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5417 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5418 hs_buf->data, msg_len + 12 );
5419
5420 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5421 ssl->in_hslen = msg_len + 12;
5422 ssl->in_msglen = msg_len + 12;
5423 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5424
5425 ret = 0;
5426 goto exit;
5427 }
5428 else
5429 {
5430 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5431 hs->in_msg_seq ) );
5432 }
5433
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005434 ret = -1;
5435
5436exit:
5437
5438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5439 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005440}
5441
Hanno Beckera02b0b42018-08-21 17:20:27 +01005442static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5443 size_t desired )
5444{
5445 int offset;
5446 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005447 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5448 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005449
Hanno Becker01315ea2018-08-21 17:22:17 +01005450 /* Get rid of future records epoch first, if such exist. */
5451 ssl_free_buffered_record( ssl );
5452
5453 /* Check if we have enough space available now. */
5454 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5455 hs->buffering.total_bytes_buffered ) )
5456 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005457 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005458 return( 0 );
5459 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005460
Hanno Becker4f432ad2018-08-28 10:02:32 +01005461 /* We don't have enough space to buffer the next expected handshake
5462 * message. Remove buffers used for future messages to gain space,
5463 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005464 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5465 offset >= 0; offset-- )
5466 {
5467 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5468 offset ) );
5469
Hanno Beckerb309b922018-08-23 13:18:05 +01005470 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005471
5472 /* Check if we have enough space available now. */
5473 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5474 hs->buffering.total_bytes_buffered ) )
5475 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005477 return( 0 );
5478 }
5479 }
5480
5481 return( -1 );
5482}
5483
Hanno Becker40f50842018-08-15 14:48:01 +01005484static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5485{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005486 int ret = 0;
5487 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5488
5489 if( hs == NULL )
5490 return( 0 );
5491
5492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5493
5494 switch( ssl->in_msgtype )
5495 {
5496 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5497 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005498
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005499 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005500 break;
5501
5502 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005503 {
5504 unsigned recv_msg_seq_offset;
5505 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5506 mbedtls_ssl_hs_buffer *hs_buf;
5507 size_t msg_len = ssl->in_hslen - 12;
5508
5509 /* We should never receive an old handshake
5510 * message - double-check nonetheless. */
5511 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5512 {
5513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5515 }
5516
5517 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5518 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5519 {
5520 /* Silently ignore -- message too far in the future */
5521 MBEDTLS_SSL_DEBUG_MSG( 2,
5522 ( "Ignore future HS message with sequence number %u, "
5523 "buffering window %u - %u",
5524 recv_msg_seq, ssl->handshake->in_msg_seq,
5525 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5526
5527 goto exit;
5528 }
5529
5530 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5531 recv_msg_seq, recv_msg_seq_offset ) );
5532
5533 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5534
5535 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005536 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005537 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005538 size_t reassembly_buf_sz;
5539
Hanno Becker37f95322018-08-16 13:55:32 +01005540 hs_buf->is_fragmented =
5541 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5542
5543 /* We copy the message back into the input buffer
5544 * after reassembly, so check that it's not too large.
5545 * This is an implementation-specific limitation
5546 * and not one from the standard, hence it is not
5547 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005548 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005549 {
5550 /* Ignore message */
5551 goto exit;
5552 }
5553
Hanno Beckere0b150f2018-08-21 15:51:03 +01005554 /* Check if we have enough space to buffer the message. */
5555 if( hs->buffering.total_bytes_buffered >
5556 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5557 {
5558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5560 }
5561
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005562 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5563 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005564
5565 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5566 hs->buffering.total_bytes_buffered ) )
5567 {
5568 if( recv_msg_seq_offset > 0 )
5569 {
5570 /* If we can't buffer a future message because
5571 * of space limitations -- ignore. */
5572 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5573 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5574 (unsigned) hs->buffering.total_bytes_buffered ) );
5575 goto exit;
5576 }
Hanno Beckere1801392018-08-21 16:51:05 +01005577 else
5578 {
5579 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future message of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- attempt to make space by freeing buffered future messages\n",
5580 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5581 (unsigned) hs->buffering.total_bytes_buffered ) );
5582 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005583
Hanno Beckera02b0b42018-08-21 17:20:27 +01005584 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005585 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005586 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Reassembly of next message of size %u (%u with bitmap) would exceed the compile-time limit %u (already %u bytes buffered) -- fail\n",
5587 (unsigned) msg_len,
5588 (unsigned) reassembly_buf_sz,
5589 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005590 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005591 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5592 goto exit;
5593 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005594 }
5595
5596 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5597 msg_len ) );
5598
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005599 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5600 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005601 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005602 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005603 goto exit;
5604 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005605 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005606
5607 /* Prepare final header: copy msg_type, length and message_seq,
5608 * then add standardised fragment_offset and fragment_length */
5609 memcpy( hs_buf->data, ssl->in_msg, 6 );
5610 memset( hs_buf->data + 6, 0, 3 );
5611 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5612
5613 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005614
5615 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005616 }
5617 else
5618 {
5619 /* Make sure msg_type and length are consistent */
5620 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5621 {
5622 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5623 /* Ignore */
5624 goto exit;
5625 }
5626 }
5627
Hanno Becker4422bbb2018-08-20 09:40:19 +01005628 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005629 {
5630 size_t frag_len, frag_off;
5631 unsigned char * const msg = hs_buf->data + 12;
5632
5633 /*
5634 * Check and copy current fragment
5635 */
5636
5637 /* Validation of header fields already done in
5638 * mbedtls_ssl_prepare_handshake_record(). */
5639 frag_off = ssl_get_hs_frag_off( ssl );
5640 frag_len = ssl_get_hs_frag_len( ssl );
5641
5642 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5643 frag_off, frag_len ) );
5644 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5645
5646 if( hs_buf->is_fragmented )
5647 {
5648 unsigned char * const bitmask = msg + msg_len;
5649 ssl_bitmask_set( bitmask, frag_off, frag_len );
5650 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5651 msg_len ) == 0 );
5652 }
5653 else
5654 {
5655 hs_buf->is_complete = 1;
5656 }
5657
5658 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5659 hs_buf->is_complete ? "" : "not yet " ) );
5660 }
5661
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005662 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005663 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005664
5665 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005666 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005667 break;
5668 }
5669
5670exit:
5671
5672 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5673 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005674}
5675#endif /* MBEDTLS_SSL_PROTO_DTLS */
5676
Hanno Becker1097b342018-08-15 14:09:41 +01005677static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005678{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005679 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005680 * Consume last content-layer message and potentially
5681 * update in_msglen which keeps track of the contents'
5682 * consumption state.
5683 *
5684 * (1) Handshake messages:
5685 * Remove last handshake message, move content
5686 * and adapt in_msglen.
5687 *
5688 * (2) Alert messages:
5689 * Consume whole record content, in_msglen = 0.
5690 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005691 * (3) Change cipher spec:
5692 * Consume whole record content, in_msglen = 0.
5693 *
5694 * (4) Application data:
5695 * Don't do anything - the record layer provides
5696 * the application data as a stream transport
5697 * and consumes through mbedtls_ssl_read only.
5698 *
5699 */
5700
5701 /* Case (1): Handshake messages */
5702 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005703 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005704 /* Hard assertion to be sure that no application data
5705 * is in flight, as corrupting ssl->in_msglen during
5706 * ssl->in_offt != NULL is fatal. */
5707 if( ssl->in_offt != NULL )
5708 {
5709 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5710 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5711 }
5712
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005713 /*
5714 * Get next Handshake message in the current record
5715 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005716
Hanno Becker4a810fb2017-05-24 16:27:30 +01005717 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005718 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005719 * current handshake content: If DTLS handshake
5720 * fragmentation is used, that's the fragment
5721 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005722 * size here is faulty and should be changed at
5723 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005724 * (2) While it doesn't seem to cause problems, one
5725 * has to be very careful not to assume that in_hslen
5726 * is always <= in_msglen in a sensible communication.
5727 * Again, it's wrong for DTLS handshake fragmentation.
5728 * The following check is therefore mandatory, and
5729 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005730 * Additionally, ssl->in_hslen might be arbitrarily out of
5731 * bounds after handling a DTLS message with an unexpected
5732 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005733 */
5734 if( ssl->in_hslen < ssl->in_msglen )
5735 {
5736 ssl->in_msglen -= ssl->in_hslen;
5737 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5738 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005739
Hanno Becker4a810fb2017-05-24 16:27:30 +01005740 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5741 ssl->in_msg, ssl->in_msglen );
5742 }
5743 else
5744 {
5745 ssl->in_msglen = 0;
5746 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005747
Hanno Becker4a810fb2017-05-24 16:27:30 +01005748 ssl->in_hslen = 0;
5749 }
5750 /* Case (4): Application data */
5751 else if( ssl->in_offt != NULL )
5752 {
5753 return( 0 );
5754 }
5755 /* Everything else (CCS & Alerts) */
5756 else
5757 {
5758 ssl->in_msglen = 0;
5759 }
5760
Hanno Becker1097b342018-08-15 14:09:41 +01005761 return( 0 );
5762}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005763
Hanno Beckere74d5562018-08-15 14:26:08 +01005764static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5765{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005766 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005767 return( 1 );
5768
5769 return( 0 );
5770}
5771
Hanno Becker5f066e72018-08-16 14:56:31 +01005772#if defined(MBEDTLS_SSL_PROTO_DTLS)
5773
5774static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5775{
5776 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5777 if( hs == NULL )
5778 return;
5779
Hanno Becker01315ea2018-08-21 17:22:17 +01005780 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005781 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005782 hs->buffering.total_bytes_buffered -=
5783 hs->buffering.future_record.len;
5784
5785 mbedtls_free( hs->buffering.future_record.data );
5786 hs->buffering.future_record.data = NULL;
5787 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005788}
5789
5790static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5791{
5792 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5793 unsigned char * rec;
5794 size_t rec_len;
5795 unsigned rec_epoch;
5796
5797 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5798 return( 0 );
5799
5800 if( hs == NULL )
5801 return( 0 );
5802
Hanno Becker5f066e72018-08-16 14:56:31 +01005803 rec = hs->buffering.future_record.data;
5804 rec_len = hs->buffering.future_record.len;
5805 rec_epoch = hs->buffering.future_record.epoch;
5806
5807 if( rec == NULL )
5808 return( 0 );
5809
Hanno Becker4cb782d2018-08-20 11:19:05 +01005810 /* Only consider loading future records if the
5811 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005812 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005813 return( 0 );
5814
Hanno Becker5f066e72018-08-16 14:56:31 +01005815 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5816
5817 if( rec_epoch != ssl->in_epoch )
5818 {
5819 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5820 goto exit;
5821 }
5822
5823 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5824
5825 /* Double-check that the record is not too large */
5826 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5827 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5828 {
5829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5830 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5831 }
5832
5833 memcpy( ssl->in_hdr, rec, rec_len );
5834 ssl->in_left = rec_len;
5835 ssl->next_record_offset = 0;
5836
5837 ssl_free_buffered_record( ssl );
5838
5839exit:
5840 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5841 return( 0 );
5842}
5843
5844static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5845{
5846 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5847 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005848 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005849
5850 /* Don't buffer future records outside handshakes. */
5851 if( hs == NULL )
5852 return( 0 );
5853
5854 /* Only buffer handshake records (we are only interested
5855 * in Finished messages). */
5856 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5857 return( 0 );
5858
5859 /* Don't buffer more than one future epoch record. */
5860 if( hs->buffering.future_record.data != NULL )
5861 return( 0 );
5862
Hanno Becker01315ea2018-08-21 17:22:17 +01005863 /* Don't buffer record if there's not enough buffering space remaining. */
5864 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5865 hs->buffering.total_bytes_buffered ) )
5866 {
5867 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering of future epoch record of size %u would exceed the compile-time limit %u (already %u bytes buffered) -- ignore\n",
5868 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5869 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005870 return( 0 );
5871 }
5872
Hanno Becker5f066e72018-08-16 14:56:31 +01005873 /* Buffer record */
5874 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5875 ssl->in_epoch + 1 ) );
5876 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5877 rec_hdr_len + ssl->in_msglen );
5878
5879 /* ssl_parse_record_header() only considers records
5880 * of the next epoch as candidates for buffering. */
5881 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005882 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005883
5884 hs->buffering.future_record.data =
5885 mbedtls_calloc( 1, hs->buffering.future_record.len );
5886 if( hs->buffering.future_record.data == NULL )
5887 {
5888 /* If we run out of RAM trying to buffer a
5889 * record from the next epoch, just ignore. */
5890 return( 0 );
5891 }
5892
Hanno Becker01315ea2018-08-21 17:22:17 +01005893 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005894
Hanno Becker01315ea2018-08-21 17:22:17 +01005895 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005896 return( 0 );
5897}
5898
5899#endif /* MBEDTLS_SSL_PROTO_DTLS */
5900
Hanno Beckere74d5562018-08-15 14:26:08 +01005901static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005902{
5903 int ret;
5904
Hanno Becker5f066e72018-08-16 14:56:31 +01005905#if defined(MBEDTLS_SSL_PROTO_DTLS)
5906 /* We might have buffered a future record; if so,
5907 * and if the epoch matches now, load it.
5908 * On success, this call will set ssl->in_left to
5909 * the length of the buffered record, so that
5910 * the calls to ssl_fetch_input() below will
5911 * essentially be no-ops. */
5912 ret = ssl_load_buffered_record( ssl );
5913 if( ret != 0 )
5914 return( ret );
5915#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005916
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005917 /* Reset in pointers to default state for TLS/DTLS records,
5918 * assuming no CID and no offset between record content and
5919 * record plaintext. */
5920 ssl_update_in_pointers( ssl );
5921
5922 /* Ensure that we have enough space available for the default form
5923 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5924 * with no space for CIDs counted in). */
5925 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5926 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005927 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005928 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005929 return( ret );
5930 }
5931
5932 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005933 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005934#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005935 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5936 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005937 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005938 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5939 {
5940 ret = ssl_buffer_future_record( ssl );
5941 if( ret != 0 )
5942 return( ret );
5943
5944 /* Fall through to handling of unexpected records */
5945 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5946 }
5947
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005948 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5949 {
5950 /* Skip unexpected record (but not whole datagram) */
5951 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005952 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005953
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005954 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5955 "(header)" ) );
5956 }
5957 else
5958 {
5959 /* Skip invalid record and the rest of the datagram */
5960 ssl->next_record_offset = 0;
5961 ssl->in_left = 0;
5962
5963 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5964 "(header)" ) );
5965 }
5966
5967 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005968 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005969 }
5970#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005971 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005972 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005973
5974 /*
5975 * Read and optionally decrypt the message contents
5976 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005977 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005978 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005979 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005981 return( ret );
5982 }
5983
5984 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005986 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005987 {
Hanno Becker5903de42019-05-03 14:46:38 +01005988 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005989 if( ssl->next_record_offset < ssl->in_left )
5990 {
5991 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5992 }
5993 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005994 else
5995#endif
5996 ssl->in_left = 0;
5997
5998 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005999 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006000#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006001 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006002 {
6003 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01006004 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006005 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006006 /* Except when waiting for Finished as a bad mac here
6007 * probably means something went wrong in the handshake
6008 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6009 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6010 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6011 {
6012#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6013 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6014 {
6015 mbedtls_ssl_send_alert_message( ssl,
6016 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6017 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6018 }
6019#endif
6020 return( ret );
6021 }
6022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006023#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006024 if( ssl->conf->badmac_limit != 0 &&
6025 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6028 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006029 }
6030#endif
6031
Hanno Becker4a810fb2017-05-24 16:27:30 +01006032 /* As above, invalid records cause
6033 * dismissal of the whole datagram. */
6034
6035 ssl->next_record_offset = 0;
6036 ssl->in_left = 0;
6037
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006038 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006039 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006040 }
6041
6042 return( ret );
6043 }
6044 else
6045#endif
6046 {
6047 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006048#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6049 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006050 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006051 mbedtls_ssl_send_alert_message( ssl,
6052 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6053 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006054 }
6055#endif
6056 return( ret );
6057 }
6058 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006059
Simon Butcher99000142016-10-13 17:21:01 +01006060 return( 0 );
6061}
6062
6063int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6064{
6065 int ret;
6066
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006067 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006068 * Handle particular types of records
6069 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006070 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 {
Simon Butcher99000142016-10-13 17:21:01 +01006072 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6073 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006074 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006075 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 }
6077
Hanno Beckere678eaa2018-08-21 14:57:46 +01006078 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006079 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006080 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006081 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006082 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6083 ssl->in_msglen ) );
6084 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006085 }
6086
Hanno Beckere678eaa2018-08-21 14:57:46 +01006087 if( ssl->in_msg[0] != 1 )
6088 {
6089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6090 ssl->in_msg[0] ) );
6091 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6092 }
6093
6094#if defined(MBEDTLS_SSL_PROTO_DTLS)
6095 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6096 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6097 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6098 {
6099 if( ssl->handshake == NULL )
6100 {
6101 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6102 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6103 }
6104
6105 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6106 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6107 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006108#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006109 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006111 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006112 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006113 if( ssl->in_msglen != 2 )
6114 {
6115 /* Note: Standard allows for more than one 2 byte alert
6116 to be packed in a single message, but Mbed TLS doesn't
6117 currently support this. */
6118 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6119 ssl->in_msglen ) );
6120 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6121 }
6122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006123 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006124 ssl->in_msg[0], ssl->in_msg[1] ) );
6125
6126 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006127 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006129 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006130 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006132 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006133 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006134 }
6135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006136 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6137 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006138 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006139 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6140 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006141 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006142
6143#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6144 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6145 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6146 {
Hanno Becker90333da2017-10-10 11:27:13 +01006147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006148 /* Will be handled when trying to parse ServerHello */
6149 return( 0 );
6150 }
6151#endif
6152
6153#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6154 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6155 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6156 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6157 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6158 {
6159 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6160 /* Will be handled in mbedtls_ssl_parse_certificate() */
6161 return( 0 );
6162 }
6163#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6164
6165 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006166 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006167 }
6168
Hanno Beckerc76c6192017-06-06 10:03:17 +01006169#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006170 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006171 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006172 /* Drop unexpected ApplicationData records,
6173 * except at the beginning of renegotiations */
6174 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6175 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6176#if defined(MBEDTLS_SSL_RENEGOTIATION)
6177 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6178 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006179#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006180 )
6181 {
6182 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6183 return( MBEDTLS_ERR_SSL_NON_FATAL );
6184 }
6185
6186 if( ssl->handshake != NULL &&
6187 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6188 {
6189 ssl_handshake_wrapup_free_hs_transform( ssl );
6190 }
6191 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006192#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006193
Paul Bakker5121ce52009-01-03 21:22:43 +00006194 return( 0 );
6195}
6196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006197int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006198{
6199 int ret;
6200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006201 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6202 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6203 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006204 {
6205 return( ret );
6206 }
6207
6208 return( 0 );
6209}
6210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006211int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006212 unsigned char level,
6213 unsigned char message )
6214{
6215 int ret;
6216
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006217 if( ssl == NULL || ssl->conf == NULL )
6218 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006220 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006221 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006224 ssl->out_msglen = 2;
6225 ssl->out_msg[0] = level;
6226 ssl->out_msg[1] = message;
6227
Hanno Becker67bc7c32018-08-06 11:33:50 +01006228 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006229 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006231 return( ret );
6232 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006234
6235 return( 0 );
6236}
6237
Hanno Beckerb9d44792019-02-08 07:19:04 +00006238#if defined(MBEDTLS_X509_CRT_PARSE_C)
6239static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6240{
6241#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6242 if( session->peer_cert != NULL )
6243 {
6244 mbedtls_x509_crt_free( session->peer_cert );
6245 mbedtls_free( session->peer_cert );
6246 session->peer_cert = NULL;
6247 }
6248#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6249 if( session->peer_cert_digest != NULL )
6250 {
6251 /* Zeroization is not necessary. */
6252 mbedtls_free( session->peer_cert_digest );
6253 session->peer_cert_digest = NULL;
6254 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6255 session->peer_cert_digest_len = 0;
6256 }
6257#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6258}
6259#endif /* MBEDTLS_X509_CRT_PARSE_C */
6260
Paul Bakker5121ce52009-01-03 21:22:43 +00006261/*
6262 * Handshake functions
6263 */
Hanno Becker21489932019-02-05 13:20:55 +00006264#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006265/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006267{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006268 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6269 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006270
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006271 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006272
Hanno Becker7177a882019-02-05 13:36:46 +00006273 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006274 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006276 ssl->state++;
6277 return( 0 );
6278 }
6279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006280 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6281 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006282}
6283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006285{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006286 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6287 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006289 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006290
Hanno Becker7177a882019-02-05 13:36:46 +00006291 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006292 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006294 ssl->state++;
6295 return( 0 );
6296 }
6297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6299 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006300}
Gilles Peskinef9828522017-05-03 12:28:43 +02006301
Hanno Becker21489932019-02-05 13:20:55 +00006302#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006303/* Some certificate support -> implement write and parse */
6304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006305int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006306{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006307 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006308 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006309 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006310 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6311 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006312
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006313 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006314
Hanno Becker7177a882019-02-05 13:36:46 +00006315 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006318 ssl->state++;
6319 return( 0 );
6320 }
6321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006323 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006324 {
6325 if( ssl->client_auth == 0 )
6326 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 ssl->state++;
6329 return( 0 );
6330 }
6331
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006333 /*
6334 * If using SSLv3 and got no cert, send an Alert message
6335 * (otherwise an empty Certificate message will be sent).
6336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006337 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6338 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006339 {
6340 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006341 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6342 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6343 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006346 goto write_msg;
6347 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006348#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006349 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006350#endif /* MBEDTLS_SSL_CLI_C */
6351#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006352 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006354 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006356 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6357 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006358 }
6359 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006360#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006362 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006363
6364 /*
6365 * 0 . 0 handshake type
6366 * 1 . 3 handshake length
6367 * 4 . 6 length of all certs
6368 * 7 . 9 length of cert. 1
6369 * 10 . n-1 peer certificate
6370 * n . n+2 length of cert. 2
6371 * n+3 . ... upper level cert, etc.
6372 */
6373 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006375
Paul Bakker29087132010-03-21 21:03:34 +00006376 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006377 {
6378 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006379 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006380 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006381 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006382 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 }
6385
6386 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6387 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6388 ssl->out_msg[i + 2] = (unsigned char)( n );
6389
6390 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6391 i += n; crt = crt->next;
6392 }
6393
6394 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6395 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6396 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6397
6398 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006399 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6400 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006401
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006402#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006403write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006404#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006405
6406 ssl->state++;
6407
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006408 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006409 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006410 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006411 return( ret );
6412 }
6413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006415
Paul Bakkered27a042013-04-18 22:46:23 +02006416 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006417}
6418
Hanno Becker84879e32019-01-31 07:44:03 +00006419#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006420
6421#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006422static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6423 unsigned char *crt_buf,
6424 size_t crt_buf_len )
6425{
6426 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6427
6428 if( peer_crt == NULL )
6429 return( -1 );
6430
6431 if( peer_crt->raw.len != crt_buf_len )
6432 return( -1 );
6433
Hanno Becker46f34d02019-02-08 14:00:04 +00006434 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006435}
Hanno Becker177475a2019-02-05 17:02:46 +00006436#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6437static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6438 unsigned char *crt_buf,
6439 size_t crt_buf_len )
6440{
6441 int ret;
6442 unsigned char const * const peer_cert_digest =
6443 ssl->session->peer_cert_digest;
6444 mbedtls_md_type_t const peer_cert_digest_type =
6445 ssl->session->peer_cert_digest_type;
6446 mbedtls_md_info_t const * const digest_info =
6447 mbedtls_md_info_from_type( peer_cert_digest_type );
6448 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6449 size_t digest_len;
6450
6451 if( peer_cert_digest == NULL || digest_info == NULL )
6452 return( -1 );
6453
6454 digest_len = mbedtls_md_get_size( digest_info );
6455 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6456 return( -1 );
6457
6458 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6459 if( ret != 0 )
6460 return( -1 );
6461
6462 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6463}
6464#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006465#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006466
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006467/*
6468 * Once the certificate message is read, parse it into a cert chain and
6469 * perform basic checks, but leave actual verification to the caller
6470 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006471static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6472 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006473{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006474 int ret;
6475#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6476 int crt_cnt=0;
6477#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006478 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006479 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006480
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006483 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006484 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6485 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006486 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006487 }
6488
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006489 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6490 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006493 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6494 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006495 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006496 }
6497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006498 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006499
Paul Bakker5121ce52009-01-03 21:22:43 +00006500 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006503 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006504
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006505 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006509 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6510 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006511 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006512 }
6513
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006514 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6515 i += 3;
6516
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006517 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006518 while( i < ssl->in_hslen )
6519 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006520 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006521 if ( i + 3 > ssl->in_hslen ) {
6522 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006523 mbedtls_ssl_send_alert_message( ssl,
6524 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6525 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006526 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6527 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006528 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6529 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006530 if( ssl->in_msg[i] != 0 )
6531 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006533 mbedtls_ssl_send_alert_message( ssl,
6534 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6535 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006536 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006537 }
6538
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006539 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006540 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6541 | (unsigned int) ssl->in_msg[i + 2];
6542 i += 3;
6543
6544 if( n < 128 || i + n > ssl->in_hslen )
6545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006547 mbedtls_ssl_send_alert_message( ssl,
6548 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6549 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006550 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006551 }
6552
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006553 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006554#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6555 if( crt_cnt++ == 0 &&
6556 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6557 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006558 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006559 /* During client-side renegotiation, check that the server's
6560 * end-CRTs hasn't changed compared to the initial handshake,
6561 * mitigating the triple handshake attack. On success, reuse
6562 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006563 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6564 if( ssl_check_peer_crt_unchanged( ssl,
6565 &ssl->in_msg[i],
6566 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006567 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006568 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6569 mbedtls_ssl_send_alert_message( ssl,
6570 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6571 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6572 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006573 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006574
6575 /* Now we can safely free the original chain. */
6576 ssl_clear_peer_cert( ssl->session );
6577 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006578#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6579
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006580 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006581#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006582 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006583#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006584 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006585 * it in-place from the input buffer instead of making a copy. */
6586 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6587#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006588 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006589 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006590 case 0: /*ok*/
6591 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6592 /* Ignore certificate with an unknown algorithm: maybe a
6593 prior certificate was already trusted. */
6594 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006595
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006596 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6597 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6598 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006599
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006600 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6601 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6602 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006603
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006604 default:
6605 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6606 crt_parse_der_failed:
6607 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6608 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6609 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006610 }
6611
6612 i += n;
6613 }
6614
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006615 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006616 return( 0 );
6617}
6618
Hanno Becker4a55f632019-02-05 12:49:06 +00006619#if defined(MBEDTLS_SSL_SRV_C)
6620static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6621{
6622 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6623 return( -1 );
6624
6625#if defined(MBEDTLS_SSL_PROTO_SSL3)
6626 /*
6627 * Check if the client sent an empty certificate
6628 */
6629 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6630 {
6631 if( ssl->in_msglen == 2 &&
6632 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6633 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6634 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6635 {
6636 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6637 return( 0 );
6638 }
6639
6640 return( -1 );
6641 }
6642#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6643
6644#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6645 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6646 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6647 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6648 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6649 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6650 {
6651 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6652 return( 0 );
6653 }
6654
6655 return( -1 );
6656#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6657 MBEDTLS_SSL_PROTO_TLS1_2 */
6658}
6659#endif /* MBEDTLS_SSL_SRV_C */
6660
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006661/* Check if a certificate message is expected.
6662 * Return either
6663 * - SSL_CERTIFICATE_EXPECTED, or
6664 * - SSL_CERTIFICATE_SKIP
6665 * indicating whether a Certificate message is expected or not.
6666 */
6667#define SSL_CERTIFICATE_EXPECTED 0
6668#define SSL_CERTIFICATE_SKIP 1
6669static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6670 int authmode )
6671{
6672 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006673 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006674
6675 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6676 return( SSL_CERTIFICATE_SKIP );
6677
6678#if defined(MBEDTLS_SSL_SRV_C)
6679 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6680 {
6681 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6682 return( SSL_CERTIFICATE_SKIP );
6683
6684 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6685 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006686 ssl->session_negotiate->verify_result =
6687 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6688 return( SSL_CERTIFICATE_SKIP );
6689 }
6690 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006691#else
6692 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006693#endif /* MBEDTLS_SSL_SRV_C */
6694
6695 return( SSL_CERTIFICATE_EXPECTED );
6696}
6697
Hanno Becker68636192019-02-05 14:36:34 +00006698static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6699 int authmode,
6700 mbedtls_x509_crt *chain,
6701 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006702{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006703 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006704 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006705 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006706 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006707
Hanno Becker8927c832019-04-03 12:52:50 +01006708 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6709 void *p_vrfy;
6710
Hanno Becker68636192019-02-05 14:36:34 +00006711 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6712 return( 0 );
6713
Hanno Becker8927c832019-04-03 12:52:50 +01006714 if( ssl->f_vrfy != NULL )
6715 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006716 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006717 f_vrfy = ssl->f_vrfy;
6718 p_vrfy = ssl->p_vrfy;
6719 }
6720 else
6721 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006722 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006723 f_vrfy = ssl->conf->f_vrfy;
6724 p_vrfy = ssl->conf->p_vrfy;
6725 }
6726
Hanno Becker68636192019-02-05 14:36:34 +00006727 /*
6728 * Main check: verify certificate
6729 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006730#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6731 if( ssl->conf->f_ca_cb != NULL )
6732 {
6733 ((void) rs_ctx);
6734 have_ca_chain = 1;
6735
6736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006737 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006738 chain,
6739 ssl->conf->f_ca_cb,
6740 ssl->conf->p_ca_cb,
6741 ssl->conf->cert_profile,
6742 ssl->hostname,
6743 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006744 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006745 }
6746 else
6747#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6748 {
6749 mbedtls_x509_crt *ca_chain;
6750 mbedtls_x509_crl *ca_crl;
6751
6752#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6753 if( ssl->handshake->sni_ca_chain != NULL )
6754 {
6755 ca_chain = ssl->handshake->sni_ca_chain;
6756 ca_crl = ssl->handshake->sni_ca_crl;
6757 }
6758 else
6759#endif
6760 {
6761 ca_chain = ssl->conf->ca_chain;
6762 ca_crl = ssl->conf->ca_crl;
6763 }
6764
6765 if( ca_chain != NULL )
6766 have_ca_chain = 1;
6767
6768 ret = mbedtls_x509_crt_verify_restartable(
6769 chain,
6770 ca_chain, ca_crl,
6771 ssl->conf->cert_profile,
6772 ssl->hostname,
6773 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006774 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006775 }
Hanno Becker68636192019-02-05 14:36:34 +00006776
6777 if( ret != 0 )
6778 {
6779 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6780 }
6781
6782#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6783 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6784 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6785#endif
6786
6787 /*
6788 * Secondary checks: always done, but change 'ret' only if it was 0
6789 */
6790
6791#if defined(MBEDTLS_ECP_C)
6792 {
6793 const mbedtls_pk_context *pk = &chain->pk;
6794
6795 /* If certificate uses an EC key, make sure the curve is OK */
6796 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6797 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6798 {
6799 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6800
6801 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6802 if( ret == 0 )
6803 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6804 }
6805 }
6806#endif /* MBEDTLS_ECP_C */
6807
6808 if( mbedtls_ssl_check_cert_usage( chain,
6809 ciphersuite_info,
6810 ! ssl->conf->endpoint,
6811 &ssl->session_negotiate->verify_result ) != 0 )
6812 {
6813 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6814 if( ret == 0 )
6815 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6816 }
6817
6818 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6819 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6820 * with details encoded in the verification flags. All other kinds
6821 * of error codes, including those from the user provided f_vrfy
6822 * functions, are treated as fatal and lead to a failure of
6823 * ssl_parse_certificate even if verification was optional. */
6824 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6825 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6826 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6827 {
6828 ret = 0;
6829 }
6830
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006831 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006832 {
6833 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6834 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6835 }
6836
6837 if( ret != 0 )
6838 {
6839 uint8_t alert;
6840
6841 /* The certificate may have been rejected for several reasons.
6842 Pick one and send the corresponding alert. Which alert to send
6843 may be a subject of debate in some cases. */
6844 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6845 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6846 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6847 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6848 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6849 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6850 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6851 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6852 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6853 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6854 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6855 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6856 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6857 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6858 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6859 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6860 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6861 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6862 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6863 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6864 else
6865 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6866 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6867 alert );
6868 }
6869
6870#if defined(MBEDTLS_DEBUG_C)
6871 if( ssl->session_negotiate->verify_result != 0 )
6872 {
6873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6874 ssl->session_negotiate->verify_result ) );
6875 }
6876 else
6877 {
6878 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6879 }
6880#endif /* MBEDTLS_DEBUG_C */
6881
6882 return( ret );
6883}
6884
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006885#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6886static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6887 unsigned char *start, size_t len )
6888{
6889 int ret;
6890 /* Remember digest of the peer's end-CRT. */
6891 ssl->session_negotiate->peer_cert_digest =
6892 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6893 if( ssl->session_negotiate->peer_cert_digest == NULL )
6894 {
6895 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6896 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6897 mbedtls_ssl_send_alert_message( ssl,
6898 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6899 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6900
6901 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6902 }
6903
6904 ret = mbedtls_md( mbedtls_md_info_from_type(
6905 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6906 start, len,
6907 ssl->session_negotiate->peer_cert_digest );
6908
6909 ssl->session_negotiate->peer_cert_digest_type =
6910 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6911 ssl->session_negotiate->peer_cert_digest_len =
6912 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6913
6914 return( ret );
6915}
6916
6917static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6918 unsigned char *start, size_t len )
6919{
6920 unsigned char *end = start + len;
6921 int ret;
6922
6923 /* Make a copy of the peer's raw public key. */
6924 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6925 ret = mbedtls_pk_parse_subpubkey( &start, end,
6926 &ssl->handshake->peer_pubkey );
6927 if( ret != 0 )
6928 {
6929 /* We should have parsed the public key before. */
6930 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6931 }
6932
6933 return( 0 );
6934}
6935#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6936
Hanno Becker68636192019-02-05 14:36:34 +00006937int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6938{
6939 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006940 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006941#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6942 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6943 ? ssl->handshake->sni_authmode
6944 : ssl->conf->authmode;
6945#else
6946 const int authmode = ssl->conf->authmode;
6947#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006948 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006949 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006950
6951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6952
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006953 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6954 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006955 {
6956 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006957 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006958 }
6959
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006960#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6961 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006962 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006963 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006964 chain = ssl->handshake->ecrs_peer_cert;
6965 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006966 goto crt_verify;
6967 }
6968#endif
6969
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006970 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006971 {
6972 /* mbedtls_ssl_read_record may have sent an alert already. We
6973 let it decide whether to alert. */
6974 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006975 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006976 }
6977
Hanno Becker4a55f632019-02-05 12:49:06 +00006978#if defined(MBEDTLS_SSL_SRV_C)
6979 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6980 {
6981 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006982
6983 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006984 ret = 0;
6985 else
6986 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006987
Hanno Becker6bdfab22019-02-05 13:11:17 +00006988 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006989 }
6990#endif /* MBEDTLS_SSL_SRV_C */
6991
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006992 /* Clear existing peer CRT structure in case we tried to
6993 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006994 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006995
6996 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6997 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006998 {
6999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
7000 sizeof( mbedtls_x509_crt ) ) );
7001 mbedtls_ssl_send_alert_message( ssl,
7002 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7003 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00007004
Hanno Becker3dad3112019-02-05 17:19:52 +00007005 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7006 goto exit;
7007 }
7008 mbedtls_x509_crt_init( chain );
7009
7010 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007011 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007012 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007013
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007014#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7015 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007016 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007017
7018crt_verify:
7019 if( ssl->handshake->ecrs_enabled)
7020 rs_ctx = &ssl->handshake->ecrs_ctx;
7021#endif
7022
Hanno Becker68636192019-02-05 14:36:34 +00007023 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007024 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007025 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007026 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007027
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007028#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007029 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007030 unsigned char *crt_start, *pk_start;
7031 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007032
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007033 /* We parse the CRT chain without copying, so
7034 * these pointers point into the input buffer,
7035 * and are hence still valid after freeing the
7036 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007037
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007038 crt_start = chain->raw.p;
7039 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007040
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007041 pk_start = chain->pk_raw.p;
7042 pk_len = chain->pk_raw.len;
7043
7044 /* Free the CRT structures before computing
7045 * digest and copying the peer's public key. */
7046 mbedtls_x509_crt_free( chain );
7047 mbedtls_free( chain );
7048 chain = NULL;
7049
7050 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007051 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007052 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007053
7054 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7055 if( ret != 0 )
7056 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007057 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007058#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7059 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007060 ssl->session_negotiate->peer_cert = chain;
7061 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007062#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007064 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007065
Hanno Becker6bdfab22019-02-05 13:11:17 +00007066exit:
7067
Hanno Becker3dad3112019-02-05 17:19:52 +00007068 if( ret == 0 )
7069 ssl->state++;
7070
7071#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7072 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7073 {
7074 ssl->handshake->ecrs_peer_cert = chain;
7075 chain = NULL;
7076 }
7077#endif
7078
7079 if( chain != NULL )
7080 {
7081 mbedtls_x509_crt_free( chain );
7082 mbedtls_free( chain );
7083 }
7084
Paul Bakker5121ce52009-01-03 21:22:43 +00007085 return( ret );
7086}
Hanno Becker21489932019-02-05 13:20:55 +00007087#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007090{
7091 int ret;
7092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007093 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007095 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007096 ssl->out_msglen = 1;
7097 ssl->out_msg[0] = 1;
7098
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 ssl->state++;
7100
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007101 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007102 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007103 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007104 return( ret );
7105 }
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007108
7109 return( 0 );
7110}
7111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007113{
7114 int ret;
7115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007117
Hanno Becker327c93b2018-08-15 13:56:18 +01007118 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007119 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007121 return( ret );
7122 }
7123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007127 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7128 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007129 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007130 }
7131
Hanno Beckere678eaa2018-08-21 14:57:46 +01007132 /* CCS records are only accepted if they have length 1 and content '1',
7133 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007134
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007135 /*
7136 * Switch to our negotiated transform and session parameters for inbound
7137 * data.
7138 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007140 ssl->transform_in = ssl->transform_negotiate;
7141 ssl->session_in = ssl->session_negotiate;
7142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007144 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007147 ssl_dtls_replay_reset( ssl );
7148#endif
7149
7150 /* Increment epoch */
7151 if( ++ssl->in_epoch == 0 )
7152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007154 /* This is highly unlikely to happen for legitimate reasons, so
7155 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007156 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007157 }
7158 }
7159 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007161 memset( ssl->in_ctr, 0, 8 );
7162
Hanno Becker79594fd2019-05-08 09:38:41 +01007163 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007164
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7166 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007169 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007170 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007171 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7172 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007173 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007174 }
7175 }
7176#endif
7177
Paul Bakker5121ce52009-01-03 21:22:43 +00007178 ssl->state++;
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007181
7182 return( 0 );
7183}
7184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7186 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007187{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007188 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007190#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7191 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7192 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007193 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007194 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007195#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007196#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7197#if defined(MBEDTLS_SHA512_C)
7198 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007199 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7200 else
7201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#if defined(MBEDTLS_SHA256_C)
7203 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007204 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007205 else
7206#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007207#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007210 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007211 }
Paul Bakker380da532012-04-18 16:10:25 +00007212}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007214void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007215{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7217 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007218 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7219 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007220#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007221#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7222#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007224 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007225 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7226#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007227 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007228#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007230#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007231#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007232 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007233 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007234#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007235 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007236#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007237#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007238#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007239}
7240
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007241static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007242 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007243{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7245 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007246 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7247 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007248#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007249#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7250#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007251#if defined(MBEDTLS_USE_PSA_CRYPTO)
7252 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7253#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007254 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007255#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007256#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007258#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007259 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007260#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007261 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007262#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007263#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007264#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007265}
7266
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007267#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7268 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7269static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007270 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007271{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007272 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7273 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007274}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007275#endif
Paul Bakker380da532012-04-18 16:10:25 +00007276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007277#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7278#if defined(MBEDTLS_SHA256_C)
7279static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007280 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007281{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007282#if defined(MBEDTLS_USE_PSA_CRYPTO)
7283 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7284#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007285 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007286#endif
Paul Bakker380da532012-04-18 16:10:25 +00007287}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007288#endif
Paul Bakker380da532012-04-18 16:10:25 +00007289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007290#if defined(MBEDTLS_SHA512_C)
7291static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007292 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007293{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007294#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007295 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007296#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007297 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007298#endif
Paul Bakker380da532012-04-18 16:10:25 +00007299}
Paul Bakker769075d2012-11-24 11:26:46 +01007300#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007301#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007304static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007305 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007306{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007307 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007308 mbedtls_md5_context md5;
7309 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007310
Paul Bakker5121ce52009-01-03 21:22:43 +00007311 unsigned char padbuf[48];
7312 unsigned char md5sum[16];
7313 unsigned char sha1sum[20];
7314
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007315 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007316 if( !session )
7317 session = ssl->session;
7318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007319 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007320
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007321 mbedtls_md5_init( &md5 );
7322 mbedtls_sha1_init( &sha1 );
7323
7324 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7325 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007326
7327 /*
7328 * SSLv3:
7329 * hash =
7330 * MD5( master + pad2 +
7331 * MD5( handshake + sender + master + pad1 ) )
7332 * + SHA1( master + pad2 +
7333 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007334 */
7335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007337 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7338 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007339#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007340
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007341#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007342 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7343 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007344#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007346 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007347 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007348
Paul Bakker1ef83d62012-04-11 12:09:53 +00007349 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007351 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7352 mbedtls_md5_update_ret( &md5, session->master, 48 );
7353 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7354 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007356 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7357 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7358 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7359 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007360
Paul Bakker1ef83d62012-04-11 12:09:53 +00007361 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007362
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007363 mbedtls_md5_starts_ret( &md5 );
7364 mbedtls_md5_update_ret( &md5, session->master, 48 );
7365 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7366 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7367 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007368
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007369 mbedtls_sha1_starts_ret( &sha1 );
7370 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7371 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7372 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7373 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007375 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007376
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007377 mbedtls_md5_free( &md5 );
7378 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007379
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007380 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7381 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7382 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007383
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007384 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007388#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007389static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007390 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007391{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007392 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007393 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007394 mbedtls_md5_context md5;
7395 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007396 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007399 if( !session )
7400 session = ssl->session;
7401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007403
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007404 mbedtls_md5_init( &md5 );
7405 mbedtls_sha1_init( &sha1 );
7406
7407 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7408 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007409
Paul Bakker1ef83d62012-04-11 12:09:53 +00007410 /*
7411 * TLSv1:
7412 * hash = PRF( master, finished_label,
7413 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7414 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007417 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7418 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007419#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007422 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7423 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007424#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007426 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007427 ? "client finished"
7428 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007429
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007430 mbedtls_md5_finish_ret( &md5, padbuf );
7431 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007433 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007434 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007435
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007436 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007437
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007438 mbedtls_md5_free( &md5 );
7439 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007440
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007441 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007443 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007444}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007447#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7448#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007449static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007450 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007451{
7452 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007453 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007454 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007455#if defined(MBEDTLS_USE_PSA_CRYPTO)
7456 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007457 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007458 psa_status_t status;
7459#else
7460 mbedtls_sha256_context sha256;
7461#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007462
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007463 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007464 if( !session )
7465 session = ssl->session;
7466
Andrzej Kurekeb342242019-01-29 09:14:33 -05007467 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7468 ? "client finished"
7469 : "server finished";
7470
7471#if defined(MBEDTLS_USE_PSA_CRYPTO)
7472 sha256_psa = psa_hash_operation_init();
7473
7474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7475
7476 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7477 if( status != PSA_SUCCESS )
7478 {
7479 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7480 return;
7481 }
7482
7483 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7484 if( status != PSA_SUCCESS )
7485 {
7486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7487 return;
7488 }
7489 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7490#else
7491
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007492 mbedtls_sha256_init( &sha256 );
7493
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007494 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007495
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007496 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007497
7498 /*
7499 * TLSv1.2:
7500 * hash = PRF( master, finished_label,
7501 * Hash( handshake ) )[0.11]
7502 */
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504#if !defined(MBEDTLS_SHA256_ALT)
7505 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007506 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007507#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007508
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007509 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007510 mbedtls_sha256_free( &sha256 );
7511#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007512
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007513 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007514 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007515
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007517
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007518 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007519
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007520 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007521}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007522#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007524#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007525static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007527{
7528 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007529 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007530 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007531#if defined(MBEDTLS_USE_PSA_CRYPTO)
7532 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007533 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007534 psa_status_t status;
7535#else
7536 mbedtls_sha512_context sha512;
7537#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007538
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007539 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007540 if( !session )
7541 session = ssl->session;
7542
Andrzej Kurekeb342242019-01-29 09:14:33 -05007543 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7544 ? "client finished"
7545 : "server finished";
7546
7547#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007548 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007549
7550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7551
Andrzej Kurek972fba52019-01-30 03:29:12 -05007552 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007553 if( status != PSA_SUCCESS )
7554 {
7555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7556 return;
7557 }
7558
Andrzej Kurek972fba52019-01-30 03:29:12 -05007559 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007560 if( status != PSA_SUCCESS )
7561 {
7562 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7563 return;
7564 }
7565 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7566#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007567 mbedtls_sha512_init( &sha512 );
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007570
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007571 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007572
7573 /*
7574 * TLSv1.2:
7575 * hash = PRF( master, finished_label,
7576 * Hash( handshake ) )[0.11]
7577 */
7578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007579#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007580 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7581 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007582#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007583
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007584 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007585 mbedtls_sha512_free( &sha512 );
7586#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007587
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007588 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007589 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007590
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007591 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007592
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007593 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007596}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597#endif /* MBEDTLS_SHA512_C */
7598#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007599
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007600static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007601{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007603
7604 /*
7605 * Free our handshake params
7606 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007607 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007609 ssl->handshake = NULL;
7610
7611 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007612 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007613 */
7614 if( ssl->transform )
7615 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007616 mbedtls_ssl_transform_free( ssl->transform );
7617 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007618 }
7619 ssl->transform = ssl->transform_negotiate;
7620 ssl->transform_negotiate = NULL;
7621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007622 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007623}
7624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007625void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007626{
7627 int resume = ssl->handshake->resume;
7628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007631#if defined(MBEDTLS_SSL_RENEGOTIATION)
7632 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007634 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007635 ssl->renego_records_seen = 0;
7636 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007637#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007638
7639 /*
7640 * Free the previous session and switch in the current one
7641 */
Paul Bakker0a597072012-09-25 21:55:46 +00007642 if( ssl->session )
7643 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007644#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007645 /* RFC 7366 3.1: keep the EtM state */
7646 ssl->session_negotiate->encrypt_then_mac =
7647 ssl->session->encrypt_then_mac;
7648#endif
7649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007650 mbedtls_ssl_session_free( ssl->session );
7651 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007652 }
7653 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007654 ssl->session_negotiate = NULL;
7655
Paul Bakker0a597072012-09-25 21:55:46 +00007656 /*
7657 * Add cache entry
7658 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007659 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007660 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007661 resume == 0 )
7662 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007663 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007665 }
Paul Bakker0a597072012-09-25 21:55:46 +00007666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007667#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007668 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007669 ssl->handshake->flight != NULL )
7670 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007671 /* Cancel handshake timer */
7672 ssl_set_timer( ssl, 0 );
7673
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007674 /* Keep last flight around in case we need to resend it:
7675 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007676 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007677 }
7678 else
7679#endif
7680 ssl_handshake_wrapup_free_hs_transform( ssl );
7681
Paul Bakker48916f92012-09-16 19:57:18 +00007682 ssl->state++;
7683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007684 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007685}
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007688{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007689 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007690
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007692
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007693 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007694
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007695 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007696
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007697 /*
7698 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7699 * may define some other value. Currently (early 2016), no defined
7700 * ciphersuite does this (and this is unlikely to change as activity has
7701 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7702 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007703 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007706 ssl->verify_data_len = hash_len;
7707 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007708#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007709
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7712 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007713
7714 /*
7715 * In case of session resuming, invert the client and server
7716 * ChangeCipherSpec messages order.
7717 */
Paul Bakker0a597072012-09-25 21:55:46 +00007718 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007720#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007721 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007722 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007723#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007724#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007725 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007726 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007727#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007728 }
7729 else
7730 ssl->state++;
7731
Paul Bakker48916f92012-09-16 19:57:18 +00007732 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007733 * Switch to our negotiated transform and session parameters for outbound
7734 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007735 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007736 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007737
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007739 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007740 {
7741 unsigned char i;
7742
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007743 /* Remember current epoch settings for resending */
7744 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007745 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007746
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007747 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007748 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007749
7750 /* Increment epoch */
7751 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007752 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007753 break;
7754
7755 /* The loop goes to its end iff the counter is wrapping */
7756 if( i == 0 )
7757 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7759 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007760 }
7761 }
7762 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007763#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007764 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007765
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007766 ssl->transform_out = ssl->transform_negotiate;
7767 ssl->session_out = ssl->session_negotiate;
7768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7770 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007771 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7775 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007776 }
7777 }
7778#endif
7779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007780#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007781 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007782 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007783#endif
7784
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007785 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007786 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007787 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007788 return( ret );
7789 }
7790
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007791#if defined(MBEDTLS_SSL_PROTO_DTLS)
7792 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7793 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7794 {
7795 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7796 return( ret );
7797 }
7798#endif
7799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007801
7802 return( 0 );
7803}
7804
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007805#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007806#define SSL_MAX_HASH_LEN 36
7807#else
7808#define SSL_MAX_HASH_LEN 12
7809#endif
7810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007812{
Paul Bakker23986e52011-04-24 08:57:21 +00007813 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007814 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007815 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007816
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007817 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007818
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007819 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007820
Hanno Becker327c93b2018-08-15 13:56:18 +01007821 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007822 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007823 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007824 return( ret );
7825 }
7826
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007829 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007830 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7831 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007832 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007833 }
7834
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007835 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007836#if defined(MBEDTLS_SSL_PROTO_SSL3)
7837 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007838 hash_len = 36;
7839 else
7840#endif
7841 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7844 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007846 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007847 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7848 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007849 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007850 }
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007853 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007854 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007855 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007856 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7857 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007858 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007859 }
7860
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007862 ssl->verify_data_len = hash_len;
7863 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007864#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007865
Paul Bakker0a597072012-09-25 21:55:46 +00007866 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007868#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007869 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007870 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007871#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007872#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007873 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007874 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007875#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007876 }
7877 else
7878 ssl->state++;
7879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007881 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007882 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007883#endif
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007886
7887 return( 0 );
7888}
7889
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007890static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007891{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007892 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007894#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7895 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7896 mbedtls_md5_init( &handshake->fin_md5 );
7897 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007898 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7899 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007900#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007901#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7902#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007903#if defined(MBEDTLS_USE_PSA_CRYPTO)
7904 handshake->fin_sha256_psa = psa_hash_operation_init();
7905 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7906#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007907 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007908 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007909#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007910#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007912#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007913 handshake->fin_sha384_psa = psa_hash_operation_init();
7914 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007915#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007916 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007917 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007918#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007919#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007920#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007921
7922 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007923
7924#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7925 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7926 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7927#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007929#if defined(MBEDTLS_DHM_C)
7930 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007931#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007932#if defined(MBEDTLS_ECDH_C)
7933 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007934#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007935#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007936 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007937#if defined(MBEDTLS_SSL_CLI_C)
7938 handshake->ecjpake_cache = NULL;
7939 handshake->ecjpake_cache_len = 0;
7940#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007941#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007942
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007943#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007944 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007945#endif
7946
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007947#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7948 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7949#endif
Hanno Becker75173122019-02-06 16:18:31 +00007950
7951#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7952 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7953 mbedtls_pk_init( &handshake->peer_pubkey );
7954#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007955}
7956
Hanno Beckera18d1322018-01-03 14:27:32 +00007957void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007958{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007959 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007961 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7962 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007963
Hanno Beckerd56ed242018-01-03 15:32:51 +00007964#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965 mbedtls_md_init( &transform->md_ctx_enc );
7966 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007967#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968}
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007971{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007972 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007973}
7974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007975static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007976{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007977 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007978 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007979 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007980 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007981 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007982 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007983 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007984
7985 /*
7986 * Either the pointers are now NULL or cleared properly and can be freed.
7987 * Now allocate missing structures.
7988 */
7989 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007991 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007992 }
Paul Bakker48916f92012-09-16 19:57:18 +00007993
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007994 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007995 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007996 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007997 }
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Paul Bakker82788fb2014-10-20 13:59:19 +02007999 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008000 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008001 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02008002 }
Paul Bakker48916f92012-09-16 19:57:18 +00008003
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008004 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008005 if( ssl->handshake == NULL ||
8006 ssl->transform_negotiate == NULL ||
8007 ssl->session_negotiate == NULL )
8008 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008009 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008011 mbedtls_free( ssl->handshake );
8012 mbedtls_free( ssl->transform_negotiate );
8013 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008014
8015 ssl->handshake = NULL;
8016 ssl->transform_negotiate = NULL;
8017 ssl->session_negotiate = NULL;
8018
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008019 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008020 }
8021
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008022 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008023 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008024 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008025 ssl_handshake_params_init( ssl->handshake );
8026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008027#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008028 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8029 {
8030 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008031
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008032 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8033 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8034 else
8035 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008036
8037 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008038 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008039#endif
8040
Paul Bakker48916f92012-09-16 19:57:18 +00008041 return( 0 );
8042}
8043
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008044#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008045/* Dummy cookie callbacks for defaults */
8046static int ssl_cookie_write_dummy( void *ctx,
8047 unsigned char **p, unsigned char *end,
8048 const unsigned char *cli_id, size_t cli_id_len )
8049{
8050 ((void) ctx);
8051 ((void) p);
8052 ((void) end);
8053 ((void) cli_id);
8054 ((void) cli_id_len);
8055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008056 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008057}
8058
8059static int ssl_cookie_check_dummy( void *ctx,
8060 const unsigned char *cookie, size_t cookie_len,
8061 const unsigned char *cli_id, size_t cli_id_len )
8062{
8063 ((void) ctx);
8064 ((void) cookie);
8065 ((void) cookie_len);
8066 ((void) cli_id);
8067 ((void) cli_id_len);
8068
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008069 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008070}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008071#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008072
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008073/* Once ssl->out_hdr as the address of the beginning of the
8074 * next outgoing record is set, deduce the other pointers.
8075 *
8076 * Note: For TLS, we save the implicit record sequence number
8077 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8078 * and the caller has to make sure there's space for this.
8079 */
8080
8081static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8082 mbedtls_ssl_transform *transform )
8083{
8084#if defined(MBEDTLS_SSL_PROTO_DTLS)
8085 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8086 {
8087 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008088#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008089 ssl->out_cid = ssl->out_ctr + 8;
8090 ssl->out_len = ssl->out_cid;
8091 if( transform != NULL )
8092 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008093#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008094 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008095#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008096 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008097 }
8098 else
8099#endif
8100 {
8101 ssl->out_ctr = ssl->out_hdr - 8;
8102 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008103#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008104 ssl->out_cid = ssl->out_len;
8105#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008106 ssl->out_iv = ssl->out_hdr + 5;
8107 }
8108
8109 /* Adjust out_msg to make space for explicit IV, if used. */
8110 if( transform != NULL &&
8111 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8112 {
8113 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8114 }
8115 else
8116 ssl->out_msg = ssl->out_iv;
8117}
8118
8119/* Once ssl->in_hdr as the address of the beginning of the
8120 * next incoming record is set, deduce the other pointers.
8121 *
8122 * Note: For TLS, we save the implicit record sequence number
8123 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8124 * and the caller has to make sure there's space for this.
8125 */
8126
Hanno Becker79594fd2019-05-08 09:38:41 +01008127static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008128{
Hanno Becker79594fd2019-05-08 09:38:41 +01008129 /* This function sets the pointers to match the case
8130 * of unprotected TLS/DTLS records, with both ssl->in_iv
8131 * and ssl->in_msg pointing to the beginning of the record
8132 * content.
8133 *
8134 * When decrypting a protected record, ssl->in_msg
8135 * will be shifted to point to the beginning of the
8136 * record plaintext.
8137 */
8138
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008139#if defined(MBEDTLS_SSL_PROTO_DTLS)
8140 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8141 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008142 /* This sets the header pointers to match records
8143 * without CID. When we receive a record containing
8144 * a CID, the fields are shifted accordingly in
8145 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008146 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008147#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008148 ssl->in_cid = ssl->in_ctr + 8;
8149 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008150#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008151 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008152#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008153 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008154 }
8155 else
8156#endif
8157 {
8158 ssl->in_ctr = ssl->in_hdr - 8;
8159 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008161 ssl->in_cid = ssl->in_len;
8162#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008163 ssl->in_iv = ssl->in_hdr + 5;
8164 }
8165
Hanno Becker79594fd2019-05-08 09:38:41 +01008166 /* This will be adjusted at record decryption time. */
8167 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008168}
8169
Paul Bakker5121ce52009-01-03 21:22:43 +00008170/*
8171 * Initialize an SSL context
8172 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008173void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8174{
8175 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8176}
8177
8178/*
8179 * Setup an SSL context
8180 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008181
8182static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8183{
8184 /* Set the incoming and outgoing record pointers. */
8185#if defined(MBEDTLS_SSL_PROTO_DTLS)
8186 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8187 {
8188 ssl->out_hdr = ssl->out_buf;
8189 ssl->in_hdr = ssl->in_buf;
8190 }
8191 else
8192#endif /* MBEDTLS_SSL_PROTO_DTLS */
8193 {
8194 ssl->out_hdr = ssl->out_buf + 8;
8195 ssl->in_hdr = ssl->in_buf + 8;
8196 }
8197
8198 /* Derive other internal pointers. */
8199 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008200 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008201}
8202
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008203int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008204 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008205{
Paul Bakker48916f92012-09-16 19:57:18 +00008206 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008207
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008208 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008209
8210 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008211 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008212 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008213
8214 /* Set to NULL in case of an error condition */
8215 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008216
Angus Grattond8213d02016-05-25 20:56:48 +10008217 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8218 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008219 {
Angus Grattond8213d02016-05-25 20:56:48 +10008220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008221 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008222 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008223 }
8224
8225 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8226 if( ssl->out_buf == NULL )
8227 {
8228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008229 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008230 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008231 }
8232
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008233 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008234
Paul Bakker48916f92012-09-16 19:57:18 +00008235 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008236 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008237
8238 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008239
8240error:
8241 mbedtls_free( ssl->in_buf );
8242 mbedtls_free( ssl->out_buf );
8243
8244 ssl->conf = NULL;
8245
8246 ssl->in_buf = NULL;
8247 ssl->out_buf = NULL;
8248
8249 ssl->in_hdr = NULL;
8250 ssl->in_ctr = NULL;
8251 ssl->in_len = NULL;
8252 ssl->in_iv = NULL;
8253 ssl->in_msg = NULL;
8254
8255 ssl->out_hdr = NULL;
8256 ssl->out_ctr = NULL;
8257 ssl->out_len = NULL;
8258 ssl->out_iv = NULL;
8259 ssl->out_msg = NULL;
8260
k-stachowiak9f7798e2018-07-31 16:52:32 +02008261 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008262}
8263
8264/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008265 * Reset an initialized and used SSL context for re-use while retaining
8266 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008267 *
8268 * If partial is non-zero, keep data in the input buffer and client ID.
8269 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008270 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008271static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008272{
Paul Bakker48916f92012-09-16 19:57:18 +00008273 int ret;
8274
Hanno Becker7e772132018-08-10 12:38:21 +01008275#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8276 !defined(MBEDTLS_SSL_SRV_C)
8277 ((void) partial);
8278#endif
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008281
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008282 /* Cancel any possibly running timer */
8283 ssl_set_timer( ssl, 0 );
8284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285#if defined(MBEDTLS_SSL_RENEGOTIATION)
8286 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008287 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008288
8289 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008290 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8291 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008292#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008293 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008294
Paul Bakker7eb013f2011-10-06 12:37:39 +00008295 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008296 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008297
8298 ssl->in_msgtype = 0;
8299 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008301 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008302 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008303#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008304#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008305 ssl_dtls_replay_reset( ssl );
8306#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008307
8308 ssl->in_hslen = 0;
8309 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008310
8311 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008312
8313 ssl->out_msgtype = 0;
8314 ssl->out_msglen = 0;
8315 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008316#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8317 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008318 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008319#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008320
Hanno Becker19859472018-08-06 09:40:20 +01008321 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8322
Paul Bakker48916f92012-09-16 19:57:18 +00008323 ssl->transform_in = NULL;
8324 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008325
Hanno Becker78640902018-08-13 16:35:15 +01008326 ssl->session_in = NULL;
8327 ssl->session_out = NULL;
8328
Angus Grattond8213d02016-05-25 20:56:48 +10008329 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008330
8331#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008332 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008333#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8334 {
8335 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008336 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008337 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008339#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8340 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008341 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008342 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8343 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008344 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008345 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8346 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008347 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008348 }
8349#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008350
Paul Bakker48916f92012-09-16 19:57:18 +00008351 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008352 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008353 mbedtls_ssl_transform_free( ssl->transform );
8354 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008355 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008356 }
Paul Bakker48916f92012-09-16 19:57:18 +00008357
Paul Bakkerc0463502013-02-14 11:19:38 +01008358 if( ssl->session )
8359 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360 mbedtls_ssl_session_free( ssl->session );
8361 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008362 ssl->session = NULL;
8363 }
8364
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008365#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008366 ssl->alpn_chosen = NULL;
8367#endif
8368
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008369#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008370#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008371 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008372#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008373 {
8374 mbedtls_free( ssl->cli_id );
8375 ssl->cli_id = NULL;
8376 ssl->cli_id_len = 0;
8377 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008378#endif
8379
Paul Bakker48916f92012-09-16 19:57:18 +00008380 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8381 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008382
8383 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008384}
8385
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008386/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008387 * Reset an initialized and used SSL context for re-use while retaining
8388 * all application-set variables, function pointers and data.
8389 */
8390int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8391{
8392 return( ssl_session_reset_int( ssl, 0 ) );
8393}
8394
8395/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008396 * SSL set accessors
8397 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008401}
8402
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008403void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008404{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008405 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008406}
8407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008408#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008409void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008410{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008411 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008412}
8413#endif
8414
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008415#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008416void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008417{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008418 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008419}
8420#endif
8421
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008422#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008423
Hanno Becker1841b0a2018-08-24 11:13:57 +01008424void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8425 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008426{
8427 ssl->disable_datagram_packing = !allow_packing;
8428}
8429
8430void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8431 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008432{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008433 conf->hs_timeout_min = min;
8434 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008435}
8436#endif
8437
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008438void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008439{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008440 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008441}
8442
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008444void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008445 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008446 void *p_vrfy )
8447{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008448 conf->f_vrfy = f_vrfy;
8449 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008450}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008451#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008452
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008453void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008454 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008455 void *p_rng )
8456{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008457 conf->f_rng = f_rng;
8458 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008459}
8460
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008461void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008462 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008463 void *p_dbg )
8464{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008465 conf->f_dbg = f_dbg;
8466 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008467}
8468
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008469void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008470 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008471 mbedtls_ssl_send_t *f_send,
8472 mbedtls_ssl_recv_t *f_recv,
8473 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008474{
8475 ssl->p_bio = p_bio;
8476 ssl->f_send = f_send;
8477 ssl->f_recv = f_recv;
8478 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008479}
8480
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008481#if defined(MBEDTLS_SSL_PROTO_DTLS)
8482void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8483{
8484 ssl->mtu = mtu;
8485}
8486#endif
8487
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008488void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008489{
8490 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008491}
8492
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008493void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8494 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008495 mbedtls_ssl_set_timer_t *f_set_timer,
8496 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008497{
8498 ssl->p_timer = p_timer;
8499 ssl->f_set_timer = f_set_timer;
8500 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008501
8502 /* Make sure we start with no timer running */
8503 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008504}
8505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008506#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008507void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008508 void *p_cache,
8509 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8510 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008511{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008512 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008513 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008514 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008515}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008516#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008517
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008518#if defined(MBEDTLS_SSL_CLI_C)
8519int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008520{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008521 int ret;
8522
8523 if( ssl == NULL ||
8524 session == NULL ||
8525 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008526 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008528 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008529 }
8530
Hanno Becker52055ae2019-02-06 14:30:46 +00008531 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8532 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008533 return( ret );
8534
Paul Bakker0a597072012-09-25 21:55:46 +00008535 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008536
8537 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008538}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008539#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008540
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008541void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008542 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008543{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008544 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8545 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8546 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8547 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008548}
8549
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008550void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008551 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008552 int major, int minor )
8553{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008554 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008555 return;
8556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008557 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008558 return;
8559
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008560 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008561}
8562
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008563#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008564void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008565 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008566{
8567 conf->cert_profile = profile;
8568}
8569
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008570/* Append a new keycert entry to a (possibly empty) list */
8571static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8572 mbedtls_x509_crt *cert,
8573 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008574{
niisato8ee24222018-06-25 19:05:48 +09008575 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008576
niisato8ee24222018-06-25 19:05:48 +09008577 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8578 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008579 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008580
niisato8ee24222018-06-25 19:05:48 +09008581 new_cert->cert = cert;
8582 new_cert->key = key;
8583 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008584
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008585 /* Update head is the list was null, else add to the end */
8586 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008587 {
niisato8ee24222018-06-25 19:05:48 +09008588 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008589 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008590 else
8591 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008592 mbedtls_ssl_key_cert *cur = *head;
8593 while( cur->next != NULL )
8594 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008595 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008596 }
8597
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008598 return( 0 );
8599}
8600
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008601int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008602 mbedtls_x509_crt *own_cert,
8603 mbedtls_pk_context *pk_key )
8604{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008605 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008606}
8607
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008608void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008609 mbedtls_x509_crt *ca_chain,
8610 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008611{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008612 conf->ca_chain = ca_chain;
8613 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008614
8615#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8616 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8617 * cannot be used together. */
8618 conf->f_ca_cb = NULL;
8619 conf->p_ca_cb = NULL;
8620#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008621}
Hanno Becker5adaad92019-03-27 16:54:37 +00008622
8623#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8624void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008625 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008626 void *p_ca_cb )
8627{
8628 conf->f_ca_cb = f_ca_cb;
8629 conf->p_ca_cb = p_ca_cb;
8630
8631 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8632 * cannot be used together. */
8633 conf->ca_chain = NULL;
8634 conf->ca_crl = NULL;
8635}
8636#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008637#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008638
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008639#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8640int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8641 mbedtls_x509_crt *own_cert,
8642 mbedtls_pk_context *pk_key )
8643{
8644 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8645 own_cert, pk_key ) );
8646}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008647
8648void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8649 mbedtls_x509_crt *ca_chain,
8650 mbedtls_x509_crl *ca_crl )
8651{
8652 ssl->handshake->sni_ca_chain = ca_chain;
8653 ssl->handshake->sni_ca_crl = ca_crl;
8654}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008655
8656void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8657 int authmode )
8658{
8659 ssl->handshake->sni_authmode = authmode;
8660}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008661#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8662
Hanno Becker8927c832019-04-03 12:52:50 +01008663#if defined(MBEDTLS_X509_CRT_PARSE_C)
8664void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8665 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8666 void *p_vrfy )
8667{
8668 ssl->f_vrfy = f_vrfy;
8669 ssl->p_vrfy = p_vrfy;
8670}
8671#endif
8672
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008673#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008674/*
8675 * Set EC J-PAKE password for current handshake
8676 */
8677int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8678 const unsigned char *pw,
8679 size_t pw_len )
8680{
8681 mbedtls_ecjpake_role role;
8682
Janos Follath8eb64132016-06-03 15:40:57 +01008683 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008684 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8685
8686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8687 role = MBEDTLS_ECJPAKE_SERVER;
8688 else
8689 role = MBEDTLS_ECJPAKE_CLIENT;
8690
8691 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8692 role,
8693 MBEDTLS_MD_SHA256,
8694 MBEDTLS_ECP_DP_SECP256R1,
8695 pw, pw_len ) );
8696}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008697#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008699#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008700
8701static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8702{
8703 /* Remove reference to existing PSK, if any. */
8704#if defined(MBEDTLS_USE_PSA_CRYPTO)
8705 if( conf->psk_opaque != 0 )
8706 {
8707 /* The maintenance of the PSK key slot is the
8708 * user's responsibility. */
8709 conf->psk_opaque = 0;
8710 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008711 /* This and the following branch should never
8712 * be taken simultaenously as we maintain the
8713 * invariant that raw and opaque PSKs are never
8714 * configured simultaneously. As a safeguard,
8715 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008716#endif /* MBEDTLS_USE_PSA_CRYPTO */
8717 if( conf->psk != NULL )
8718 {
8719 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8720
8721 mbedtls_free( conf->psk );
8722 conf->psk = NULL;
8723 conf->psk_len = 0;
8724 }
8725
8726 /* Remove reference to PSK identity, if any. */
8727 if( conf->psk_identity != NULL )
8728 {
8729 mbedtls_free( conf->psk_identity );
8730 conf->psk_identity = NULL;
8731 conf->psk_identity_len = 0;
8732 }
8733}
8734
Hanno Becker7390c712018-11-15 13:33:04 +00008735/* This function assumes that PSK identity in the SSL config is unset.
8736 * It checks that the provided identity is well-formed and attempts
8737 * to make a copy of it in the SSL config.
8738 * On failure, the PSK identity in the config remains unset. */
8739static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8740 unsigned char const *psk_identity,
8741 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008742{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008743 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008744 if( psk_identity == NULL ||
8745 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008746 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008747 {
8748 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8749 }
8750
Hanno Becker7390c712018-11-15 13:33:04 +00008751 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8752 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008753 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008754
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008755 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008756 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008757
8758 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008759}
8760
Hanno Becker7390c712018-11-15 13:33:04 +00008761int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8762 const unsigned char *psk, size_t psk_len,
8763 const unsigned char *psk_identity, size_t psk_identity_len )
8764{
8765 int ret;
8766 /* Remove opaque/raw PSK + PSK Identity */
8767 ssl_conf_remove_psk( conf );
8768
8769 /* Check and set raw PSK */
8770 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8771 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8772 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8773 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8774 conf->psk_len = psk_len;
8775 memcpy( conf->psk, psk, conf->psk_len );
8776
8777 /* Check and set PSK Identity */
8778 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8779 if( ret != 0 )
8780 ssl_conf_remove_psk( conf );
8781
8782 return( ret );
8783}
8784
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008785static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8786{
8787#if defined(MBEDTLS_USE_PSA_CRYPTO)
8788 if( ssl->handshake->psk_opaque != 0 )
8789 {
8790 ssl->handshake->psk_opaque = 0;
8791 }
8792 else
8793#endif /* MBEDTLS_USE_PSA_CRYPTO */
8794 if( ssl->handshake->psk != NULL )
8795 {
8796 mbedtls_platform_zeroize( ssl->handshake->psk,
8797 ssl->handshake->psk_len );
8798 mbedtls_free( ssl->handshake->psk );
8799 ssl->handshake->psk_len = 0;
8800 }
8801}
8802
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008803int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8804 const unsigned char *psk, size_t psk_len )
8805{
8806 if( psk == NULL || ssl->handshake == NULL )
8807 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8808
8809 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8810 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8811
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008812 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008813
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008814 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008815 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008816
8817 ssl->handshake->psk_len = psk_len;
8818 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8819
8820 return( 0 );
8821}
8822
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008823#if defined(MBEDTLS_USE_PSA_CRYPTO)
8824int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008825 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008826 const unsigned char *psk_identity,
8827 size_t psk_identity_len )
8828{
Hanno Becker7390c712018-11-15 13:33:04 +00008829 int ret;
8830 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008831 ssl_conf_remove_psk( conf );
8832
Hanno Becker7390c712018-11-15 13:33:04 +00008833 /* Check and set opaque PSK */
8834 if( psk_slot == 0 )
8835 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008836 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008837
8838 /* Check and set PSK Identity */
8839 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8840 psk_identity_len );
8841 if( ret != 0 )
8842 ssl_conf_remove_psk( conf );
8843
8844 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008845}
8846
8847int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008848 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008849{
8850 if( psk_slot == 0 || ssl->handshake == NULL )
8851 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8852
8853 ssl_remove_psk( ssl );
8854 ssl->handshake->psk_opaque = psk_slot;
8855 return( 0 );
8856}
8857#endif /* MBEDTLS_USE_PSA_CRYPTO */
8858
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008859void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008860 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008861 size_t),
8862 void *p_psk )
8863{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008864 conf->f_psk = f_psk;
8865 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008866}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008867#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008868
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008869#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008870
8871#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008872int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008873{
8874 int ret;
8875
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008876 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8877 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8878 {
8879 mbedtls_mpi_free( &conf->dhm_P );
8880 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008881 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008882 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008883
8884 return( 0 );
8885}
Hanno Becker470a8c42017-10-04 15:28:46 +01008886#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008887
Hanno Beckera90658f2017-10-04 15:29:08 +01008888int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8889 const unsigned char *dhm_P, size_t P_len,
8890 const unsigned char *dhm_G, size_t G_len )
8891{
8892 int ret;
8893
8894 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8895 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8896 {
8897 mbedtls_mpi_free( &conf->dhm_P );
8898 mbedtls_mpi_free( &conf->dhm_G );
8899 return( ret );
8900 }
8901
8902 return( 0 );
8903}
Paul Bakker5121ce52009-01-03 21:22:43 +00008904
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008905int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008906{
8907 int ret;
8908
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008909 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8910 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8911 {
8912 mbedtls_mpi_free( &conf->dhm_P );
8913 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008914 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008915 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008916
8917 return( 0 );
8918}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008919#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008920
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008921#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8922/*
8923 * Set the minimum length for Diffie-Hellman parameters
8924 */
8925void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8926 unsigned int bitlen )
8927{
8928 conf->dhm_min_bitlen = bitlen;
8929}
8930#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8931
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008932#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008933/*
8934 * Set allowed/preferred hashes for handshake signatures
8935 */
8936void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8937 const int *hashes )
8938{
8939 conf->sig_hashes = hashes;
8940}
Hanno Becker947194e2017-04-07 13:25:49 +01008941#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008942
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008943#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008944/*
8945 * Set the allowed elliptic curves
8946 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008947void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008948 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008949{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008950 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008951}
Hanno Becker947194e2017-04-07 13:25:49 +01008952#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008953
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008954#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008955int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008956{
Hanno Becker947194e2017-04-07 13:25:49 +01008957 /* Initialize to suppress unnecessary compiler warning */
8958 size_t hostname_len = 0;
8959
8960 /* Check if new hostname is valid before
8961 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008962 if( hostname != NULL )
8963 {
8964 hostname_len = strlen( hostname );
8965
8966 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8968 }
8969
8970 /* Now it's clear that we will overwrite the old hostname,
8971 * so we can free it safely */
8972
8973 if( ssl->hostname != NULL )
8974 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008975 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008976 mbedtls_free( ssl->hostname );
8977 }
8978
8979 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008980
Paul Bakker5121ce52009-01-03 21:22:43 +00008981 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008982 {
8983 ssl->hostname = NULL;
8984 }
8985 else
8986 {
8987 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008988 if( ssl->hostname == NULL )
8989 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008990
Hanno Becker947194e2017-04-07 13:25:49 +01008991 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008992
Hanno Becker947194e2017-04-07 13:25:49 +01008993 ssl->hostname[hostname_len] = '\0';
8994 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008995
8996 return( 0 );
8997}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008998#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008999
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01009000#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009001void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009002 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00009003 const unsigned char *, size_t),
9004 void *p_sni )
9005{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009006 conf->f_sni = f_sni;
9007 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009008}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009009#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009011#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009012int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009013{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009014 size_t cur_len, tot_len;
9015 const char **p;
9016
9017 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009018 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9019 * MUST NOT be truncated."
9020 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009021 */
9022 tot_len = 0;
9023 for( p = protos; *p != NULL; p++ )
9024 {
9025 cur_len = strlen( *p );
9026 tot_len += cur_len;
9027
9028 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009029 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009030 }
9031
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009032 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009033
9034 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009035}
9036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009037const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009038{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009039 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009040}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009041#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009042
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009043void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009044{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009045 conf->max_major_ver = major;
9046 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009047}
9048
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009049void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009051 conf->min_major_ver = major;
9052 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009053}
9054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009055#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009056void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009057{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009058 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009059}
9060#endif
9061
Janos Follath088ce432017-04-10 12:42:31 +01009062#if defined(MBEDTLS_SSL_SRV_C)
9063void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9064 char cert_req_ca_list )
9065{
9066 conf->cert_req_ca_list = cert_req_ca_list;
9067}
9068#endif
9069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009070#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009071void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009072{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009073 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009074}
9075#endif
9076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009077#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009078void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009079{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009080 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009081}
9082#endif
9083
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009084#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009085void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009086{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009087 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009088}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009089#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009091#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009092int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009094 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009095 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009096 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009097 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009098 }
9099
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009100 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009101
9102 return( 0 );
9103}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009104#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009107void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009109 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009110}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009111#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009113#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009114void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009115{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009116 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009117}
9118#endif
9119
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009120void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009121{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009122 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009123}
9124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009125#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009126void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009128 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009129}
9130
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009131void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009132{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009133 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009134}
9135
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009136void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009137 const unsigned char period[8] )
9138{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009139 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009140}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009141#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009143#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009144#if defined(MBEDTLS_SSL_CLI_C)
9145void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009146{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009147 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009148}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009149#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009150
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009151#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009152void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9153 mbedtls_ssl_ticket_write_t *f_ticket_write,
9154 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9155 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009156{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009157 conf->f_ticket_write = f_ticket_write;
9158 conf->f_ticket_parse = f_ticket_parse;
9159 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009160}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009161#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009162#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009163
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009164#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9165void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9166 mbedtls_ssl_export_keys_t *f_export_keys,
9167 void *p_export_keys )
9168{
9169 conf->f_export_keys = f_export_keys;
9170 conf->p_export_keys = p_export_keys;
9171}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009172
9173void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9174 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9175 void *p_export_keys )
9176{
9177 conf->f_export_keys_ext = f_export_keys_ext;
9178 conf->p_export_keys = p_export_keys;
9179}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009180#endif
9181
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009182#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009183void mbedtls_ssl_conf_async_private_cb(
9184 mbedtls_ssl_config *conf,
9185 mbedtls_ssl_async_sign_t *f_async_sign,
9186 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9187 mbedtls_ssl_async_resume_t *f_async_resume,
9188 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009189 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009190{
9191 conf->f_async_sign_start = f_async_sign;
9192 conf->f_async_decrypt_start = f_async_decrypt;
9193 conf->f_async_resume = f_async_resume;
9194 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009195 conf->p_async_config_data = async_config_data;
9196}
9197
Gilles Peskine8f97af72018-04-26 11:46:10 +02009198void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9199{
9200 return( conf->p_async_config_data );
9201}
9202
Gilles Peskine1febfef2018-04-30 11:54:39 +02009203void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009204{
9205 if( ssl->handshake == NULL )
9206 return( NULL );
9207 else
9208 return( ssl->handshake->user_async_ctx );
9209}
9210
Gilles Peskine1febfef2018-04-30 11:54:39 +02009211void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009212 void *ctx )
9213{
9214 if( ssl->handshake != NULL )
9215 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009216}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009217#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009218
Paul Bakker5121ce52009-01-03 21:22:43 +00009219/*
9220 * SSL get accessors
9221 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009222size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009223{
9224 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9225}
9226
Hanno Becker8b170a02017-10-10 11:51:19 +01009227int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9228{
9229 /*
9230 * Case A: We're currently holding back
9231 * a message for further processing.
9232 */
9233
9234 if( ssl->keep_current_message == 1 )
9235 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009237 return( 1 );
9238 }
9239
9240 /*
9241 * Case B: Further records are pending in the current datagram.
9242 */
9243
9244#if defined(MBEDTLS_SSL_PROTO_DTLS)
9245 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9246 ssl->in_left > ssl->next_record_offset )
9247 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009248 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009249 return( 1 );
9250 }
9251#endif /* MBEDTLS_SSL_PROTO_DTLS */
9252
9253 /*
9254 * Case C: A handshake message is being processed.
9255 */
9256
Hanno Becker8b170a02017-10-10 11:51:19 +01009257 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9258 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009259 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009260 return( 1 );
9261 }
9262
9263 /*
9264 * Case D: An application data message is being processed
9265 */
9266 if( ssl->in_offt != NULL )
9267 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009268 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009269 return( 1 );
9270 }
9271
9272 /*
9273 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009274 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009275 * we implement support for multiple alerts in single records.
9276 */
9277
9278 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9279 return( 0 );
9280}
9281
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009282uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009283{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009284 if( ssl->session != NULL )
9285 return( ssl->session->verify_result );
9286
9287 if( ssl->session_negotiate != NULL )
9288 return( ssl->session_negotiate->verify_result );
9289
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009290 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009291}
9292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009294{
Paul Bakker926c8e42013-03-06 10:23:34 +01009295 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009296 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009299}
9300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009301const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009304 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009305 {
9306 switch( ssl->minor_ver )
9307 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009308 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009309 return( "DTLSv1.0" );
9310
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009311 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009312 return( "DTLSv1.2" );
9313
9314 default:
9315 return( "unknown (DTLS)" );
9316 }
9317 }
9318#endif
9319
Paul Bakker43ca69c2011-01-15 17:35:19 +00009320 switch( ssl->minor_ver )
9321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009322 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009323 return( "SSLv3.0" );
9324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009325 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009326 return( "TLSv1.0" );
9327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009328 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329 return( "TLSv1.1" );
9330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009331 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009332 return( "TLSv1.2" );
9333
Paul Bakker43ca69c2011-01-15 17:35:19 +00009334 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009335 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009336 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009337}
9338
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009339int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009340{
Hanno Becker3136ede2018-08-17 15:28:19 +01009341 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009343 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009344
Hanno Becker5903de42019-05-03 14:46:38 +01009345 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9346
Hanno Becker78640902018-08-13 16:35:15 +01009347 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009348 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350#if defined(MBEDTLS_ZLIB_SUPPORT)
9351 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9352 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009353#endif
9354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009357 case MBEDTLS_MODE_GCM:
9358 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009359 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009360 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009361 transform_expansion = transform->minlen;
9362 break;
9363
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009364 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009365
9366 block_size = mbedtls_cipher_get_block_size(
9367 &transform->cipher_ctx_enc );
9368
Hanno Becker3136ede2018-08-17 15:28:19 +01009369 /* Expansion due to the addition of the MAC. */
9370 transform_expansion += transform->maclen;
9371
9372 /* Expansion due to the addition of CBC padding;
9373 * Theoretically up to 256 bytes, but we never use
9374 * more than the block size of the underlying cipher. */
9375 transform_expansion += block_size;
9376
9377 /* For TLS 1.1 or higher, an explicit IV is added
9378 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009379#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9380 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009381 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009382#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009383
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009384 break;
9385
9386 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009387 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009389 }
9390
Hanno Beckera0e20d02019-05-15 14:03:01 +01009391#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009392 if( transform->out_cid_len != 0 )
9393 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009394#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009395
Hanno Becker5903de42019-05-03 14:46:38 +01009396 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009397}
9398
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009399#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9400size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9401{
9402 size_t max_len;
9403
9404 /*
9405 * Assume mfl_code is correct since it was checked when set
9406 */
Angus Grattond8213d02016-05-25 20:56:48 +10009407 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009408
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009409 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009410 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009411 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009412 {
Angus Grattond8213d02016-05-25 20:56:48 +10009413 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009414 }
9415
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009416 /* During a handshake, use the value being negotiated */
9417 if( ssl->session_negotiate != NULL &&
9418 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9419 {
9420 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9421 }
9422
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009423 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009424}
9425#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9426
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009427#if defined(MBEDTLS_SSL_PROTO_DTLS)
9428static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9429{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009430 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9431 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9432 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9433 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9434 return ( 0 );
9435
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009436 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9437 return( ssl->mtu );
9438
9439 if( ssl->mtu == 0 )
9440 return( ssl->handshake->mtu );
9441
9442 return( ssl->mtu < ssl->handshake->mtu ?
9443 ssl->mtu : ssl->handshake->mtu );
9444}
9445#endif /* MBEDTLS_SSL_PROTO_DTLS */
9446
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009447int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9448{
9449 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9450
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009451#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9452 !defined(MBEDTLS_SSL_PROTO_DTLS)
9453 (void) ssl;
9454#endif
9455
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009456#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9457 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9458
9459 if( max_len > mfl )
9460 max_len = mfl;
9461#endif
9462
9463#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009464 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009465 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009466 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009467 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9468 const size_t overhead = (size_t) ret;
9469
9470 if( ret < 0 )
9471 return( ret );
9472
9473 if( mtu <= overhead )
9474 {
9475 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9476 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9477 }
9478
9479 if( max_len > mtu - overhead )
9480 max_len = mtu - overhead;
9481 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009482#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009483
Hanno Becker0defedb2018-08-10 12:35:02 +01009484#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9485 !defined(MBEDTLS_SSL_PROTO_DTLS)
9486 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009487#endif
9488
9489 return( (int) max_len );
9490}
9491
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009492#if defined(MBEDTLS_X509_CRT_PARSE_C)
9493const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009494{
9495 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009496 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009497
Hanno Beckere6824572019-02-07 13:18:46 +00009498#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009499 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009500#else
9501 return( NULL );
9502#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009503}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009504#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009505
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009507int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9508 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009509{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009510 if( ssl == NULL ||
9511 dst == NULL ||
9512 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009513 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009514 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009516 }
9517
Hanno Becker52055ae2019-02-06 14:30:46 +00009518 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009519}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009521
Paul Bakker5121ce52009-01-03 21:22:43 +00009522/*
Paul Bakker1961b702013-01-25 14:49:24 +01009523 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009524 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009525int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009526{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009528
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009529 if( ssl == NULL || ssl->conf == NULL )
9530 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009532#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009533 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009534 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009535#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009536#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009537 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009538 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009539#endif
9540
Paul Bakker1961b702013-01-25 14:49:24 +01009541 return( ret );
9542}
9543
9544/*
9545 * Perform the SSL handshake
9546 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009547int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009548{
9549 int ret = 0;
9550
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009551 if( ssl == NULL || ssl->conf == NULL )
9552 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009554 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009556 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009557 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009558 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009559
9560 if( ret != 0 )
9561 break;
9562 }
9563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009565
9566 return( ret );
9567}
9568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569#if defined(MBEDTLS_SSL_RENEGOTIATION)
9570#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009571/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009572 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009573 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009574static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009575{
9576 int ret;
9577
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009579
9580 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009581 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9582 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009583
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009584 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009585 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009586 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009587 return( ret );
9588 }
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009591
9592 return( 0 );
9593}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009594#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009595
9596/*
9597 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 * - any side: calling mbedtls_ssl_renegotiate(),
9599 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9600 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009601 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009602 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009603 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009604 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009605static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009606{
9607 int ret;
9608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009610
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009611 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9612 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009613
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009614 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9615 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009616#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009617 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009618 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009619 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009620 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009621 ssl->handshake->out_msg_seq = 1;
9622 else
9623 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009624 }
9625#endif
9626
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9628 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009629
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009630 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009631 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009632 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009633 return( ret );
9634 }
9635
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009636 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009637
9638 return( 0 );
9639}
9640
9641/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009642 * Renegotiate current connection on client,
9643 * or request renegotiation on server
9644 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009645int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009646{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009648
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009649 if( ssl == NULL || ssl->conf == NULL )
9650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9651
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009652#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009653 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009654 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009656 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9657 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009659 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009660
9661 /* Did we already try/start sending HelloRequest? */
9662 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009663 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009664
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009665 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009666 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009668
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670 /*
9671 * On client, either start the renegotiation process or,
9672 * if already in progress, continue the handshake
9673 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009674 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9677 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009678
9679 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009681 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009682 return( ret );
9683 }
9684 }
9685 else
9686 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009688 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009689 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009690 return( ret );
9691 }
9692 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009694
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009695 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009696}
9697
9698/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009699 * Check record counters and renegotiate if they're above the limit.
9700 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009701static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009702{
Andres AG2196c7f2016-12-15 17:01:16 +00009703 size_t ep_len = ssl_ep_len( ssl );
9704 int in_ctr_cmp;
9705 int out_ctr_cmp;
9706
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9708 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009709 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009710 {
9711 return( 0 );
9712 }
9713
Andres AG2196c7f2016-12-15 17:01:16 +00009714 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9715 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009716 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009717 ssl->conf->renego_period + ep_len, 8 - ep_len );
9718
9719 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009720 {
9721 return( 0 );
9722 }
9723
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009724 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009725 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009726}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009728
9729/*
9730 * Receive application data decrypted from the SSL layer
9731 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009732int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009733{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009734 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009735 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009736
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009737 if( ssl == NULL || ssl->conf == NULL )
9738 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009741
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009743 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009744 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009745 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009746 return( ret );
9747
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009748 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009749 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009750 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009751 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009752 return( ret );
9753 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009754 }
9755#endif
9756
Hanno Becker4a810fb2017-05-24 16:27:30 +01009757 /*
9758 * Check if renegotiation is necessary and/or handshake is
9759 * in process. If yes, perform/continue, and fall through
9760 * if an unexpected packet is received while the client
9761 * is waiting for the ServerHello.
9762 *
9763 * (There is no equivalent to the last condition on
9764 * the server-side as it is not treated as within
9765 * a handshake while waiting for the ClientHello
9766 * after a renegotiation request.)
9767 */
9768
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009770 ret = ssl_check_ctr_renegotiate( ssl );
9771 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9772 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009773 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009775 return( ret );
9776 }
9777#endif
9778
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009781 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009782 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9783 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009784 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009785 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009786 return( ret );
9787 }
9788 }
9789
Hanno Beckere41158b2017-10-23 13:30:32 +01009790 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009791 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009792 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009793 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009794 if( ssl->f_get_timer != NULL &&
9795 ssl->f_get_timer( ssl->p_timer ) == -1 )
9796 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009797 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009798 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009799
Hanno Becker327c93b2018-08-15 13:56:18 +01009800 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009801 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009802 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9803 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009804
Hanno Becker4a810fb2017-05-24 16:27:30 +01009805 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9806 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009807 }
9808
9809 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009810 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009811 {
9812 /*
9813 * OpenSSL sends empty messages to randomize the IV
9814 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009815 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009816 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009817 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009818 return( 0 );
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009821 return( ret );
9822 }
9823 }
9824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009826 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009827 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009828
Hanno Becker4a810fb2017-05-24 16:27:30 +01009829 /*
9830 * - For client-side, expect SERVER_HELLO_REQUEST.
9831 * - For server-side, expect CLIENT_HELLO.
9832 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9833 */
9834
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009836 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009837 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009838 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009839 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009840 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009841
9842 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009844 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009845 {
9846 continue;
9847 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009848#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009849 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009850 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009851#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009852
Hanno Becker4a810fb2017-05-24 16:27:30 +01009853#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009854 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009856 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009857 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009858
9859 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009860#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009861 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009862 {
9863 continue;
9864 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009865#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009866 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009867 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009868#endif /* MBEDTLS_SSL_SRV_C */
9869
Hanno Becker21df7f92017-10-17 11:03:26 +01009870#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009872 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9873 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9874 ssl->conf->allow_legacy_renegotiation ==
9875 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9876 {
9877 /*
9878 * Accept renegotiation request
9879 */
Paul Bakker48916f92012-09-16 19:57:18 +00009880
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009881 /* DTLS clients need to know renego is server-initiated */
9882#if defined(MBEDTLS_SSL_PROTO_DTLS)
9883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9884 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9885 {
9886 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9887 }
9888#endif
9889 ret = ssl_start_renegotiation( ssl );
9890 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9891 ret != 0 )
9892 {
9893 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9894 return( ret );
9895 }
9896 }
9897 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009898#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009899 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009900 /*
9901 * Refuse renegotiation
9902 */
9903
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009904 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009905
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009906#if defined(MBEDTLS_SSL_PROTO_SSL3)
9907 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009908 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009909 /* SSLv3 does not have a "no_renegotiation" warning, so
9910 we send a fatal alert and abort the connection. */
9911 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9912 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9913 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009914 }
9915 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9917#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9918 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9919 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009921 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9922 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9923 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009924 {
9925 return( ret );
9926 }
Paul Bakker48916f92012-09-16 19:57:18 +00009927 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009928 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009929#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9930 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9933 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009934 }
Paul Bakker48916f92012-09-16 19:57:18 +00009935 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009936
Hanno Becker90333da2017-10-10 11:27:13 +01009937 /* At this point, we don't know whether the renegotiation has been
9938 * completed or not. The cases to consider are the following:
9939 * 1) The renegotiation is complete. In this case, no new record
9940 * has been read yet.
9941 * 2) The renegotiation is incomplete because the client received
9942 * an application data record while awaiting the ServerHello.
9943 * 3) The renegotiation is incomplete because the client received
9944 * a non-handshake, non-application data message while awaiting
9945 * the ServerHello.
9946 * In each of these case, looping will be the proper action:
9947 * - For 1), the next iteration will read a new record and check
9948 * if it's application data.
9949 * - For 2), the loop condition isn't satisfied as application data
9950 * is present, hence continue is the same as break
9951 * - For 3), the loop condition is satisfied and read_record
9952 * will re-deliver the message that was held back by the client
9953 * when expecting the ServerHello.
9954 */
9955 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009956 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009957#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009958 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009959 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009960 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009961 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009962 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009963 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009965 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009966 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009967 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009968 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009969 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009971
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009972 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9973 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009976 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009977 }
9978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009979 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009980 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009981 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9982 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009983 }
9984
9985 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009986
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009987 /* We're going to return something now, cancel timer,
9988 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009989 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009990 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009991
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009992#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009993 /* If we requested renego but received AppData, resend HelloRequest.
9994 * Do it now, after setting in_offt, to avoid taking this branch
9995 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009996#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009997 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009998 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009999 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +020010000 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010002 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +020010003 return( ret );
10004 }
10005 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010006#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010007#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010008 }
10009
10010 n = ( len < ssl->in_msglen )
10011 ? len : ssl->in_msglen;
10012
10013 memcpy( buf, ssl->in_offt, n );
10014 ssl->in_msglen -= n;
10015
10016 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010017 {
10018 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010019 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010020 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010022 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010023 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010024 /* more data available */
10025 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010026 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010028 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010029
Paul Bakker23986e52011-04-24 08:57:21 +000010030 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010031}
10032
10033/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010034 * Send application data to be encrypted by the SSL layer, taking care of max
10035 * fragment length and buffer size.
10036 *
10037 * According to RFC 5246 Section 6.2.1:
10038 *
10039 * Zero-length fragments of Application data MAY be sent as they are
10040 * potentially useful as a traffic analysis countermeasure.
10041 *
10042 * Therefore, it is possible that the input message length is 0 and the
10043 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010044 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010045static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010046 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010047{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010048 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10049 const size_t max_len = (size_t) ret;
10050
10051 if( ret < 0 )
10052 {
10053 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10054 return( ret );
10055 }
10056
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010057 if( len > max_len )
10058 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010059#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010060 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010061 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010062 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010063 "maximum fragment length: %d > %d",
10064 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010065 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010066 }
10067 else
10068#endif
10069 len = max_len;
10070 }
Paul Bakker887bd502011-06-08 13:10:54 +000010071
Paul Bakker5121ce52009-01-03 21:22:43 +000010072 if( ssl->out_left != 0 )
10073 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010074 /*
10075 * The user has previously tried to send the data and
10076 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10077 * written. In this case, we expect the high-level write function
10078 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10079 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010081 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010082 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010083 return( ret );
10084 }
10085 }
Paul Bakker887bd502011-06-08 13:10:54 +000010086 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010087 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010088 /*
10089 * The user is trying to send a message the first time, so we need to
10090 * copy the data into the internal buffers and setup the data structure
10091 * to keep track of partial writes
10092 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010093 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010095 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010096
Hanno Becker67bc7c32018-08-06 11:33:50 +010010097 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010100 return( ret );
10101 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010102 }
10103
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010104 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010105}
10106
10107/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010108 * Write application data, doing 1/n-1 splitting if necessary.
10109 *
10110 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010111 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010112 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010114#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010115static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010116 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010117{
10118 int ret;
10119
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010120 if( ssl->conf->cbc_record_splitting ==
10121 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010122 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010123 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10124 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10125 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010126 {
10127 return( ssl_write_real( ssl, buf, len ) );
10128 }
10129
10130 if( ssl->split_done == 0 )
10131 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010132 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010133 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010134 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010135 }
10136
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010137 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10138 return( ret );
10139 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010140
10141 return( ret + 1 );
10142}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010143#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010144
10145/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010146 * Write application data (public-facing wrapper)
10147 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010148int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010149{
10150 int ret;
10151
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010152 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010153
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010154 if( ssl == NULL || ssl->conf == NULL )
10155 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10156
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010157#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010158 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10159 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010160 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010161 return( ret );
10162 }
10163#endif
10164
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010165 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010166 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010167 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010168 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010169 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010170 return( ret );
10171 }
10172 }
10173
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010174#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010175 ret = ssl_write_split( ssl, buf, len );
10176#else
10177 ret = ssl_write_real( ssl, buf, len );
10178#endif
10179
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010180 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010181
10182 return( ret );
10183}
10184
10185/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010186 * Notify the peer that the connection is being closed
10187 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010188int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010189{
10190 int ret;
10191
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010192 if( ssl == NULL || ssl->conf == NULL )
10193 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010196
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010197 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010198 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010199
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010200 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010202 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10203 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10204 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010205 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010207 return( ret );
10208 }
10209 }
10210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010212
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010213 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010214}
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010217{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010218 if( transform == NULL )
10219 return;
10220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010222 deflateEnd( &transform->ctx_deflate );
10223 inflateEnd( &transform->ctx_inflate );
10224#endif
10225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010226 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10227 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010228
Hanno Beckerd56ed242018-01-03 15:32:51 +000010229#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010230 mbedtls_md_free( &transform->md_ctx_enc );
10231 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010232#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010233
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010234 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010235}
10236
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010237#if defined(MBEDTLS_X509_CRT_PARSE_C)
10238static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010239{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010241
10242 while( cur != NULL )
10243 {
10244 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010245 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010246 cur = next;
10247 }
10248}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010249#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010250
Hanno Becker0271f962018-08-16 13:23:47 +010010251#if defined(MBEDTLS_SSL_PROTO_DTLS)
10252
10253static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10254{
10255 unsigned offset;
10256 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10257
10258 if( hs == NULL )
10259 return;
10260
Hanno Becker283f5ef2018-08-24 09:34:47 +010010261 ssl_free_buffered_record( ssl );
10262
Hanno Becker0271f962018-08-16 13:23:47 +010010263 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010264 ssl_buffering_free_slot( ssl, offset );
10265}
10266
10267static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10268 uint8_t slot )
10269{
10270 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10271 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010272
10273 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10274 return;
10275
Hanno Beckere605b192018-08-21 15:59:07 +010010276 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010277 {
Hanno Beckere605b192018-08-21 15:59:07 +010010278 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010279 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010280 mbedtls_free( hs_buf->data );
10281 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010282 }
10283}
10284
10285#endif /* MBEDTLS_SSL_PROTO_DTLS */
10286
Gilles Peskine9b562d52018-04-25 20:32:43 +020010287void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010288{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010289 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10290
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010291 if( handshake == NULL )
10292 return;
10293
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010294#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10295 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10296 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010297 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010298 handshake->async_in_progress = 0;
10299 }
10300#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10301
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010302#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10303 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10304 mbedtls_md5_free( &handshake->fin_md5 );
10305 mbedtls_sha1_free( &handshake->fin_sha1 );
10306#endif
10307#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10308#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010309#if defined(MBEDTLS_USE_PSA_CRYPTO)
10310 psa_hash_abort( &handshake->fin_sha256_psa );
10311#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010312 mbedtls_sha256_free( &handshake->fin_sha256 );
10313#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010314#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010315#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010316#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010317 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010318#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010319 mbedtls_sha512_free( &handshake->fin_sha512 );
10320#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010321#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010322#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10323
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324#if defined(MBEDTLS_DHM_C)
10325 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010326#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010327#if defined(MBEDTLS_ECDH_C)
10328 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010329#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010330#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010331 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010332#if defined(MBEDTLS_SSL_CLI_C)
10333 mbedtls_free( handshake->ecjpake_cache );
10334 handshake->ecjpake_cache = NULL;
10335 handshake->ecjpake_cache_len = 0;
10336#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010337#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010338
Janos Follath4ae5c292016-02-10 11:27:43 +000010339#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10340 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010341 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010342 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010343#endif
10344
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010345#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10346 if( handshake->psk != NULL )
10347 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010348 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010349 mbedtls_free( handshake->psk );
10350 }
10351#endif
10352
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010353#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10354 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010355 /*
10356 * Free only the linked list wrapper, not the keys themselves
10357 * since the belong to the SNI callback
10358 */
10359 if( handshake->sni_key_cert != NULL )
10360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010362
10363 while( cur != NULL )
10364 {
10365 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010366 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010367 cur = next;
10368 }
10369 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010370#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010371
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010372#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010373 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010374 if( handshake->ecrs_peer_cert != NULL )
10375 {
10376 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10377 mbedtls_free( handshake->ecrs_peer_cert );
10378 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010379#endif
10380
Hanno Becker75173122019-02-06 16:18:31 +000010381#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10382 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10383 mbedtls_pk_free( &handshake->peer_pubkey );
10384#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010386#if defined(MBEDTLS_SSL_PROTO_DTLS)
10387 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010388 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010389 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010390#endif
10391
Hanno Becker4a63ed42019-01-08 11:39:35 +000010392#if defined(MBEDTLS_ECDH_C) && \
10393 defined(MBEDTLS_USE_PSA_CRYPTO)
10394 psa_destroy_key( handshake->ecdh_psa_privkey );
10395#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10396
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010397 mbedtls_platform_zeroize( handshake,
10398 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010399}
10400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010402{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010403 if( session == NULL )
10404 return;
10405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010407 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010408#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010409
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010410#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010411 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010412#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010413
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010414 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010415}
10416
Paul Bakker5121ce52009-01-03 21:22:43 +000010417/*
10418 * Free an SSL context
10419 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010421{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010422 if( ssl == NULL )
10423 return;
10424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010426
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010427 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010428 {
Angus Grattond8213d02016-05-25 20:56:48 +100010429 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010430 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010431 }
10432
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010433 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010434 {
Angus Grattond8213d02016-05-25 20:56:48 +100010435 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010436 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010437 }
10438
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010439#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010440 if( ssl->compress_buf != NULL )
10441 {
Angus Grattond8213d02016-05-25 20:56:48 +100010442 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010443 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010444 }
10445#endif
10446
Paul Bakker48916f92012-09-16 19:57:18 +000010447 if( ssl->transform )
10448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010449 mbedtls_ssl_transform_free( ssl->transform );
10450 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010451 }
10452
10453 if( ssl->handshake )
10454 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010455 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010456 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10457 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010459 mbedtls_free( ssl->handshake );
10460 mbedtls_free( ssl->transform_negotiate );
10461 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010462 }
10463
Paul Bakkerc0463502013-02-14 11:19:38 +010010464 if( ssl->session )
10465 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010466 mbedtls_ssl_session_free( ssl->session );
10467 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010468 }
10469
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010470#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010471 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010472 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010473 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010474 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010475 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010476#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010477
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010478#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10479 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010480 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10482 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010483 }
10484#endif
10485
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010486#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010487 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010488#endif
10489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010490 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010491
Paul Bakker86f04f42013-02-14 11:20:09 +010010492 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010493 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010494}
10495
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010496/*
10497 * Initialze mbedtls_ssl_config
10498 */
10499void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10500{
10501 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10502}
10503
Simon Butcherc97b6972015-12-27 23:48:17 +000010504#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010505static int ssl_preset_default_hashes[] = {
10506#if defined(MBEDTLS_SHA512_C)
10507 MBEDTLS_MD_SHA512,
10508 MBEDTLS_MD_SHA384,
10509#endif
10510#if defined(MBEDTLS_SHA256_C)
10511 MBEDTLS_MD_SHA256,
10512 MBEDTLS_MD_SHA224,
10513#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010514#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010515 MBEDTLS_MD_SHA1,
10516#endif
10517 MBEDTLS_MD_NONE
10518};
Simon Butcherc97b6972015-12-27 23:48:17 +000010519#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010520
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010521static int ssl_preset_suiteb_ciphersuites[] = {
10522 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10523 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10524 0
10525};
10526
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010527#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010528static int ssl_preset_suiteb_hashes[] = {
10529 MBEDTLS_MD_SHA256,
10530 MBEDTLS_MD_SHA384,
10531 MBEDTLS_MD_NONE
10532};
10533#endif
10534
10535#if defined(MBEDTLS_ECP_C)
10536static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010537#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010538 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010539#endif
10540#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010541 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010542#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010543 MBEDTLS_ECP_DP_NONE
10544};
10545#endif
10546
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010548 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010550int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010551 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010552{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010553#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010555#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010557 /* Use the functions here so that they are covered in tests,
10558 * but otherwise access member directly for efficiency */
10559 mbedtls_ssl_conf_endpoint( conf, endpoint );
10560 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010561
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010562 /*
10563 * Things that are common to all presets
10564 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010565#if defined(MBEDTLS_SSL_CLI_C)
10566 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10567 {
10568 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10569#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10570 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10571#endif
10572 }
10573#endif
10574
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010575#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010576 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010577#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010578
10579#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10580 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10581#endif
10582
10583#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10584 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10585#endif
10586
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010587#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10588 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10589#endif
10590
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010591#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010592 conf->f_cookie_write = ssl_cookie_write_dummy;
10593 conf->f_cookie_check = ssl_cookie_check_dummy;
10594#endif
10595
10596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10597 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10598#endif
10599
Janos Follath088ce432017-04-10 12:42:31 +010010600#if defined(MBEDTLS_SSL_SRV_C)
10601 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10602#endif
10603
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010604#if defined(MBEDTLS_SSL_PROTO_DTLS)
10605 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10606 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10607#endif
10608
10609#if defined(MBEDTLS_SSL_RENEGOTIATION)
10610 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010611 memset( conf->renego_period, 0x00, 2 );
10612 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010613#endif
10614
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010615#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10616 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10617 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010618 const unsigned char dhm_p[] =
10619 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10620 const unsigned char dhm_g[] =
10621 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10622
Hanno Beckera90658f2017-10-04 15:29:08 +010010623 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10624 dhm_p, sizeof( dhm_p ),
10625 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010626 {
10627 return( ret );
10628 }
10629 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010630#endif
10631
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010632 /*
10633 * Preset-specific defaults
10634 */
10635 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010636 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010637 /*
10638 * NSA Suite B
10639 */
10640 case MBEDTLS_SSL_PRESET_SUITEB:
10641 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10642 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10643 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10644 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10645
10646 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10647 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10648 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10649 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10650 ssl_preset_suiteb_ciphersuites;
10651
10652#if defined(MBEDTLS_X509_CRT_PARSE_C)
10653 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010654#endif
10655
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010656#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010657 conf->sig_hashes = ssl_preset_suiteb_hashes;
10658#endif
10659
10660#if defined(MBEDTLS_ECP_C)
10661 conf->curve_list = ssl_preset_suiteb_curves;
10662#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010663 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010664
10665 /*
10666 * Default
10667 */
10668 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010669 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10670 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10671 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10672 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10673 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10674 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10675 MBEDTLS_SSL_MIN_MINOR_VERSION :
10676 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010677 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10678 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10679
10680#if defined(MBEDTLS_SSL_PROTO_DTLS)
10681 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10682 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10683#endif
10684
10685 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10686 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10687 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10688 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10689 mbedtls_ssl_list_ciphersuites();
10690
10691#if defined(MBEDTLS_X509_CRT_PARSE_C)
10692 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10693#endif
10694
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010695#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010696 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010697#endif
10698
10699#if defined(MBEDTLS_ECP_C)
10700 conf->curve_list = mbedtls_ecp_grp_id_list();
10701#endif
10702
10703#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10704 conf->dhm_min_bitlen = 1024;
10705#endif
10706 }
10707
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010708 return( 0 );
10709}
10710
10711/*
10712 * Free mbedtls_ssl_config
10713 */
10714void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10715{
10716#if defined(MBEDTLS_DHM_C)
10717 mbedtls_mpi_free( &conf->dhm_P );
10718 mbedtls_mpi_free( &conf->dhm_G );
10719#endif
10720
10721#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10722 if( conf->psk != NULL )
10723 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010724 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010725 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010726 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010727 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010728 }
10729
10730 if( conf->psk_identity != NULL )
10731 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010732 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010733 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010734 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010735 conf->psk_identity_len = 0;
10736 }
10737#endif
10738
10739#if defined(MBEDTLS_X509_CRT_PARSE_C)
10740 ssl_key_cert_free( conf->key_cert );
10741#endif
10742
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010743 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010744}
10745
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010746#if defined(MBEDTLS_PK_C) && \
10747 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010748/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010749 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010750 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010751unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010752{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010753#if defined(MBEDTLS_RSA_C)
10754 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10755 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010756#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010757#if defined(MBEDTLS_ECDSA_C)
10758 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10759 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010760#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010761 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010762}
10763
Hanno Becker7e5437a2017-04-28 17:15:26 +010010764unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10765{
10766 switch( type ) {
10767 case MBEDTLS_PK_RSA:
10768 return( MBEDTLS_SSL_SIG_RSA );
10769 case MBEDTLS_PK_ECDSA:
10770 case MBEDTLS_PK_ECKEY:
10771 return( MBEDTLS_SSL_SIG_ECDSA );
10772 default:
10773 return( MBEDTLS_SSL_SIG_ANON );
10774 }
10775}
10776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010777mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010778{
10779 switch( sig )
10780 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010781#if defined(MBEDTLS_RSA_C)
10782 case MBEDTLS_SSL_SIG_RSA:
10783 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010784#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785#if defined(MBEDTLS_ECDSA_C)
10786 case MBEDTLS_SSL_SIG_ECDSA:
10787 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010788#endif
10789 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010790 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010791 }
10792}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010793#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010794
Hanno Becker7e5437a2017-04-28 17:15:26 +010010795#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10796 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10797
10798/* Find an entry in a signature-hash set matching a given hash algorithm. */
10799mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10800 mbedtls_pk_type_t sig_alg )
10801{
10802 switch( sig_alg )
10803 {
10804 case MBEDTLS_PK_RSA:
10805 return( set->rsa );
10806 case MBEDTLS_PK_ECDSA:
10807 return( set->ecdsa );
10808 default:
10809 return( MBEDTLS_MD_NONE );
10810 }
10811}
10812
10813/* Add a signature-hash-pair to a signature-hash set */
10814void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10815 mbedtls_pk_type_t sig_alg,
10816 mbedtls_md_type_t md_alg )
10817{
10818 switch( sig_alg )
10819 {
10820 case MBEDTLS_PK_RSA:
10821 if( set->rsa == MBEDTLS_MD_NONE )
10822 set->rsa = md_alg;
10823 break;
10824
10825 case MBEDTLS_PK_ECDSA:
10826 if( set->ecdsa == MBEDTLS_MD_NONE )
10827 set->ecdsa = md_alg;
10828 break;
10829
10830 default:
10831 break;
10832 }
10833}
10834
10835/* Allow exactly one hash algorithm for each signature. */
10836void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10837 mbedtls_md_type_t md_alg )
10838{
10839 set->rsa = md_alg;
10840 set->ecdsa = md_alg;
10841}
10842
10843#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10844 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10845
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010846/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010847 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010848 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010849mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010850{
10851 switch( hash )
10852 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010853#if defined(MBEDTLS_MD5_C)
10854 case MBEDTLS_SSL_HASH_MD5:
10855 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010856#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010857#if defined(MBEDTLS_SHA1_C)
10858 case MBEDTLS_SSL_HASH_SHA1:
10859 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010861#if defined(MBEDTLS_SHA256_C)
10862 case MBEDTLS_SSL_HASH_SHA224:
10863 return( MBEDTLS_MD_SHA224 );
10864 case MBEDTLS_SSL_HASH_SHA256:
10865 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010867#if defined(MBEDTLS_SHA512_C)
10868 case MBEDTLS_SSL_HASH_SHA384:
10869 return( MBEDTLS_MD_SHA384 );
10870 case MBEDTLS_SSL_HASH_SHA512:
10871 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010872#endif
10873 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010874 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010875 }
10876}
10877
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010878/*
10879 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10880 */
10881unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10882{
10883 switch( md )
10884 {
10885#if defined(MBEDTLS_MD5_C)
10886 case MBEDTLS_MD_MD5:
10887 return( MBEDTLS_SSL_HASH_MD5 );
10888#endif
10889#if defined(MBEDTLS_SHA1_C)
10890 case MBEDTLS_MD_SHA1:
10891 return( MBEDTLS_SSL_HASH_SHA1 );
10892#endif
10893#if defined(MBEDTLS_SHA256_C)
10894 case MBEDTLS_MD_SHA224:
10895 return( MBEDTLS_SSL_HASH_SHA224 );
10896 case MBEDTLS_MD_SHA256:
10897 return( MBEDTLS_SSL_HASH_SHA256 );
10898#endif
10899#if defined(MBEDTLS_SHA512_C)
10900 case MBEDTLS_MD_SHA384:
10901 return( MBEDTLS_SSL_HASH_SHA384 );
10902 case MBEDTLS_MD_SHA512:
10903 return( MBEDTLS_SSL_HASH_SHA512 );
10904#endif
10905 default:
10906 return( MBEDTLS_SSL_HASH_NONE );
10907 }
10908}
10909
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010910#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010912 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010913 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010914 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010915int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010916{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010917 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010918
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010919 if( ssl->conf->curve_list == NULL )
10920 return( -1 );
10921
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010922 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010923 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010924 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010925
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010926 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010927}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010928#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010929
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010930#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010931/*
10932 * Check if a hash proposed by the peer is in our list.
10933 * Return 0 if we're willing to use it, -1 otherwise.
10934 */
10935int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10936 mbedtls_md_type_t md )
10937{
10938 const int *cur;
10939
10940 if( ssl->conf->sig_hashes == NULL )
10941 return( -1 );
10942
10943 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10944 if( *cur == (int) md )
10945 return( 0 );
10946
10947 return( -1 );
10948}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010949#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010951#if defined(MBEDTLS_X509_CRT_PARSE_C)
10952int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10953 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010954 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010955 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010956{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010957 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010958#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010959 int usage = 0;
10960#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010962 const char *ext_oid;
10963 size_t ext_len;
10964#endif
10965
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010966#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10967 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010968 ((void) cert);
10969 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010970 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010971#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010972
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010973#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10974 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010975 {
10976 /* Server part of the key exchange */
10977 switch( ciphersuite->key_exchange )
10978 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010979 case MBEDTLS_KEY_EXCHANGE_RSA:
10980 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010981 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010982 break;
10983
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010984 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10985 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10986 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10987 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010988 break;
10989
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010990 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10991 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010992 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010993 break;
10994
10995 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010996 case MBEDTLS_KEY_EXCHANGE_NONE:
10997 case MBEDTLS_KEY_EXCHANGE_PSK:
10998 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10999 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020011000 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011001 usage = 0;
11002 }
11003 }
11004 else
11005 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011006 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11007 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011008 }
11009
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011010 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011011 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011012 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011013 ret = -1;
11014 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011015#else
11016 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011019#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11020 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11023 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011024 }
11025 else
11026 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011027 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11028 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011029 }
11030
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011032 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011033 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011034 ret = -1;
11035 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011036#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011037
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011038 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011039}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011040#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011041
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011042/*
11043 * Convert version numbers to/from wire format
11044 * and, for DTLS, to/from TLS equivalent.
11045 *
11046 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011047 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011048 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11049 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11050 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011051void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011052 unsigned char ver[2] )
11053{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011054#if defined(MBEDTLS_SSL_PROTO_DTLS)
11055 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011057 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011058 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11059
11060 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11061 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11062 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011063 else
11064#else
11065 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011066#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011067 {
11068 ver[0] = (unsigned char) major;
11069 ver[1] = (unsigned char) minor;
11070 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011071}
11072
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011073void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011074 const unsigned char ver[2] )
11075{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011076#if defined(MBEDTLS_SSL_PROTO_DTLS)
11077 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011078 {
11079 *major = 255 - ver[0] + 2;
11080 *minor = 255 - ver[1] + 1;
11081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011082 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011083 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11084 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011085 else
11086#else
11087 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011088#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011089 {
11090 *major = ver[0];
11091 *minor = ver[1];
11092 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011093}
11094
Simon Butcher99000142016-10-13 17:21:01 +010011095int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11096{
11097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11098 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11099 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11100
11101 switch( md )
11102 {
11103#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11104#if defined(MBEDTLS_MD5_C)
11105 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011106 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011107#endif
11108#if defined(MBEDTLS_SHA1_C)
11109 case MBEDTLS_SSL_HASH_SHA1:
11110 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11111 break;
11112#endif
11113#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11114#if defined(MBEDTLS_SHA512_C)
11115 case MBEDTLS_SSL_HASH_SHA384:
11116 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11117 break;
11118#endif
11119#if defined(MBEDTLS_SHA256_C)
11120 case MBEDTLS_SSL_HASH_SHA256:
11121 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11122 break;
11123#endif
11124 default:
11125 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11126 }
11127
11128 return 0;
11129#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11130 (void) ssl;
11131 (void) md;
11132
11133 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11134#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11135}
11136
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011137#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11138 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11139int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11140 unsigned char *output,
11141 unsigned char *data, size_t data_len )
11142{
11143 int ret = 0;
11144 mbedtls_md5_context mbedtls_md5;
11145 mbedtls_sha1_context mbedtls_sha1;
11146
11147 mbedtls_md5_init( &mbedtls_md5 );
11148 mbedtls_sha1_init( &mbedtls_sha1 );
11149
11150 /*
11151 * digitally-signed struct {
11152 * opaque md5_hash[16];
11153 * opaque sha_hash[20];
11154 * };
11155 *
11156 * md5_hash
11157 * MD5(ClientHello.random + ServerHello.random
11158 * + ServerParams);
11159 * sha_hash
11160 * SHA(ClientHello.random + ServerHello.random
11161 * + ServerParams);
11162 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011166 goto exit;
11167 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011168 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011169 ssl->handshake->randbytes, 64 ) ) != 0 )
11170 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 goto exit;
11173 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 goto exit;
11178 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011179 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011180 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011181 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011182 goto exit;
11183 }
11184
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011187 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011188 goto exit;
11189 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011190 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011191 ssl->handshake->randbytes, 64 ) ) != 0 )
11192 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011193 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011194 goto exit;
11195 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011196 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011197 data_len ) ) != 0 )
11198 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011199 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011200 goto exit;
11201 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011202 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011203 output + 16 ) ) != 0 )
11204 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011205 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011206 goto exit;
11207 }
11208
11209exit:
11210 mbedtls_md5_free( &mbedtls_md5 );
11211 mbedtls_sha1_free( &mbedtls_sha1 );
11212
11213 if( ret != 0 )
11214 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11215 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11216
11217 return( ret );
11218
11219}
11220#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11221 MBEDTLS_SSL_PROTO_TLS1_1 */
11222
11223#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11224 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011225
11226#if defined(MBEDTLS_USE_PSA_CRYPTO)
11227int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11228 unsigned char *hash, size_t *hashlen,
11229 unsigned char *data, size_t data_len,
11230 mbedtls_md_type_t md_alg )
11231{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011232 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011233 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011234 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11235
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011236 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011237
11238 if( ( status = psa_hash_setup( &hash_operation,
11239 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011240 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011241 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011242 goto exit;
11243 }
11244
Andrzej Kurek814feff2019-01-14 04:35:19 -050011245 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11246 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011247 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011248 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011249 goto exit;
11250 }
11251
Andrzej Kurek814feff2019-01-14 04:35:19 -050011252 if( ( status = psa_hash_update( &hash_operation,
11253 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011254 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011255 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 goto exit;
11257 }
11258
Andrzej Kurek814feff2019-01-14 04:35:19 -050011259 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11260 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011261 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011262 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011263 goto exit;
11264 }
11265
11266exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011267 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011268 {
11269 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11270 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011271 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011272 {
11273 case PSA_ERROR_NOT_SUPPORTED:
11274 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011275 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011276 case PSA_ERROR_BUFFER_TOO_SMALL:
11277 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11278 case PSA_ERROR_INSUFFICIENT_MEMORY:
11279 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11280 default:
11281 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11282 }
11283 }
11284 return( 0 );
11285}
11286
11287#else
11288
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011289int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011290 unsigned char *hash, size_t *hashlen,
11291 unsigned char *data, size_t data_len,
11292 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011293{
11294 int ret = 0;
11295 mbedtls_md_context_t ctx;
11296 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011297 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011298
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011299 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011300
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011301 mbedtls_md_init( &ctx );
11302
11303 /*
11304 * digitally-signed struct {
11305 * opaque client_random[32];
11306 * opaque server_random[32];
11307 * ServerDHParams params;
11308 * };
11309 */
11310 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11311 {
11312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11313 goto exit;
11314 }
11315 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11316 {
11317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11318 goto exit;
11319 }
11320 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11321 {
11322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11323 goto exit;
11324 }
11325 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11326 {
11327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11328 goto exit;
11329 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011330 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011331 {
11332 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11333 goto exit;
11334 }
11335
11336exit:
11337 mbedtls_md_free( &ctx );
11338
11339 if( ret != 0 )
11340 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11341 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11342
11343 return( ret );
11344}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011345#endif /* MBEDTLS_USE_PSA_CRYPTO */
11346
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011347#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11348 MBEDTLS_SSL_PROTO_TLS1_2 */
11349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011350#endif /* MBEDTLS_SSL_TLS_C */