blob: 56745e3f5f992b95bf118762a5daa47aa61904ed [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;
Janos Follath53b8ec22019-08-08 10:28:27 +0100633 psa_key_attributes_t key_attributes;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500634 psa_key_handle_t master_slot;
Janos Follathda6ac012019-08-16 13:47:29 +0100635 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +0100636 PSA_KEY_DERIVATION_OPERATION_INIT;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500637
Andrzej Kurekc929a822019-01-14 03:51:11 -0500638 if( md_type == MBEDTLS_MD_SHA384 )
639 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
640 else
641 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
642
Janos Follath53b8ec22019-08-08 10:28:27 +0100643 key_attributes = psa_key_attributes_init();
644 psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
645 psa_set_key_algorithm( &key_attributes, alg );
646 psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500647
Janos Follath53b8ec22019-08-08 10:28:27 +0100648 status = psa_import_key( &key_attributes, secret, slen, &master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500649 if( status != PSA_SUCCESS )
650 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
651
Janos Follathda6ac012019-08-16 13:47:29 +0100652 status = psa_key_derivation( &derivation,
Andrzej Kurekc929a822019-01-14 03:51:11 -0500653 master_slot, alg,
654 random, rlen,
655 (unsigned char const *) label,
656 (size_t) strlen( label ),
657 dlen );
658 if( status != PSA_SUCCESS )
659 {
Janos Follathda6ac012019-08-16 13:47:29 +0100660 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500661 psa_destroy_key( master_slot );
662 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
663 }
664
Janos Follathda6ac012019-08-16 13:47:29 +0100665 status = psa_key_derivation_output_bytes( &derivation, dstbuf, dlen );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500666 if( status != PSA_SUCCESS )
667 {
Janos Follathda6ac012019-08-16 13:47:29 +0100668 psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500669 psa_destroy_key( master_slot );
670 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
671 }
672
Janos Follathda6ac012019-08-16 13:47:29 +0100673 status = psa_key_derivation_abort( &derivation );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500674 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500675 {
676 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500677 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500678 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500679
680 status = psa_destroy_key( master_slot );
681 if( status != PSA_SUCCESS )
682 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
683
Andrzej Kurek33171262019-01-15 03:25:18 -0500684 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500685}
686
687#else /* MBEDTLS_USE_PSA_CRYPTO */
688
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100690 const unsigned char *secret, size_t slen,
691 const char *label,
692 const unsigned char *random, size_t rlen,
693 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000694{
695 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100696 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300697 unsigned char *tmp;
698 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200699 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
700 const mbedtls_md_info_t *md_info;
701 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100702 int ret;
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200706 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
707 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200709 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100710
Ron Eldor3b350852019-05-07 18:31:49 +0300711 tmp_len = md_len + strlen( label ) + rlen;
712 tmp = mbedtls_calloc( 1, tmp_len );
713 if( tmp == NULL )
714 {
715 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
716 goto exit;
717 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000718
719 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100720 memcpy( tmp + md_len, label, nb );
721 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000722 nb += rlen;
723
724 /*
725 * Compute P_<hash>(secret, label + random)[0..dlen]
726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300728 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100729
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200730 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
731 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
732 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100733
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100734 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000735 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_md_hmac_reset ( &md_ctx );
737 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
738 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 mbedtls_md_hmac_reset ( &md_ctx );
741 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
742 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100744 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000745
746 for( j = 0; j < k; j++ )
747 dstbuf[i + j] = h_i[j];
748 }
749
Ron Eldor3b350852019-05-07 18:31:49 +0300750exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100752
Ron Eldor3b350852019-05-07 18:31:49 +0300753 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500754 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000755
Ron Eldor3b350852019-05-07 18:31:49 +0300756 mbedtls_free( tmp );
757
758 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000759}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500760#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100762static int tls_prf_sha256( const unsigned char *secret, size_t slen,
763 const char *label,
764 const unsigned char *random, size_t rlen,
765 unsigned char *dstbuf, size_t dlen )
766{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200767 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100768 label, random, rlen, dstbuf, dlen ) );
769}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200773static int tls_prf_sha384( const unsigned char *secret, size_t slen,
774 const char *label,
775 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000776 unsigned char *dstbuf, size_t dlen )
777{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200778 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100779 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000780}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781#endif /* MBEDTLS_SHA512_C */
782#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000783
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200784static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200785
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200786#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
787 defined(MBEDTLS_SSL_PROTO_TLS1_1)
788static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200789#endif
Paul Bakker380da532012-04-18 16:10:25 +0000790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200791#if defined(MBEDTLS_SSL_PROTO_SSL3)
792static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
793static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200794#endif
795
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200796#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
797static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
798static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200799#endif
800
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200801#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
802#if defined(MBEDTLS_SHA256_C)
803static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
804static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
805static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200806#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100807
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200808#if defined(MBEDTLS_SHA512_C)
809static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
810static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
811static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100812#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200813#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000814
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100815#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100816 defined(MBEDTLS_USE_PSA_CRYPTO)
817static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
818{
819 if( ssl->conf->f_psk != NULL )
820 {
821 /* If we've used a callback to select the PSK,
822 * the static configuration is irrelevant. */
823 if( ssl->handshake->psk_opaque != 0 )
824 return( 1 );
825
826 return( 0 );
827 }
828
829 if( ssl->conf->psk_opaque != 0 )
830 return( 1 );
831
832 return( 0 );
833}
834#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100835 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100836
Ron Eldorcf280092019-05-14 20:19:13 +0300837#if defined(MBEDTLS_SSL_EXPORT_KEYS)
838static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
839{
840#if defined(MBEDTLS_SSL_PROTO_SSL3)
841 if( tls_prf == ssl3_prf )
842 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300843 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300844 }
845 else
846#endif
847#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
848 if( tls_prf == tls1_prf )
849 {
850 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
851 }
852 else
853#endif
854#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
855#if defined(MBEDTLS_SHA512_C)
856 if( tls_prf == tls_prf_sha384 )
857 {
858 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
859 }
860 else
861#endif
862#if defined(MBEDTLS_SHA256_C)
863 if( tls_prf == tls_prf_sha256 )
864 {
865 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
866 }
867 else
868#endif
869#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
870 return( MBEDTLS_SSL_TLS_PRF_NONE );
871}
872#endif /* MBEDTLS_SSL_EXPORT_KEYS */
873
Ron Eldor51d3ab52019-05-12 14:54:30 +0300874int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
875 const unsigned char *secret, size_t slen,
876 const char *label,
877 const unsigned char *random, size_t rlen,
878 unsigned char *dstbuf, size_t dlen )
879{
880 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
881
882 switch( prf )
883 {
884#if defined(MBEDTLS_SSL_PROTO_SSL3)
885 case MBEDTLS_SSL_TLS_PRF_SSL3:
886 tls_prf = ssl3_prf;
887 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300888#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300889#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
890 case MBEDTLS_SSL_TLS_PRF_TLS1:
891 tls_prf = tls1_prf;
892 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300893#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
894
895#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300896#if defined(MBEDTLS_SHA512_C)
897 case MBEDTLS_SSL_TLS_PRF_SHA384:
898 tls_prf = tls_prf_sha384;
899 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300900#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300901#if defined(MBEDTLS_SHA256_C)
902 case MBEDTLS_SSL_TLS_PRF_SHA256:
903 tls_prf = tls_prf_sha256;
904 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300905#endif /* MBEDTLS_SHA256_C */
906#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300907 default:
908 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
909 }
910
911 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
912}
913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000915{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200916 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000917#if defined(MBEDTLS_USE_PSA_CRYPTO)
918 int psa_fallthrough;
919#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000920 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000921 unsigned char keyblk[256];
922 unsigned char *key1;
923 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100924 unsigned char *mac_enc;
925 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000926 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200927 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000928 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000929 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200930 const mbedtls_cipher_info_t *cipher_info;
931 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100932
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000933 /* cf. RFC 5246, Section 8.1:
934 * "The master secret is always exactly 48 bytes in length." */
935 size_t const master_secret_len = 48;
936
Hanno Becker35b23c72018-10-23 12:10:41 +0100937#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
938 unsigned char session_hash[48];
939#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
940
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200941 mbedtls_ssl_session *session = ssl->session_negotiate;
942 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
943 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000944
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200945 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000946
Jaeden Amero2de07f12019-06-05 13:32:08 +0100947#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC) && \
948 defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000949 transform->encrypt_then_mac = session->encrypt_then_mac;
950#endif
951 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000952
953 ciphersuite_info = handshake->ciphersuite_info;
954 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100955 if( cipher_info == NULL )
956 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200957 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000958 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200959 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100960 }
961
Hanno Beckere694c3e2017-12-27 21:34:08 +0000962 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100963 if( md_info == NULL )
964 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000966 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200967 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100968 }
969
Hanno Beckera0e20d02019-05-15 14:03:01 +0100970#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4bf74652019-04-26 16:22:27 +0100971 /* Copy own and peer's CID if the use of the CID
972 * extension has been negotiated. */
973 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
974 {
975 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100976
Hanno Becker05154c32019-05-03 15:23:51 +0100977 transform->in_cid_len = ssl->own_cid_len;
Hanno Becker05154c32019-05-03 15:23:51 +0100978 memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100979 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100980 transform->in_cid_len );
Hanno Beckerd1f20352019-05-15 10:21:55 +0100981
982 transform->out_cid_len = ssl->handshake->peer_cid_len;
983 memcpy( transform->out_cid, ssl->handshake->peer_cid,
984 ssl->handshake->peer_cid_len );
985 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
986 transform->out_cid_len );
Hanno Becker4bf74652019-04-26 16:22:27 +0100987 }
Hanno Beckera0e20d02019-05-15 14:03:01 +0100988#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker4bf74652019-04-26 16:22:27 +0100989
Paul Bakker5121ce52009-01-03 21:22:43 +0000990 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000991 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000992 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200993#if defined(MBEDTLS_SSL_PROTO_SSL3)
994 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000995 {
Paul Bakker48916f92012-09-16 19:57:18 +0000996 handshake->tls_prf = ssl3_prf;
997 handshake->calc_verify = ssl_calc_verify_ssl;
998 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000999 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001000 else
1001#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001002#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1003 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001004 {
Paul Bakker48916f92012-09-16 19:57:18 +00001005 handshake->tls_prf = tls1_prf;
1006 handshake->calc_verify = ssl_calc_verify_tls;
1007 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001008 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001009 else
1010#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001011#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1012#if defined(MBEDTLS_SHA512_C)
1013 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +00001014 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001015 {
Paul Bakker48916f92012-09-16 19:57:18 +00001016 handshake->tls_prf = tls_prf_sha384;
1017 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1018 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001019 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001020 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001021#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001022#if defined(MBEDTLS_SHA256_C)
1023 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001024 {
Paul Bakker48916f92012-09-16 19:57:18 +00001025 handshake->tls_prf = tls_prf_sha256;
1026 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1027 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001028 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029 else
1030#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001031#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1034 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001035 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001036
1037 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001038 * SSLv3:
1039 * master =
1040 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1041 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1042 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001043 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001044 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001045 * master = PRF( premaster, "master secret", randbytes )[0..47]
1046 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001047 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001048 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001049 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1050 }
1051 else
1052 {
1053 /* The label for the KDF used for key expansion.
1054 * This is either "master secret" or "extended master secret"
1055 * depending on whether the Extended Master Secret extension
1056 * is used. */
1057 char const *lbl = "master secret";
1058
1059 /* The salt for the KDF used for key expansion.
1060 * - If the Extended Master Secret extension is not used,
1061 * this is ClientHello.Random + ServerHello.Random
1062 * (see Sect. 8.1 in RFC 5246).
1063 * - If the Extended Master Secret extension is used,
1064 * this is the transcript of the handshake so far.
1065 * (see Sect. 4 in RFC 7627). */
1066 unsigned char const *salt = handshake->randbytes;
1067 size_t salt_len = 64;
1068
1069#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001070 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001071 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001072 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001073
Hanno Becker35b23c72018-10-23 12:10:41 +01001074 lbl = "extended master secret";
1075 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001076 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1078 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001080#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001081 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1082 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001083 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001084 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001085 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001086#endif /* MBEDTLS_SHA512_C */
1087 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001088 }
1089 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001090#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001091 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001092
Hanno Becker35b23c72018-10-23 12:10:41 +01001093 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001094 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001095#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1096
Hanno Becker7d0a5692018-10-23 15:26:22 +01001097#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1098 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1099 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1100 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1101 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001102 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001103 /* Perform PSK-to-MS expansion in a single step. */
1104 psa_status_t status;
1105 psa_algorithm_t alg;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001106 psa_key_handle_t psk;
Janos Follathda6ac012019-08-16 13:47:29 +01001107 psa_key_derivation_operation_t derivation =
Janos Follath8dee8772019-07-30 12:53:32 +01001108 PSA_KEY_DERIVATION_OPERATION_INIT;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001109
1110 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1111
1112 psk = ssl->conf->psk_opaque;
1113 if( ssl->handshake->psk_opaque != 0 )
1114 psk = ssl->handshake->psk_opaque;
1115
Hanno Becker22bf1452019-04-05 11:21:08 +01001116 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001117 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1118 else
1119 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1120
Janos Follathda6ac012019-08-16 13:47:29 +01001121 status = psa_key_derivation( &derivation, psk, alg,
Hanno Becker7d0a5692018-10-23 15:26:22 +01001122 salt, salt_len,
1123 (unsigned char const *) lbl,
1124 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001125 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001126 if( status != PSA_SUCCESS )
1127 {
Janos Follathda6ac012019-08-16 13:47:29 +01001128 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001129 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1130 }
1131
Janos Follathda6ac012019-08-16 13:47:29 +01001132 status = psa_key_derivation_output_bytes( &derivation,
Janos Follath6de99db2019-07-30 12:55:06 +01001133 session->master,
1134 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001135 if( status != PSA_SUCCESS )
1136 {
Janos Follathda6ac012019-08-16 13:47:29 +01001137 psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001138 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1139 }
1140
Janos Follathda6ac012019-08-16 13:47:29 +01001141 status = psa_key_derivation_abort( &derivation );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001142 if( status != PSA_SUCCESS )
1143 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001144 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001145 else
1146#endif
1147 {
1148 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1149 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001150 session->master,
1151 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001152 if( ret != 0 )
1153 {
1154 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1155 return( ret );
1156 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001157
Hanno Becker7d0a5692018-10-23 15:26:22 +01001158 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1159 handshake->premaster,
1160 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001161
Hanno Becker7d0a5692018-10-23 15:26:22 +01001162 mbedtls_platform_zeroize( handshake->premaster,
1163 sizeof(handshake->premaster) );
1164 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001165 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001166
1167 /*
1168 * Swap the client and server random values.
1169 */
Paul Bakker48916f92012-09-16 19:57:18 +00001170 memcpy( tmp, handshake->randbytes, 64 );
1171 memcpy( handshake->randbytes, tmp + 32, 32 );
1172 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001173 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001174
1175 /*
1176 * SSLv3:
1177 * key block =
1178 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1179 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1180 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1181 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1182 * ...
1183 *
1184 * TLSv1:
1185 * key block = PRF( master, "key expansion", randbytes )
1186 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001187 ret = handshake->tls_prf( session->master, 48, "key expansion",
1188 handshake->randbytes, 64, keyblk, 256 );
1189 if( ret != 0 )
1190 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001191 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001192 return( ret );
1193 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001195 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1196 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1197 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1198 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1199 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001200
Paul Bakker5121ce52009-01-03 21:22:43 +00001201 /*
1202 * Determine the appropriate key, IV and MAC length.
1203 */
Paul Bakker68884e32013-01-07 18:20:04 +01001204
Hanno Becker88aaf652017-12-27 08:17:40 +00001205 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001206
Hanno Becker8031d062018-01-03 15:32:31 +00001207#if defined(MBEDTLS_GCM_C) || \
1208 defined(MBEDTLS_CCM_C) || \
1209 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001210 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001211 cipher_info->mode == MBEDTLS_MODE_CCM ||
1212 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001213 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001214 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001215
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001216 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001217 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001218 transform->taglen =
1219 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001220
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001221 /* All modes haves 96-bit IVs;
1222 * GCM and CCM has 4 implicit and 8 explicit bytes
1223 * ChachaPoly has all 12 bytes implicit
1224 */
Paul Bakker68884e32013-01-07 18:20:04 +01001225 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001226 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1227 transform->fixed_ivlen = 12;
1228 else
1229 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001230
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001231 /* Minimum length of encrypted record */
1232 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001233 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001234 }
1235 else
Hanno Becker8031d062018-01-03 15:32:31 +00001236#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1237#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1238 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1239 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001240 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001241 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1243 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001244 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001245 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001246 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001247 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001248
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001249 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001250 mac_key_len = mbedtls_md_get_size( md_info );
1251 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001252
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001254 /*
1255 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1256 * (rfc 6066 page 13 or rfc 2104 section 4),
1257 * so we only need to adjust the length here.
1258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001261 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001262
1263#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1264 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001265 * HMAC implementation which also truncates the key
1266 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001267 mac_key_len = transform->maclen;
1268#endif
1269 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001270#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001271
1272 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001273 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001274
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001275 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001276 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001277 transform->minlen = transform->maclen;
1278 else
Paul Bakker68884e32013-01-07 18:20:04 +01001279 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001280 /*
1281 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001282 * 1. if EtM is in use: one block plus MAC
1283 * otherwise: * first multiple of blocklen greater than maclen
1284 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001285 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001286#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1287 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001288 {
1289 transform->minlen = transform->maclen
1290 + cipher_info->block_size;
1291 }
1292 else
1293#endif
1294 {
1295 transform->minlen = transform->maclen
1296 + cipher_info->block_size
1297 - transform->maclen % cipher_info->block_size;
1298 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001300#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1301 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1302 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001303 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001304 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001305#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001306#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1307 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1308 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001309 {
1310 transform->minlen += transform->ivlen;
1311 }
1312 else
1313#endif
1314 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001315 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001316 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1317 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001318 }
Paul Bakker68884e32013-01-07 18:20:04 +01001319 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001320 }
Hanno Becker8031d062018-01-03 15:32:31 +00001321 else
1322#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1323 {
1324 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1325 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1326 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001327
Hanno Becker88aaf652017-12-27 08:17:40 +00001328 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1329 (unsigned) keylen,
1330 (unsigned) transform->minlen,
1331 (unsigned) transform->ivlen,
1332 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001333
1334 /*
1335 * Finally setup the cipher contexts, IVs and MAC secrets.
1336 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001337#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001338 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001340 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001341 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001342
Paul Bakker68884e32013-01-07 18:20:04 +01001343 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001344 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001346 /*
1347 * This is not used in TLS v1.1.
1348 */
Paul Bakker48916f92012-09-16 19:57:18 +00001349 iv_copy_len = ( transform->fixed_ivlen ) ?
1350 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001351 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1352 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001353 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001354 }
1355 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001356#endif /* MBEDTLS_SSL_CLI_C */
1357#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001358 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001359 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001360 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001361 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001362
Hanno Becker81c7b182017-11-09 18:39:33 +00001363 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001364 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001366 /*
1367 * This is not used in TLS v1.1.
1368 */
Paul Bakker48916f92012-09-16 19:57:18 +00001369 iv_copy_len = ( transform->fixed_ivlen ) ?
1370 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001371 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1372 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001373 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001374 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001375 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001376#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001377 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001378 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001379 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1380 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001381 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001382
Hanno Beckerd56ed242018-01-03 15:32:51 +00001383#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001384#if defined(MBEDTLS_SSL_PROTO_SSL3)
1385 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001386 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001387 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001388 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001389 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001390 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1391 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001392 }
1393
Hanno Becker81c7b182017-11-09 18:39:33 +00001394 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1395 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001396 }
1397 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001398#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1399#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1400 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1401 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001402 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001403 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1404 For AEAD-based ciphersuites, there is nothing to do here. */
1405 if( mac_key_len != 0 )
1406 {
1407 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1408 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1409 }
Paul Bakker68884e32013-01-07 18:20:04 +01001410 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001411 else
1412#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001415 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1416 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001417 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001418#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001420#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1421 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001422 {
1423 int ret = 0;
1424
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001426
Hanno Becker88aaf652017-12-27 08:17:40 +00001427 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001428 transform->iv_enc, transform->iv_dec,
1429 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001430 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001431 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001433 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001434 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1435 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001436 }
1437 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001438#else
1439 ((void) mac_dec);
1440 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001441#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001442
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001443#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1444 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001445 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001446 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1447 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001448 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001449 iv_copy_len );
1450 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001451
1452 if( ssl->conf->f_export_keys_ext != NULL )
1453 {
1454 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1455 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001456 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001457 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001458 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001459 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001460 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001461 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001462#endif
1463
Hanno Beckerf704bef2018-11-16 15:21:18 +00001464#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001465
1466 /* Only use PSA-based ciphers for TLS-1.2.
1467 * That's relevant at least for TLS-1.0, where
1468 * we assume that mbedtls_cipher_crypt() updates
1469 * the structure field for the IV, which the PSA-based
1470 * implementation currently doesn't. */
1471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1472 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001473 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001474 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001475 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001476 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1477 {
1478 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001479 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001480 }
1481
1482 if( ret == 0 )
1483 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001485 psa_fallthrough = 0;
1486 }
1487 else
1488 {
1489 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1490 psa_fallthrough = 1;
1491 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001492 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001493 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001494 psa_fallthrough = 1;
1495#else
1496 psa_fallthrough = 1;
1497#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001498
Hanno Beckercb1cc802018-11-17 22:27:38 +00001499 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001500#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001501 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001502 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001503 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001505 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001506 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001507
Hanno Beckerf704bef2018-11-16 15:21:18 +00001508#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001509 /* Only use PSA-based ciphers for TLS-1.2.
1510 * That's relevant at least for TLS-1.0, where
1511 * we assume that mbedtls_cipher_crypt() updates
1512 * the structure field for the IV, which the PSA-based
1513 * implementation currently doesn't. */
1514#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1515 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001516 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001517 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001518 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001519 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1520 {
1521 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001522 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001523 }
1524
1525 if( ret == 0 )
1526 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001527 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001528 psa_fallthrough = 0;
1529 }
1530 else
1531 {
1532 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1533 psa_fallthrough = 1;
1534 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001535 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001536 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001537 psa_fallthrough = 1;
1538#else
1539 psa_fallthrough = 1;
1540#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001541
Hanno Beckercb1cc802018-11-17 22:27:38 +00001542 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001543#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001544 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 cipher_info ) ) != 0 )
1546 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001547 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001548 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001549 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001552 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001554 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001556 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001557 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001559 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001560 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001561 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001562 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001564 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_CIPHER_MODE_CBC)
1568 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001569 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1571 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001572 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001574 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001575 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001576
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1578 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001579 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001581 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001582 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001583 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001585
Paul Bakker5121ce52009-01-03 21:22:43 +00001586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001588 // Initialize compression
1589 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001590 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001591 {
Paul Bakker16770332013-10-11 09:59:44 +02001592 if( ssl->compress_buf == NULL )
1593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001594 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001595 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001596 if( ssl->compress_buf == NULL )
1597 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001598 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001599 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001600 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1601 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001602 }
1603 }
1604
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001605 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001606
Paul Bakker48916f92012-09-16 19:57:18 +00001607 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1608 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001609
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001610 if( deflateInit( &transform->ctx_deflate,
1611 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001612 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001613 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001614 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001615 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1616 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001617 }
1618 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001622end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001623 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1624 mbedtls_platform_zeroize( handshake->randbytes,
1625 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001626 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627}
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_SSL_PROTO_SSL3)
1630void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001631{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001632 mbedtls_md5_context md5;
1633 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001634 unsigned char pad_1[48];
1635 unsigned char pad_2[48];
1636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001638
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001639 mbedtls_md5_init( &md5 );
1640 mbedtls_sha1_init( &sha1 );
1641
1642 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1643 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001644
Paul Bakker380da532012-04-18 16:10:25 +00001645 memset( pad_1, 0x36, 48 );
1646 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001647
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001648 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1649 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1650 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001651
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001652 mbedtls_md5_starts_ret( &md5 );
1653 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1654 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1655 mbedtls_md5_update_ret( &md5, hash, 16 );
1656 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001657
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001658 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1659 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1660 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001661
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001662 mbedtls_sha1_starts_ret( &sha1 );
1663 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1664 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1665 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1666 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001668 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1669 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001670
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001671 mbedtls_md5_free( &md5 );
1672 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001673
Paul Bakker380da532012-04-18 16:10:25 +00001674 return;
1675}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001676#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1679void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001680{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001681 mbedtls_md5_context md5;
1682 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001684 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001685
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001686 mbedtls_md5_init( &md5 );
1687 mbedtls_sha1_init( &sha1 );
1688
1689 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1690 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001691
Andrzej Kurekeb342242019-01-29 09:14:33 -05001692 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001693 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001695 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1696 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001697
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001698 mbedtls_md5_free( &md5 );
1699 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001700
Paul Bakker380da532012-04-18 16:10:25 +00001701 return;
1702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001704
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001705#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1706#if defined(MBEDTLS_SHA256_C)
1707void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001708{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001709#if defined(MBEDTLS_USE_PSA_CRYPTO)
1710 size_t hash_size;
1711 psa_status_t status;
1712 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1713
1714 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1715 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1716 if( status != PSA_SUCCESS )
1717 {
1718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1719 return;
1720 }
1721
1722 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1723 if( status != PSA_SUCCESS )
1724 {
1725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1726 return;
1727 }
1728 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1730#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001731 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001732
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001733 mbedtls_sha256_init( &sha256 );
1734
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001736
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001737 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001738 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001740 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1741 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001742
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001743 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001745 return;
1746}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001747#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001748
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001749#if defined(MBEDTLS_SHA512_C)
1750void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001751{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001752#if defined(MBEDTLS_USE_PSA_CRYPTO)
1753 size_t hash_size;
1754 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001755 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001756
1757 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001758 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001759 if( status != PSA_SUCCESS )
1760 {
1761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1762 return;
1763 }
1764
Andrzej Kurek972fba52019-01-30 03:29:12 -05001765 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001766 if( status != PSA_SUCCESS )
1767 {
1768 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1769 return;
1770 }
1771 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1772 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1773#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001774 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001775
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001776 mbedtls_sha512_init( &sha512 );
1777
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001778 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001779
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001780 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001781 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001782
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001783 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1784 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001785
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001786 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001787#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001788 return;
1789}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001790#endif /* MBEDTLS_SHA512_C */
1791#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001792
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001793#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1794int 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 +02001795{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001796 unsigned char *p = ssl->handshake->premaster;
1797 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001798 const unsigned char *psk = ssl->conf->psk;
1799 size_t psk_len = ssl->conf->psk_len;
1800
1801 /* If the psk callback was called, use its result */
1802 if( ssl->handshake->psk != NULL )
1803 {
1804 psk = ssl->handshake->psk;
1805 psk_len = ssl->handshake->psk_len;
1806 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001807
1808 /*
1809 * PMS = struct {
1810 * opaque other_secret<0..2^16-1>;
1811 * opaque psk<0..2^16-1>;
1812 * };
1813 * with "other_secret" depending on the particular key exchange
1814 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1816 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001817 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001818 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001820
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001821 *(p++) = (unsigned char)( psk_len >> 8 );
1822 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001823
1824 if( end < p || (size_t)( end - p ) < psk_len )
1825 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1826
1827 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001828 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001829 }
1830 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001831#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1832#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1833 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001834 {
1835 /*
1836 * other_secret already set by the ClientKeyExchange message,
1837 * and is 48 bytes long
1838 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001839 if( end - p < 2 )
1840 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1841
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001842 *p++ = 0;
1843 *p++ = 48;
1844 p += 48;
1845 }
1846 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001847#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1848#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1849 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001850 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001851 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001852 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001853
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001854 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001855 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001856 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001857 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001860 return( ret );
1861 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001862 *(p++) = (unsigned char)( len >> 8 );
1863 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001864 p += len;
1865
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001866 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001867 }
1868 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1870#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1871 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001872 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001873 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001874 size_t zlen;
1875
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001876 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001877 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001878 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001881 return( ret );
1882 }
1883
1884 *(p++) = (unsigned char)( zlen >> 8 );
1885 *(p++) = (unsigned char)( zlen );
1886 p += zlen;
1887
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001888 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1889 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001890 }
1891 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001892#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001894 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1895 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001896 }
1897
1898 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001899 if( end - p < 2 )
1900 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001901
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001902 *(p++) = (unsigned char)( psk_len >> 8 );
1903 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001904
1905 if( end < p || (size_t)( end - p ) < psk_len )
1906 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1907
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001908 memcpy( p, psk, psk_len );
1909 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001910
1911 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1912
1913 return( 0 );
1914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001915#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001917#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001918/*
1919 * SSLv3.0 MAC functions
1920 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001921#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001922static void ssl_mac( mbedtls_md_context_t *md_ctx,
1923 const unsigned char *secret,
1924 const unsigned char *buf, size_t len,
1925 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001926 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001927{
1928 unsigned char header[11];
1929 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001930 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001931 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1932 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001933
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001934 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001935 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001936 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001937 else
Paul Bakker68884e32013-01-07 18:20:04 +01001938 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001939
1940 memcpy( header, ctr, 8 );
1941 header[ 8] = (unsigned char) type;
1942 header[ 9] = (unsigned char)( len >> 8 );
1943 header[10] = (unsigned char)( len );
1944
Paul Bakker68884e32013-01-07 18:20:04 +01001945 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001946 mbedtls_md_starts( md_ctx );
1947 mbedtls_md_update( md_ctx, secret, md_size );
1948 mbedtls_md_update( md_ctx, padding, padlen );
1949 mbedtls_md_update( md_ctx, header, 11 );
1950 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001951 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001952
Paul Bakker68884e32013-01-07 18:20:04 +01001953 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001954 mbedtls_md_starts( md_ctx );
1955 mbedtls_md_update( md_ctx, secret, md_size );
1956 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001957 mbedtls_md_update( md_ctx, out, md_size );
1958 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001959}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001960#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001961
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001962/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001963 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001964#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001965 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1966 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1967 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1968/* This function makes sure every byte in the memory region is accessed
1969 * (in ascending addresses order) */
1970static void ssl_read_memory( unsigned char *p, size_t len )
1971{
1972 unsigned char acc = 0;
1973 volatile unsigned char force;
1974
1975 for( ; len != 0; p++, len-- )
1976 acc ^= *p;
1977
1978 force = acc;
1979 (void) force;
1980}
1981#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1982
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001983/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001984 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001985 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001986
Hanno Beckera0e20d02019-05-15 14:03:01 +01001987#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerd3f8c792019-05-20 15:06:12 +01001988/* This functions transforms a DTLS plaintext fragment and a record content
1989 * type into an instance of the DTLSInnerPlaintext structure:
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001990 *
1991 * struct {
1992 * opaque content[DTLSPlaintext.length];
1993 * ContentType real_type;
1994 * uint8 zeros[length_of_padding];
1995 * } DTLSInnerPlaintext;
1996 *
1997 * Input:
1998 * - `content`: The beginning of the buffer holding the
1999 * plaintext to be wrapped.
2000 * - `*content_size`: The length of the plaintext in Bytes.
2001 * - `max_len`: The number of Bytes available starting from
2002 * `content`. This must be `>= *content_size`.
2003 * - `rec_type`: The desired record content type.
2004 *
2005 * Output:
2006 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
2007 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
2008 *
2009 * Returns:
2010 * - `0` on success.
2011 * - A negative error code if `max_len` didn't offer enough space
2012 * for the expansion.
2013 */
2014static int ssl_cid_build_inner_plaintext( unsigned char *content,
2015 size_t *content_size,
2016 size_t remaining,
2017 uint8_t rec_type )
2018{
2019 size_t len = *content_size;
Hanno Beckerb9ec44f2019-05-13 15:31:17 +01002020 size_t pad = ( MBEDTLS_SSL_CID_PADDING_GRANULARITY -
2021 ( len + 1 ) % MBEDTLS_SSL_CID_PADDING_GRANULARITY ) %
2022 MBEDTLS_SSL_CID_PADDING_GRANULARITY;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002023
2024 /* Write real content type */
2025 if( remaining == 0 )
2026 return( -1 );
2027 content[ len ] = rec_type;
2028 len++;
2029 remaining--;
2030
2031 if( remaining < pad )
2032 return( -1 );
2033 memset( content + len, 0, pad );
2034 len += pad;
2035 remaining -= pad;
2036
2037 *content_size = len;
2038 return( 0 );
2039}
2040
Hanno Becker07dc97d2019-05-20 15:08:01 +01002041/* This function parses a DTLSInnerPlaintext structure.
2042 * See ssl_cid_build_inner_plaintext() for details. */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002043static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2044 size_t *content_size,
2045 uint8_t *rec_type )
2046{
2047 size_t remaining = *content_size;
2048
2049 /* Determine length of padding by skipping zeroes from the back. */
2050 do
2051 {
2052 if( remaining == 0 )
2053 return( -1 );
2054 remaining--;
2055 } while( content[ remaining ] == 0 );
2056
2057 *content_size = remaining;
2058 *rec_type = content[ remaining ];
2059
2060 return( 0 );
2061}
Hanno Beckera0e20d02019-05-15 14:03:01 +01002062#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002063
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002064/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002065 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002066static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002067 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002068 mbedtls_record *rec )
2069{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002070 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002071 *
2072 * additional_data = seq_num + TLSCompressed.type +
2073 * TLSCompressed.version + TLSCompressed.length;
2074 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002075 * For the CID extension, this is extended as follows
2076 * (quoting draft-ietf-tls-dtls-connection-id-05,
2077 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002078 *
2079 * additional_data = seq_num + DTLSPlaintext.type +
2080 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002081 * cid +
2082 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002083 * length_of_DTLSInnerPlaintext;
2084 */
2085
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002086 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2087 add_data[8] = rec->type;
Hanno Beckeredb24f82019-05-20 15:01:46 +01002088 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
Hanno Beckercab87e62019-04-29 13:52:53 +01002089
Hanno Beckera0e20d02019-05-15 14:03:01 +01002090#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002091 if( rec->cid_len != 0 )
2092 {
2093 memcpy( add_data + 11, rec->cid, rec->cid_len );
2094 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2095 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2096 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2097 *add_data_len = 13 + 1 + rec->cid_len;
2098 }
2099 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01002100#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker95e4bbc2019-05-09 11:38:24 +01002101 {
2102 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2103 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
2104 *add_data_len = 13;
2105 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002106}
2107
Hanno Beckera18d1322018-01-03 14:27:32 +00002108int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2109 mbedtls_ssl_transform *transform,
2110 mbedtls_record *rec,
2111 int (*f_rng)(void *, unsigned char *, size_t),
2112 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002113{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002114 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002115 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002116 unsigned char * data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002117 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_OUT_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002118 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002119 size_t post_avail;
2120
2121 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002122#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002123 ((void) ssl);
2124#endif
2125
2126 /* The PRNG is used for dynamic IV generation that's used
2127 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2128#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2129 ( defined(MBEDTLS_AES_C) || \
2130 defined(MBEDTLS_ARIA_C) || \
2131 defined(MBEDTLS_CAMELLIA_C) ) && \
2132 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2133 ((void) f_rng);
2134 ((void) p_rng);
2135#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002138
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002139 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002140 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002141 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2142 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2143 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002144 if( rec == NULL
2145 || rec->buf == NULL
2146 || rec->buf_len < rec->data_offset
2147 || rec->buf_len - rec->data_offset < rec->data_len
Hanno Beckera0e20d02019-05-15 14:03:01 +01002148#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01002149 || rec->cid_len != 0
2150#endif
2151 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002152 {
2153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002154 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002155 }
2156
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002157 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002158 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002159 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002160 data, rec->data_len );
2161
2162 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2163
2164 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2165 {
2166 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2167 (unsigned) rec->data_len,
2168 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2169 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2170 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002171
Hanno Beckera0e20d02019-05-15 14:03:01 +01002172#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002173 /*
2174 * Add CID information
2175 */
2176 rec->cid_len = transform->out_cid_len;
2177 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2178 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002179
2180 if( rec->cid_len != 0 )
2181 {
2182 /*
Hanno Becker07dc97d2019-05-20 15:08:01 +01002183 * Wrap plaintext into DTLSInnerPlaintext structure.
2184 * See ssl_cid_build_inner_plaintext() for more information.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002185 *
Hanno Becker07dc97d2019-05-20 15:08:01 +01002186 * Note that this changes `rec->data_len`, and hence
2187 * `post_avail` needs to be recalculated afterwards.
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002188 */
2189 if( ssl_cid_build_inner_plaintext( data,
2190 &rec->data_len,
2191 post_avail,
2192 rec->type ) != 0 )
2193 {
2194 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2195 }
2196
2197 rec->type = MBEDTLS_SSL_MSG_CID;
2198 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002199#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002200
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002201 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2202
Paul Bakker5121ce52009-01-03 21:22:43 +00002203 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002204 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 */
Hanno Becker52344c22018-01-03 15:24:20 +00002206#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002207 if( mode == MBEDTLS_MODE_STREAM ||
2208 ( mode == MBEDTLS_MODE_CBC
2209#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002210 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002211#endif
2212 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002213 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002214 if( post_avail < transform->maclen )
2215 {
2216 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2217 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2218 }
2219
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002220#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002221 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002222 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002223 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2225 data, rec->data_len, rec->ctr, rec->type, mac );
2226 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002227 }
2228 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002229#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002230#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2231 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002233 {
Hanno Becker992b6872017-11-09 18:57:39 +00002234 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2235
Hanno Beckercab87e62019-04-29 13:52:53 +01002236 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002237
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002238 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002239 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002240 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2241 data, rec->data_len );
2242 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2243 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2244
2245 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002246 }
2247 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002248#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002250 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2251 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002252 }
2253
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002254 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2255 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002256
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002257 rec->data_len += transform->maclen;
2258 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002259 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002260 }
Hanno Becker52344c22018-01-03 15:24:20 +00002261#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002262
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002263 /*
2264 * Encrypt
2265 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002266#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2267 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002268 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002269 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002270 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002271 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002272 "including %d bytes of padding",
2273 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002274
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002275 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2276 transform->iv_enc, transform->ivlen,
2277 data, rec->data_len,
2278 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002279 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002280 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002281 return( ret );
2282 }
2283
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002284 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002285 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002286 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2287 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002288 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002289 }
Paul Bakker68884e32013-01-07 18:20:04 +01002290 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002291#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002292
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002293#if defined(MBEDTLS_GCM_C) || \
2294 defined(MBEDTLS_CCM_C) || \
2295 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002296 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002297 mode == MBEDTLS_MODE_CCM ||
2298 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002299 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002300 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002301 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002302 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002303
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002304 /* Check that there's space for both the authentication tag
2305 * and the explicit IV before and after the record content. */
2306 if( post_avail < transform->taglen ||
2307 rec->data_offset < explicit_iv_len )
2308 {
2309 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2310 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2311 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002312
Paul Bakker68884e32013-01-07 18:20:04 +01002313 /*
2314 * Generate IV
2315 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002316 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2317 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002318 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002319 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002320 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2321 explicit_iv_len );
2322 /* Prefix record content with explicit IV. */
2323 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002324 }
2325 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2326 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002327 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002328 unsigned char i;
2329
2330 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2331
2332 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002333 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002334 }
2335 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002336 {
2337 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002338 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2339 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002340 }
2341
Hanno Beckercab87e62019-04-29 13:52:53 +01002342 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002343
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002344 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2345 iv, transform->ivlen );
2346 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002347 data - explicit_iv_len, explicit_iv_len );
2348 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002349 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002350 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002351 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002352 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002353
Paul Bakker68884e32013-01-07 18:20:04 +01002354 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002355 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002356 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002357
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002358 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002359 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002360 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002361 data, rec->data_len, /* source */
2362 data, &rec->data_len, /* destination */
2363 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002365 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002366 return( ret );
2367 }
2368
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002369 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2370 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002371
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002372 rec->data_len += transform->taglen + explicit_iv_len;
2373 rec->data_offset -= explicit_iv_len;
2374 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002375 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002376 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002377 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002378#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2379#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002380 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002381 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002382 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002383 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002384 size_t padlen, i;
2385 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002386
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002387 /* Currently we're always using minimal padding
2388 * (up to 255 bytes would be allowed). */
2389 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2390 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002391 padlen = 0;
2392
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002393 /* Check there's enough space in the buffer for the padding. */
2394 if( post_avail < padlen + 1 )
2395 {
2396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2397 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2398 }
2399
Paul Bakker5121ce52009-01-03 21:22:43 +00002400 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002401 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002402
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002403 rec->data_len += padlen + 1;
2404 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002406#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002407 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002408 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2409 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002410 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002411 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002412 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002413 if( f_rng == NULL )
2414 {
2415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2416 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2417 }
2418
2419 if( rec->data_offset < transform->ivlen )
2420 {
2421 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2422 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2423 }
2424
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002425 /*
2426 * Generate IV
2427 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002428 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002429 if( ret != 0 )
2430 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 memcpy( data - transform->ivlen, transform->iv_enc,
2433 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002434
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002435 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002436#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002438 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002439 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002440 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002441 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002442
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002443 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2444 transform->iv_enc,
2445 transform->ivlen,
2446 data, rec->data_len,
2447 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002449 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002450 return( ret );
2451 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002452
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002453 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002454 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002455 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2456 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002457 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002459#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002461 {
2462 /*
2463 * Save IV in SSL3 and TLS1
2464 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002465 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2466 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002467 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002468 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002469#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002470 {
2471 data -= transform->ivlen;
2472 rec->data_offset -= transform->ivlen;
2473 rec->data_len += transform->ivlen;
2474 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002476#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002477 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002479 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2480
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002481 /*
2482 * MAC(MAC_write_key, seq_num +
2483 * TLSCipherText.type +
2484 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002485 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002486 * IV + // except for TLS 1.0
2487 * ENC(content + padding + padding_length));
2488 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002489
2490 if( post_avail < transform->maclen)
2491 {
2492 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2493 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2494 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002495
Hanno Beckercab87e62019-04-29 13:52:53 +01002496 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002498 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002499 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002500 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002501
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002502 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002503 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002504 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2505 data, rec->data_len );
2506 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2507 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002508
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002509 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002510
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002511 rec->data_len += transform->maclen;
2512 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002513 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002514 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002515#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002516 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002517 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002518#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002519 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002521 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2522 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002523 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002524
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002525 /* Make extra sure authentication was performed, exactly once */
2526 if( auth_done != 1 )
2527 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2529 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002530 }
2531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002532 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002533
2534 return( 0 );
2535}
2536
Hanno Beckera18d1322018-01-03 14:27:32 +00002537int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2538 mbedtls_ssl_transform *transform,
2539 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002540{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002541 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002542 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002543 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002544#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002545 size_t padlen = 0, correct = 1;
2546#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002547 unsigned char* data;
Hanno Becker92fb4fa2019-05-20 14:54:26 +01002548 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_IN_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002549 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550
Hanno Beckera18d1322018-01-03 14:27:32 +00002551#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002552 ((void) ssl);
2553#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002555 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002556 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002557 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2560 }
2561 if( rec == NULL ||
2562 rec->buf == NULL ||
2563 rec->buf_len < rec->data_offset ||
2564 rec->buf_len - rec->data_offset < rec->data_len )
2565 {
2566 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002567 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002568 }
2569
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002570 data = rec->buf + rec->data_offset;
2571 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002572
Hanno Beckera0e20d02019-05-15 14:03:01 +01002573#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckercab87e62019-04-29 13:52:53 +01002574 /*
2575 * Match record's CID with incoming CID.
2576 */
Hanno Becker938489a2019-05-08 13:02:22 +01002577 if( rec->cid_len != transform->in_cid_len ||
2578 memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 )
2579 {
Hanno Becker8367ccc2019-05-14 11:30:10 +01002580 return( MBEDTLS_ERR_SSL_UNEXPECTED_CID );
Hanno Becker938489a2019-05-08 13:02:22 +01002581 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01002582#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01002583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002584#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2585 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002586 {
2587 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002588 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2589 transform->iv_dec,
2590 transform->ivlen,
2591 data, rec->data_len,
2592 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002593 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002594 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002595 return( ret );
2596 }
2597
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002598 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002599 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002600 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2601 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002602 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002603 }
Paul Bakker68884e32013-01-07 18:20:04 +01002604 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002605#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002606#if defined(MBEDTLS_GCM_C) || \
2607 defined(MBEDTLS_CCM_C) || \
2608 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002609 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002610 mode == MBEDTLS_MODE_CCM ||
2611 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002612 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002614 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002615
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002616 /*
2617 * Compute and update sizes
2618 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002619 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002620 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002621 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002622 "+ taglen (%d)", rec->data_len,
2623 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002624 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002625 }
Paul Bakker68884e32013-01-07 18:20:04 +01002626
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002627 /*
2628 * Prepare IV
2629 */
2630 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2631 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002632 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002633 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002634 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002635
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002636 }
2637 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2638 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002639 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002640 unsigned char i;
2641
2642 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2643
2644 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002645 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002646 }
2647 else
2648 {
2649 /* Reminder if we ever add an AEAD mode with a different size */
2650 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2651 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2652 }
2653
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002654 data += explicit_iv_len;
2655 rec->data_offset += explicit_iv_len;
2656 rec->data_len -= explicit_iv_len + transform->taglen;
2657
Hanno Beckercab87e62019-04-29 13:52:53 +01002658 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002659 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002660 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002661
2662 memcpy( transform->iv_dec + transform->fixed_ivlen,
2663 data - explicit_iv_len, explicit_iv_len );
2664
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002665 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002666 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002667 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002668
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002669
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002670 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002671 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002672 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002673 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2674 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002675 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002676 data, rec->data_len,
2677 data, &olen,
2678 data + rec->data_len,
2679 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002680 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002681 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002683 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2684 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002685
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002686 return( ret );
2687 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002688 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002689
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002690 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002691 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002692 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2693 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002694 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002695 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002696 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002697#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2698#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002699 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002700 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002701 {
Paul Bakkere47b34b2013-02-27 14:48:00 +01002702 size_t minlen = 0;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002703
Paul Bakker5121ce52009-01-03 21:22:43 +00002704 /*
Paul Bakker45829992013-01-03 14:52:21 +01002705 * Check immediate ciphertext sanity
Paul Bakker5121ce52009-01-03 21:22:43 +00002706 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002707#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002708 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
2709 {
2710 /* The ciphertext is prefixed with the CBC IV. */
2711 minlen += transform->ivlen;
2712 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002713#endif
Paul Bakker45829992013-01-03 14:52:21 +01002714
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002715 /* Size considerations:
2716 *
2717 * - The CBC cipher text must not be empty and hence
2718 * at least of size transform->ivlen.
2719 *
2720 * Together with the potential IV-prefix, this explains
2721 * the first of the two checks below.
2722 *
2723 * - The record must contain a MAC, either in plain or
2724 * encrypted, depending on whether Encrypt-then-MAC
2725 * is used or not.
2726 * - If it is, the message contains the IV-prefix,
2727 * the CBC ciphertext, and the MAC.
2728 * - If it is not, the padded plaintext, and hence
2729 * the CBC ciphertext, has at least length maclen + 1
2730 * because there is at least the padding length byte.
2731 *
2732 * As the CBC ciphertext is not empty, both cases give the
2733 * lower bound minlen + maclen + 1 on the record size, which
2734 * we test for in the second check below.
2735 */
2736 if( rec->data_len < minlen + transform->ivlen ||
2737 rec->data_len < minlen + transform->maclen + 1 )
Paul Bakker45829992013-01-03 14:52:21 +01002738 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002739 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < max( ivlen(%d), maclen (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002740 "+ 1 ) ( + expl IV )", rec->data_len,
2741 transform->ivlen,
2742 transform->maclen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002743 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Paul Bakker45829992013-01-03 14:52:21 +01002744 }
2745
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002746 /*
2747 * Authenticate before decrypt if enabled
2748 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002749#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002750 if( transform->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002751 {
Hanno Becker992b6872017-11-09 18:57:39 +00002752 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002755
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002756 /* Safe due to the check data_len >= minlen + maclen + 1 above. */
2757 rec->data_len -= transform->maclen;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002758
Hanno Beckercab87e62019-04-29 13:52:53 +01002759 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002760
Hanno Beckercab87e62019-04-29 13:52:53 +01002761 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
2762 add_data_len );
2763 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
2764 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002765 mbedtls_md_hmac_update( &transform->md_ctx_dec,
2766 data, rec->data_len );
2767 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
2768 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002769
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002770 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len,
2771 transform->maclen );
Hanno Becker992b6872017-11-09 18:57:39 +00002772 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002773 transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002774
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002775 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
2776 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002777 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002778 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002779 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002780 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002781 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002782 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002783#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002784
2785 /*
2786 * Check length sanity
2787 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002788 if( rec->data_len % transform->ivlen != 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002789 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002790 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) %% ivlen (%d) != 0",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002791 rec->data_len, transform->ivlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002792 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002793 }
2794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002795#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002796 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002797 * Initialize for prepended IV for block cipher in TLS v1.1 and up
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002798 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002799 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002800 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002801 /* This is safe because data_len >= minlen + maclen + 1 initially,
2802 * and at this point we have at most subtracted maclen (note that
2803 * minlen == transform->ivlen here). */
2804 memcpy( transform->iv_dec, data, transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002805
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002806 data += transform->ivlen;
2807 rec->data_offset += transform->ivlen;
2808 rec->data_len -= transform->ivlen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002809 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002810#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002811
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002812 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2813 transform->iv_dec, transform->ivlen,
2814 data, rec->data_len, data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002815 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002816 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002817 return( ret );
2818 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002819
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002820 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2823 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002824 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002826#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002827 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002828 {
2829 /*
2830 * Save IV in SSL3 and TLS1
2831 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002832 memcpy( transform->iv_dec, transform->cipher_ctx_dec.iv,
2833 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002834 }
Paul Bakkercca5b812013-08-31 17:40:26 +02002835#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002836
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002837 /* Safe since data_len >= minlen + maclen + 1, so after having
2838 * subtracted at most minlen and maclen up to this point,
2839 * data_len > 0. */
2840 padlen = data[rec->data_len - 1];
Paul Bakker45829992013-01-03 14:52:21 +01002841
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002842 if( auth_done == 1 )
2843 {
2844 correct *= ( rec->data_len >= padlen + 1 );
2845 padlen *= ( rec->data_len >= padlen + 1 );
2846 }
2847 else
Paul Bakker45829992013-01-03 14:52:21 +01002848 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002849#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002850 if( rec->data_len < transform->maclen + padlen + 1 )
2851 {
2852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < maclen (%d) + padlen (%d)",
2853 rec->data_len,
2854 transform->maclen,
2855 padlen + 1 ) );
2856 }
Paul Bakkerd66f0702013-01-31 16:57:45 +01002857#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002858
2859 correct *= ( rec->data_len >= transform->maclen + padlen + 1 );
2860 padlen *= ( rec->data_len >= transform->maclen + padlen + 1 );
Paul Bakker45829992013-01-03 14:52:21 +01002861 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002862
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002863 padlen++;
2864
2865 /* Regardless of the validity of the padding,
2866 * we have data_len >= padlen here. */
2867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002868#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002869 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002870 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002871 if( padlen > transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002873#if defined(MBEDTLS_SSL_DEBUG_ALL)
2874 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding length: is %d, "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002875 "should be no more than %d",
2876 padlen, transform->ivlen ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002877#endif
Paul Bakker45829992013-01-03 14:52:21 +01002878 correct = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002879 }
2880 }
2881 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002882#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2883#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2884 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002885 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00002886 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002887 /* The padding check involves a series of up to 256
2888 * consecutive memory reads at the end of the record
2889 * plaintext buffer. In order to hide the length and
2890 * validity of the padding, always perform exactly
2891 * `min(256,plaintext_len)` reads (but take into account
2892 * only the last `padlen` bytes for the padding check). */
2893 size_t pad_count = 0;
2894 size_t real_count = 0;
2895 volatile unsigned char* const check = data;
Paul Bakkere47b34b2013-02-27 14:48:00 +01002896
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002897 /* Index of first padding byte; it has been ensured above
2898 * that the subtraction is safe. */
2899 size_t const padding_idx = rec->data_len - padlen;
2900 size_t const num_checks = rec->data_len <= 256 ? rec->data_len : 256;
2901 size_t const start_idx = rec->data_len - num_checks;
2902 size_t idx;
Paul Bakker956c9e02013-12-19 14:42:28 +01002903
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002904 for( idx = start_idx; idx < rec->data_len; idx++ )
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002905 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002906 real_count |= ( idx >= padding_idx );
2907 pad_count += real_count * ( check[idx] == padlen - 1 );
Paul Bakkerca9c87e2013-09-25 18:52:37 +02002908 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002909 correct &= ( pad_count == padlen );
Paul Bakkere47b34b2013-02-27 14:48:00 +01002910
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002911#if defined(MBEDTLS_SSL_DEBUG_ALL)
Paul Bakker66d5d072014-06-17 16:39:18 +02002912 if( padlen > 0 && correct == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002913 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad padding byte detected" ) );
Paul Bakkerd66f0702013-01-31 16:57:45 +01002914#endif
Paul Bakkere47b34b2013-02-27 14:48:00 +01002915 padlen &= correct * 0x1FF;
Paul Bakker5121ce52009-01-03 21:22:43 +00002916 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002917 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
2919 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02002920 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2922 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02002923 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002924
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002925 /* If the padding was found to be invalid, padlen == 0
2926 * and the subtraction is safe. If the padding was found valid,
2927 * padlen hasn't been changed and the previous assertion
2928 * data_len >= padlen still holds. */
2929 rec->data_len -= padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002930 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002931 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002932#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002933 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002934 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002935 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2936 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002937 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002938
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002939#if defined(MBEDTLS_SSL_DEBUG_ALL)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002940 MBEDTLS_SSL_DEBUG_BUF( 4, "raw buffer after decryption",
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002941 data, rec->data_len );
Manuel Pégourié-Gonnard6a25cfa2018-07-10 11:15:36 +02002942#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002943
2944 /*
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002945 * Authenticate if not done yet.
2946 * Compute the MAC regardless of the padding result (RFC4346, CBCTIME).
Paul Bakker5121ce52009-01-03 21:22:43 +00002947 */
Hanno Becker52344c22018-01-03 15:24:20 +00002948#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002949 if( auth_done == 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002950 {
Hanno Becker992b6872017-11-09 18:57:39 +00002951 unsigned char mac_expect[MBEDTLS_SSL_MAC_ADD];
Paul Bakker1e5369c2013-12-19 16:40:57 +01002952
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002953 /* If the initial value of padlen was such that
2954 * data_len < maclen + padlen + 1, then padlen
2955 * got reset to 1, and the initial check
2956 * data_len >= minlen + maclen + 1
2957 * guarantees that at this point we still
2958 * have at least data_len >= maclen.
2959 *
2960 * If the initial value of padlen was such that
2961 * data_len >= maclen + padlen + 1, then we have
2962 * subtracted either padlen + 1 (if the padding was correct)
2963 * or 0 (if the padding was incorrect) since then,
2964 * hence data_len >= maclen in any case.
2965 */
2966 rec->data_len -= transform->maclen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002967
Hanno Beckercab87e62019-04-29 13:52:53 +01002968 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002970#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002971 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002972 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002973 ssl_mac( &transform->md_ctx_dec,
2974 transform->mac_dec,
2975 data, rec->data_len,
2976 rec->ctr, rec->type,
2977 mac_expect );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002978 }
2979 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#endif /* MBEDTLS_SSL_PROTO_SSL3 */
2981#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2982 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002983 if( transform->minor_ver > MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002984 {
2985 /*
2986 * Process MAC and always update for padlen afterwards to make
Gilles Peskine20b44082018-05-29 14:06:49 +02002987 * total time independent of padlen.
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002988 *
2989 * Known timing attacks:
2990 * - Lucky Thirteen (http://www.isg.rhul.ac.uk/tls/TLStiming.pdf)
2991 *
Gilles Peskine20b44082018-05-29 14:06:49 +02002992 * To compensate for different timings for the MAC calculation
2993 * depending on how much padding was removed (which is determined
2994 * by padlen), process extra_run more blocks through the hash
2995 * function.
2996 *
2997 * The formula in the paper is
2998 * extra_run = ceil( (L1-55) / 64 ) - ceil( (L2-55) / 64 )
2999 * where L1 is the size of the header plus the decrypted message
3000 * plus CBC padding and L2 is the size of the header plus the
3001 * decrypted message. This is for an underlying hash function
3002 * with 64-byte blocks.
3003 * We use ( (Lx+8) / 64 ) to handle 'negative Lx' values
3004 * correctly. We round down instead of up, so -56 is the correct
3005 * value for our calculations instead of -55.
3006 *
Gilles Peskine1bd9d582018-06-04 11:58:44 +02003007 * Repeat the formula rather than defining a block_size variable.
3008 * This avoids requiring division by a variable at runtime
3009 * (which would be marginally less efficient and would require
3010 * linking an extra division function in some builds).
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003011 */
3012 size_t j, extra_run = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003013 unsigned char tmp[MBEDTLS_MD_MAX_BLOCK_SIZE];
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003014
3015 /*
3016 * The next two sizes are the minimum and maximum values of
3017 * in_msglen over all padlen values.
3018 *
3019 * They're independent of padlen, since we previously did
3020 * in_msglen -= padlen.
3021 *
3022 * Note that max_len + maclen is never more than the buffer
3023 * length, as we previously did in_msglen -= maclen too.
3024 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003025 const size_t max_len = rec->data_len + padlen;
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003026 const size_t min_len = ( max_len > 256 ) ? max_len - 256 : 0;
3027
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003028 memset( tmp, 0, sizeof( tmp ) );
3029
3030 switch( mbedtls_md_get_type( transform->md_ctx_dec.md_info ) )
Gilles Peskine20b44082018-05-29 14:06:49 +02003031 {
Gilles Peskined0e55a42018-06-04 12:03:30 +02003032#if defined(MBEDTLS_MD5_C) || defined(MBEDTLS_SHA1_C) || \
3033 defined(MBEDTLS_SHA256_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003034 case MBEDTLS_MD_MD5:
3035 case MBEDTLS_MD_SHA1:
Gilles Peskine20b44082018-05-29 14:06:49 +02003036 case MBEDTLS_MD_SHA256:
Gilles Peskine20b44082018-05-29 14:06:49 +02003037 /* 8 bytes of message size, 64-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003038 extra_run =
3039 ( add_data_len + rec->data_len + padlen + 8 ) / 64 -
3040 ( add_data_len + rec->data_len + 8 ) / 64;
Gilles Peskine20b44082018-05-29 14:06:49 +02003041 break;
3042#endif
Gilles Peskinea7fe25d2018-06-04 12:01:18 +02003043#if defined(MBEDTLS_SHA512_C)
Gilles Peskine20b44082018-05-29 14:06:49 +02003044 case MBEDTLS_MD_SHA384:
Gilles Peskine20b44082018-05-29 14:06:49 +02003045 /* 16 bytes of message size, 128-byte compression blocks */
Hanno Beckercab87e62019-04-29 13:52:53 +01003046 extra_run =
3047 ( add_data_len + rec->data_len + padlen + 16 ) / 128 -
3048 ( add_data_len + rec->data_len + 16 ) / 128;
Gilles Peskine20b44082018-05-29 14:06:49 +02003049 break;
3050#endif
3051 default:
Gilles Peskine5c389842018-06-04 12:02:43 +02003052 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Gilles Peskine20b44082018-05-29 14:06:49 +02003053 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3054 }
Paul Bakkere47b34b2013-02-27 14:48:00 +01003055
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003056 extra_run &= correct * 0xFF;
Paul Bakkere47b34b2013-02-27 14:48:00 +01003057
Hanno Beckercab87e62019-04-29 13:52:53 +01003058 mbedtls_md_hmac_update( &transform->md_ctx_dec, add_data,
3059 add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003060 mbedtls_md_hmac_update( &transform->md_ctx_dec, data,
3061 rec->data_len );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003062 /* Make sure we access everything even when padlen > 0. This
3063 * makes the synchronisation requirements for just-in-time
3064 * Prime+Probe attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003065 ssl_read_memory( data + rec->data_len, padlen );
3066 mbedtls_md_hmac_finish( &transform->md_ctx_dec, mac_expect );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003067
3068 /* Call mbedtls_md_process at least once due to cache attacks
3069 * that observe whether md_process() was called of not */
Manuel Pégourié-Gonnard47fede02015-04-29 01:35:48 +02003070 for( j = 0; j < extra_run + 1; j++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003071 mbedtls_md_process( &transform->md_ctx_dec, tmp );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003072
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003073 mbedtls_md_hmac_reset( &transform->md_ctx_dec );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003074
3075 /* Make sure we access all the memory that could contain the MAC,
3076 * before we check it in the next code block. This makes the
3077 * synchronisation requirements for just-in-time Prime+Probe
3078 * attacks much tighter and hopefully impractical. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003079 ssl_read_memory( data + min_len,
3080 max_len - min_len + transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003081 }
3082 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003083#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
3084 MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003085 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003086 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3087 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003088 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003089
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003090#if defined(MBEDTLS_SSL_DEBUG_ALL)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003091 MBEDTLS_SSL_DEBUG_BUF( 4, "expected mac", mac_expect, transform->maclen );
3092 MBEDTLS_SSL_DEBUG_BUF( 4, "message mac", data + rec->data_len, transform->maclen );
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02003093#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00003094
Hanno Becker2e24c3b2017-12-27 21:28:58 +00003095 if( mbedtls_ssl_safer_memcmp( data + rec->data_len, mac_expect,
3096 transform->maclen ) != 0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003097 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003098#if defined(MBEDTLS_SSL_DEBUG_ALL)
3099 MBEDTLS_SSL_DEBUG_MSG( 1, ( "message mac does not match" ) );
Paul Bakkere47b34b2013-02-27 14:48:00 +01003100#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003101 correct = 0;
3102 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003103 auth_done++;
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02003104 }
Hanno Beckerdd3ab132018-10-17 14:43:14 +01003105
3106 /*
3107 * Finally check the correct flag
3108 */
3109 if( correct == 0 )
3110 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Hanno Becker52344c22018-01-03 15:24:20 +00003111#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003112
3113 /* Make extra sure authentication was performed, exactly once */
3114 if( auth_done != 1 )
3115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3117 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01003118 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003119
Hanno Beckera0e20d02019-05-15 14:03:01 +01003120#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003121 if( rec->cid_len != 0 )
3122 {
3123 ret = ssl_cid_parse_inner_plaintext( data, &rec->data_len,
3124 &rec->type );
3125 if( ret != 0 )
3126 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
3127 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01003128#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01003129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003130 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003131
3132 return( 0 );
3133}
3134
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01003135#undef MAC_NONE
3136#undef MAC_PLAINTEXT
3137#undef MAC_CIPHERTEXT
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00003140/*
3141 * Compression/decompression functions
3142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003143static int ssl_compress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003144{
3145 int ret;
3146 unsigned char *msg_post = ssl->out_msg;
Andrzej Kurek5462e022018-04-20 07:58:53 -04003147 ptrdiff_t bytes_written = ssl->out_msg - ssl->out_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003148 size_t len_pre = ssl->out_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003149 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003151 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003152
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003153 if( len_pre == 0 )
3154 return( 0 );
3155
Paul Bakker2770fbd2012-07-03 13:30:23 +00003156 memcpy( msg_pre, ssl->out_msg, len_pre );
3157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003159 ssl->out_msglen ) );
3160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003161 MBEDTLS_SSL_DEBUG_BUF( 4, "before compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003162 ssl->out_msg, ssl->out_msglen );
3163
Paul Bakker48916f92012-09-16 19:57:18 +00003164 ssl->transform_out->ctx_deflate.next_in = msg_pre;
3165 ssl->transform_out->ctx_deflate.avail_in = len_pre;
3166 ssl->transform_out->ctx_deflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003167 ssl->transform_out->ctx_deflate.avail_out = MBEDTLS_SSL_OUT_BUFFER_LEN - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003168
Paul Bakker48916f92012-09-16 19:57:18 +00003169 ret = deflate( &ssl->transform_out->ctx_deflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003170 if( ret != Z_OK )
3171 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003172 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform compression (%d)", ret ) );
3173 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003174 }
3175
Angus Grattond8213d02016-05-25 20:56:48 +10003176 ssl->out_msglen = MBEDTLS_SSL_OUT_BUFFER_LEN -
Andrzej Kurek5462e022018-04-20 07:58:53 -04003177 ssl->transform_out->ctx_deflate.avail_out - bytes_written;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003178
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003179 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after compression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003180 ssl->out_msglen ) );
3181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003182 MBEDTLS_SSL_DEBUG_BUF( 4, "after compression: output payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003183 ssl->out_msg, ssl->out_msglen );
3184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= compress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003186
3187 return( 0 );
3188}
3189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190static int ssl_decompress_buf( mbedtls_ssl_context *ssl )
Paul Bakker2770fbd2012-07-03 13:30:23 +00003191{
3192 int ret;
3193 unsigned char *msg_post = ssl->in_msg;
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003194 ptrdiff_t header_bytes = ssl->in_msg - ssl->in_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003195 size_t len_pre = ssl->in_msglen;
Paul Bakker16770332013-10-11 09:59:44 +02003196 unsigned char *msg_pre = ssl->compress_buf;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003198 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003199
Paul Bakkerabf2f8f2013-06-30 14:57:46 +02003200 if( len_pre == 0 )
3201 return( 0 );
3202
Paul Bakker2770fbd2012-07-03 13:30:23 +00003203 memcpy( msg_pre, ssl->in_msg, len_pre );
3204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003206 ssl->in_msglen ) );
3207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003208 MBEDTLS_SSL_DEBUG_BUF( 4, "before decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003209 ssl->in_msg, ssl->in_msglen );
3210
Paul Bakker48916f92012-09-16 19:57:18 +00003211 ssl->transform_in->ctx_inflate.next_in = msg_pre;
3212 ssl->transform_in->ctx_inflate.avail_in = len_pre;
3213 ssl->transform_in->ctx_inflate.next_out = msg_post;
Angus Grattond8213d02016-05-25 20:56:48 +10003214 ssl->transform_in->ctx_inflate.avail_out = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003215 header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003216
Paul Bakker48916f92012-09-16 19:57:18 +00003217 ret = inflate( &ssl->transform_in->ctx_inflate, Z_SYNC_FLUSH );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003218 if( ret != Z_OK )
3219 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220 MBEDTLS_SSL_DEBUG_MSG( 1, ( "failed to perform decompression (%d)", ret ) );
3221 return( MBEDTLS_ERR_SSL_COMPRESSION_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003222 }
3223
Angus Grattond8213d02016-05-25 20:56:48 +10003224 ssl->in_msglen = MBEDTLS_SSL_IN_BUFFER_LEN -
Andrzej Kureka9ceef82018-04-24 06:32:44 -04003225 ssl->transform_in->ctx_inflate.avail_out - header_bytes;
Paul Bakker2770fbd2012-07-03 13:30:23 +00003226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003227 MBEDTLS_SSL_DEBUG_MSG( 3, ( "after decompression: msglen = %d, ",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003228 ssl->in_msglen ) );
3229
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003230 MBEDTLS_SSL_DEBUG_BUF( 4, "after decompression: input payload",
Paul Bakker2770fbd2012-07-03 13:30:23 +00003231 ssl->in_msg, ssl->in_msglen );
3232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= decompress buf" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00003234
3235 return( 0 );
3236}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003237#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00003238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003239#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
3240static int ssl_write_hello_request( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003242#if defined(MBEDTLS_SSL_PROTO_DTLS)
3243static int ssl_resend_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003244{
3245 /* If renegotiation is not enforced, retransmit until we would reach max
3246 * timeout if we were using the usual handshake doubling scheme */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003247 if( ssl->conf->renego_max_records < 0 )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003248 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003249 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003250 unsigned char doublings = 1;
3251
3252 while( ratio != 0 )
3253 {
3254 ++doublings;
3255 ratio >>= 1;
3256 }
3257
3258 if( ++ssl->renego_records_seen > doublings )
3259 {
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02003260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "no longer retransmitting hello request" ) );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003261 return( 0 );
3262 }
3263 }
3264
3265 return( ssl_write_hello_request( ssl ) );
3266}
3267#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003268#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003269
Paul Bakker5121ce52009-01-03 21:22:43 +00003270/*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003271 * Fill the input message buffer by appending data to it.
3272 * The amount of data already fetched is in ssl->in_left.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003273 *
3274 * If we return 0, is it guaranteed that (at least) nb_want bytes are
3275 * available (from this read and/or a previous one). Otherwise, an error code
3276 * is returned (possibly EOF or WANT_READ).
3277 *
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003278 * With stream transport (TLS) on success ssl->in_left == nb_want, but
3279 * with datagram transport (DTLS) on success ssl->in_left >= nb_want,
3280 * since we always read a whole datagram at once.
3281 *
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02003282 * For DTLS, it is up to the caller to set ssl->next_record_offset when
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003283 * they're done reading a record.
Paul Bakker5121ce52009-01-03 21:22:43 +00003284 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003285int mbedtls_ssl_fetch_input( mbedtls_ssl_context *ssl, size_t nb_want )
Paul Bakker5121ce52009-01-03 21:22:43 +00003286{
Paul Bakker23986e52011-04-24 08:57:21 +00003287 int ret;
3288 size_t len;
Paul Bakker5121ce52009-01-03 21:22:43 +00003289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003290 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003291
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003292 if( ssl->f_recv == NULL && ssl->f_recv_timeout == NULL )
3293 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003294 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003295 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003296 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003297 }
3298
Angus Grattond8213d02016-05-25 20:56:48 +10003299 if( nb_want > MBEDTLS_SSL_IN_BUFFER_LEN - (size_t)( ssl->in_hdr - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003300 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "requesting more data than fits" ) );
3302 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02003303 }
3304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003306 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00003307 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003308 uint32_t timeout;
3309
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02003310 /* Just to be sure */
3311 if( ssl->f_set_timer == NULL || ssl->f_get_timer == NULL )
3312 {
3313 MBEDTLS_SSL_DEBUG_MSG( 1, ( "You must use "
3314 "mbedtls_ssl_set_timer_cb() for DTLS" ) );
3315 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
3316 }
3317
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003318 /*
3319 * The point is, we need to always read a full datagram at once, so we
3320 * sometimes read more then requested, and handle the additional data.
3321 * It could be the rest of the current record (while fetching the
3322 * header) and/or some other records in the same datagram.
3323 */
3324
3325 /*
3326 * Move to the next record in the already read datagram if applicable
3327 */
3328 if( ssl->next_record_offset != 0 )
3329 {
3330 if( ssl->in_left < ssl->next_record_offset )
3331 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003332 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3333 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003334 }
3335
3336 ssl->in_left -= ssl->next_record_offset;
3337
3338 if( ssl->in_left != 0 )
3339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "next record in same datagram, offset: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003341 ssl->next_record_offset ) );
3342 memmove( ssl->in_hdr,
3343 ssl->in_hdr + ssl->next_record_offset,
3344 ssl->in_left );
3345 }
3346
3347 ssl->next_record_offset = 0;
3348 }
3349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003350 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Paul Bakker5121ce52009-01-03 21:22:43 +00003351 ssl->in_left, nb_want ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003352
3353 /*
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003354 * Done if we already have enough data.
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003355 */
3356 if( nb_want <= ssl->in_left)
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003357 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003359 return( 0 );
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02003360 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003361
3362 /*
Antonin Décimo36e89b52019-01-23 15:24:37 +01003363 * A record can't be split across datagrams. If we need to read but
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003364 * are not at the beginning of a new record, the caller did something
3365 * wrong.
3366 */
3367 if( ssl->in_left != 0 )
3368 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003369 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3370 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003371 }
3372
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003373 /*
3374 * Don't even try to read if time's out already.
3375 * This avoids by-passing the timer when repeatedly receiving messages
3376 * that will end up being dropped.
3377 */
3378 if( ssl_check_timer( ssl ) != 0 )
Hanno Beckere65ce782017-05-22 14:47:48 +01003379 {
3380 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timer has expired" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003381 ret = MBEDTLS_ERR_SSL_TIMEOUT;
Hanno Beckere65ce782017-05-22 14:47:48 +01003382 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003383 else
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003384 {
Angus Grattond8213d02016-05-25 20:56:48 +10003385 len = MBEDTLS_SSL_IN_BUFFER_LEN - ( ssl->in_hdr - ssl->in_buf );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003388 timeout = ssl->handshake->retransmit_timeout;
3389 else
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003390 timeout = ssl->conf->read_timeout;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003391
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003392 MBEDTLS_SSL_DEBUG_MSG( 3, ( "f_recv_timeout: %u ms", timeout ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003393
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003394 if( ssl->f_recv_timeout != NULL )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003395 ret = ssl->f_recv_timeout( ssl->p_bio, ssl->in_hdr, len,
3396 timeout );
3397 else
3398 ret = ssl->f_recv( ssl->p_bio, ssl->in_hdr, len );
3399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003401
3402 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003403 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003404 }
3405
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003406 if( ret == MBEDTLS_ERR_SSL_TIMEOUT )
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "timeout" ) );
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003409 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003411 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003412 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003413 if( ssl_double_retransmit_timeout( ssl ) != 0 )
3414 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake timeout" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003416 return( MBEDTLS_ERR_SSL_TIMEOUT );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003417 }
3418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003419 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003420 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003421 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02003422 return( ret );
3423 }
3424
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003425 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003426 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003427#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003428 else if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003429 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003430 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02003431 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003432 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003433 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003434 return( ret );
3435 }
3436
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01003437 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02003438 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02003440 }
3441
Paul Bakker5121ce52009-01-03 21:22:43 +00003442 if( ret < 0 )
3443 return( ret );
3444
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003445 ssl->in_left = ret;
3446 }
3447 else
3448#endif
3449 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02003451 ssl->in_left, nb_want ) );
3452
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003453 while( ssl->in_left < nb_want )
3454 {
3455 len = nb_want - ssl->in_left;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02003456
3457 if( ssl_check_timer( ssl ) != 0 )
3458 ret = MBEDTLS_ERR_SSL_TIMEOUT;
3459 else
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003460 {
3461 if( ssl->f_recv_timeout != NULL )
3462 {
3463 ret = ssl->f_recv_timeout( ssl->p_bio,
3464 ssl->in_hdr + ssl->in_left, len,
3465 ssl->conf->read_timeout );
3466 }
3467 else
3468 {
3469 ret = ssl->f_recv( ssl->p_bio,
3470 ssl->in_hdr + ssl->in_left, len );
3471 }
3472 }
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003473
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "in_left: %d, nb_want: %d",
Manuel Pégourié-Gonnard07617332015-06-24 23:00:03 +02003475 ssl->in_left, nb_want ) );
3476 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_recv(_timeout)", ret );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003477
3478 if( ret == 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003479 return( MBEDTLS_ERR_SSL_CONN_EOF );
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003480
3481 if( ret < 0 )
3482 return( ret );
3483
mohammad160352aecb92018-03-28 23:41:40 -07003484 if ( (size_t)ret > len || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16035bd15cb2018-02-28 04:30:59 -08003485 {
Darryl Green11999bb2018-03-13 15:22:58 +00003486 MBEDTLS_SSL_DEBUG_MSG( 1,
3487 ( "f_recv returned %d bytes but only %lu were requested",
mohammad160319d392b2018-04-02 07:25:26 -07003488 ret, (unsigned long)len ) );
mohammad16035bd15cb2018-02-28 04:30:59 -08003489 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3490 }
3491
Manuel Pégourié-Gonnardfe98ace2014-03-24 13:13:01 +01003492 ssl->in_left += ret;
3493 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003494 }
3495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003496 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= fetch input" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003497
3498 return( 0 );
3499}
3500
3501/*
3502 * Flush any data not yet written
3503 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003504int mbedtls_ssl_flush_output( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003505{
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01003506 int ret;
Hanno Becker04484622018-08-06 09:49:38 +01003507 unsigned char *buf;
Paul Bakker5121ce52009-01-03 21:22:43 +00003508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003509 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003510
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003511 if( ssl->f_send == NULL )
3512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Bad usage of mbedtls_ssl_set_bio() "
Manuel Pégourié-Gonnard1b511f92015-05-06 15:54:23 +01003514 "or mbedtls_ssl_set_bio()" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003515 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003516 }
3517
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003518 /* Avoid incrementing counter if data is flushed */
3519 if( ssl->out_left == 0 )
3520 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003521 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003522 return( 0 );
3523 }
3524
Paul Bakker5121ce52009-01-03 21:22:43 +00003525 while( ssl->out_left > 0 )
3526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003527 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message length: %d, out_left: %d",
Hanno Becker5903de42019-05-03 14:46:38 +01003528 mbedtls_ssl_out_hdr_len( ssl ) + ssl->out_msglen, ssl->out_left ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003529
Hanno Becker2b1e3542018-08-06 11:19:13 +01003530 buf = ssl->out_hdr - ssl->out_left;
Manuel Pégourié-Gonnarde6bdc442014-09-17 11:34:57 +02003531 ret = ssl->f_send( ssl->p_bio, buf, ssl->out_left );
Paul Bakker186751d2012-05-08 13:16:14 +00003532
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003533 MBEDTLS_SSL_DEBUG_RET( 2, "ssl->f_send", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00003534
3535 if( ret <= 0 )
3536 return( ret );
3537
mohammad160352aecb92018-03-28 23:41:40 -07003538 if( (size_t)ret > ssl->out_left || ( INT_MAX > SIZE_MAX && ret > SIZE_MAX ) )
mohammad16034bbaeb42018-02-22 04:29:04 -08003539 {
Darryl Green11999bb2018-03-13 15:22:58 +00003540 MBEDTLS_SSL_DEBUG_MSG( 1,
3541 ( "f_send returned %d bytes but only %lu bytes were sent",
mohammad160319d392b2018-04-02 07:25:26 -07003542 ret, (unsigned long)ssl->out_left ) );
mohammad16034bbaeb42018-02-22 04:29:04 -08003543 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3544 }
3545
Paul Bakker5121ce52009-01-03 21:22:43 +00003546 ssl->out_left -= ret;
3547 }
3548
Hanno Becker2b1e3542018-08-06 11:19:13 +01003549#if defined(MBEDTLS_SSL_PROTO_DTLS)
3550 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003551 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01003552 ssl->out_hdr = ssl->out_buf;
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003553 }
Hanno Becker2b1e3542018-08-06 11:19:13 +01003554 else
3555#endif
3556 {
3557 ssl->out_hdr = ssl->out_buf + 8;
3558 }
3559 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01003560
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003561 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= flush output" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00003562
3563 return( 0 );
3564}
3565
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003566/*
3567 * Functions to handle the DTLS retransmission state machine
3568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003569#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003570/*
3571 * Append current handshake message to current outgoing flight
3572 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003573static int ssl_flight_append( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003574{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003575 mbedtls_ssl_flight_item *msg;
Hanno Becker3b235902018-08-06 09:54:53 +01003576 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_flight_append" ) );
3577 MBEDTLS_SSL_DEBUG_BUF( 4, "message appended to flight",
3578 ssl->out_msg, ssl->out_msglen );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003579
3580 /* Allocate space for current message */
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003581 if( ( msg = mbedtls_calloc( 1, sizeof( mbedtls_ssl_flight_item ) ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003582 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003583 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed",
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003584 sizeof( mbedtls_ssl_flight_item ) ) );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003585 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003586 }
3587
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02003588 if( ( msg->p = mbedtls_calloc( 1, ssl->out_msglen ) ) == NULL )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003589 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02003590 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc %d bytes failed", ssl->out_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003591 mbedtls_free( msg );
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02003592 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003593 }
3594
3595 /* Copy current handshake message with headers */
3596 memcpy( msg->p, ssl->out_msg, ssl->out_msglen );
3597 msg->len = ssl->out_msglen;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003598 msg->type = ssl->out_msgtype;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003599 msg->next = NULL;
3600
3601 /* Append to the current flight */
3602 if( ssl->handshake->flight == NULL )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003603 ssl->handshake->flight = msg;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003604 else
3605 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003606 mbedtls_ssl_flight_item *cur = ssl->handshake->flight;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003607 while( cur->next != NULL )
3608 cur = cur->next;
3609 cur->next = msg;
3610 }
3611
Hanno Becker3b235902018-08-06 09:54:53 +01003612 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_flight_append" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003613 return( 0 );
3614}
3615
3616/*
3617 * Free the current flight of handshake messages
3618 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003619static void ssl_flight_free( mbedtls_ssl_flight_item *flight )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003620{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003621 mbedtls_ssl_flight_item *cur = flight;
3622 mbedtls_ssl_flight_item *next;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003623
3624 while( cur != NULL )
3625 {
3626 next = cur->next;
3627
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003628 mbedtls_free( cur->p );
3629 mbedtls_free( cur );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003630
3631 cur = next;
3632 }
3633}
3634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003635#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
3636static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02003637#endif
3638
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003639/*
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003640 * Swap transform_out and out_ctr with the alternative ones
3641 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003642static void ssl_swap_epochs( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003643{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003644 mbedtls_ssl_transform *tmp_transform;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003645 unsigned char tmp_out_ctr[8];
3646
3647 if( ssl->transform_out == ssl->handshake->alt_transform_out )
3648 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003649 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003650 return;
3651 }
3652
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003653 MBEDTLS_SSL_DEBUG_MSG( 3, ( "swap epochs" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003654
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003655 /* Swap transforms */
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003656 tmp_transform = ssl->transform_out;
3657 ssl->transform_out = ssl->handshake->alt_transform_out;
3658 ssl->handshake->alt_transform_out = tmp_transform;
3659
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003660 /* Swap epoch + sequence_number */
Hanno Becker19859472018-08-06 09:40:20 +01003661 memcpy( tmp_out_ctr, ssl->cur_out_ctr, 8 );
3662 memcpy( ssl->cur_out_ctr, ssl->handshake->alt_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003663 memcpy( ssl->handshake->alt_out_ctr, tmp_out_ctr, 8 );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003664
3665 /* Adjust to the newly activated transform */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01003666 ssl_update_out_pointers( ssl, ssl->transform_out );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003667
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003668#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
3669 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003671 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003672 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003673 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
3674 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003675 }
3676 }
3677#endif
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003678}
3679
3680/*
3681 * Retransmit the current flight of messages.
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003682 */
3683int mbedtls_ssl_resend( mbedtls_ssl_context *ssl )
3684{
3685 int ret = 0;
3686
3687 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_resend" ) );
3688
3689 ret = mbedtls_ssl_flight_transmit( ssl );
3690
3691 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_resend" ) );
3692
3693 return( ret );
3694}
3695
3696/*
3697 * Transmit or retransmit the current flight of messages.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003698 *
3699 * Need to remember the current message in case flush_output returns
3700 * WANT_WRITE, causing us to exit this function and come back later.
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003701 * This function must be called until state is no longer SENDING.
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003702 */
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003703int mbedtls_ssl_flight_transmit( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003704{
Hanno Becker67bc7c32018-08-06 11:33:50 +01003705 int ret;
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003706 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003707
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003708 if( ssl->handshake->retransmit_state != MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003709 {
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003710 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialise flight transmission" ) );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003711
3712 ssl->handshake->cur_msg = ssl->handshake->flight;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003713 ssl->handshake->cur_msg_p = ssl->handshake->flight->p + 12;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003714 ssl_swap_epochs( ssl );
3715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003716 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_SENDING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003717 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003718
3719 while( ssl->handshake->cur_msg != NULL )
3720 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003721 size_t max_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003722 const mbedtls_ssl_flight_item * const cur = ssl->handshake->cur_msg;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003723
Hanno Beckere1dcb032018-08-17 16:47:58 +01003724 int const is_finished =
3725 ( cur->type == MBEDTLS_SSL_MSG_HANDSHAKE &&
3726 cur->p[0] == MBEDTLS_SSL_HS_FINISHED );
3727
Hanno Becker04da1892018-08-14 13:22:10 +01003728 uint8_t const force_flush = ssl->disable_datagram_packing == 1 ?
3729 SSL_FORCE_FLUSH : SSL_DONT_FORCE_FLUSH;
3730
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003731 /* Swap epochs before sending Finished: we can't do it after
3732 * sending ChangeCipherSpec, in case write returns WANT_READ.
3733 * Must be done before copying, may change out_msg pointer */
Hanno Beckere1dcb032018-08-17 16:47:58 +01003734 if( is_finished && ssl->handshake->cur_msg_p == ( cur->p + 12 ) )
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003735 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003736 MBEDTLS_SSL_DEBUG_MSG( 2, ( "swap epochs to send finished message" ) );
Manuel Pégourié-Gonnardc715aed2014-09-19 21:39:13 +02003737 ssl_swap_epochs( ssl );
3738 }
3739
Hanno Becker67bc7c32018-08-06 11:33:50 +01003740 ret = ssl_get_remaining_payload_in_datagram( ssl );
3741 if( ret < 0 )
3742 return( ret );
3743 max_frag_len = (size_t) ret;
3744
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003745 /* CCS is copied as is, while HS messages may need fragmentation */
3746 if( cur->type == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3747 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01003748 if( max_frag_len == 0 )
3749 {
3750 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3751 return( ret );
3752
3753 continue;
3754 }
3755
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003756 memcpy( ssl->out_msg, cur->p, cur->len );
Hanno Becker67bc7c32018-08-06 11:33:50 +01003757 ssl->out_msglen = cur->len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003758 ssl->out_msgtype = cur->type;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003759
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003760 /* Update position inside current message */
3761 ssl->handshake->cur_msg_p += cur->len;
3762 }
3763 else
3764 {
3765 const unsigned char * const p = ssl->handshake->cur_msg_p;
3766 const size_t hs_len = cur->len - 12;
3767 const size_t frag_off = p - ( cur->p + 12 );
3768 const size_t rem_len = hs_len - frag_off;
Hanno Becker67bc7c32018-08-06 11:33:50 +01003769 size_t cur_hs_frag_len, max_hs_frag_len;
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003770
Hanno Beckere1dcb032018-08-17 16:47:58 +01003771 if( ( max_frag_len < 12 ) || ( max_frag_len == 12 && hs_len != 0 ) )
Manuel Pégourié-Gonnarda1071a52018-08-20 11:56:14 +02003772 {
Hanno Beckere1dcb032018-08-17 16:47:58 +01003773 if( is_finished )
Hanno Becker67bc7c32018-08-06 11:33:50 +01003774 ssl_swap_epochs( ssl );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003775
Hanno Becker67bc7c32018-08-06 11:33:50 +01003776 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3777 return( ret );
3778
3779 continue;
3780 }
3781 max_hs_frag_len = max_frag_len - 12;
3782
3783 cur_hs_frag_len = rem_len > max_hs_frag_len ?
3784 max_hs_frag_len : rem_len;
3785
3786 if( frag_off == 0 && cur_hs_frag_len != hs_len )
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003787 {
3788 MBEDTLS_SSL_DEBUG_MSG( 2, ( "fragmenting handshake message (%u > %u)",
Hanno Becker67bc7c32018-08-06 11:33:50 +01003789 (unsigned) cur_hs_frag_len,
3790 (unsigned) max_hs_frag_len ) );
Manuel Pégourié-Gonnard19c62f92018-08-16 10:50:39 +02003791 }
Manuel Pégourié-Gonnardb747c6c2018-08-12 13:28:53 +02003792
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003793 /* Messages are stored with handshake headers as if not fragmented,
3794 * copy beginning of headers then fill fragmentation fields.
3795 * Handshake headers: type(1) len(3) seq(2) f_off(3) f_len(3) */
3796 memcpy( ssl->out_msg, cur->p, 6 );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003797
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003798 ssl->out_msg[6] = ( ( frag_off >> 16 ) & 0xff );
3799 ssl->out_msg[7] = ( ( frag_off >> 8 ) & 0xff );
3800 ssl->out_msg[8] = ( ( frag_off ) & 0xff );
3801
Hanno Becker67bc7c32018-08-06 11:33:50 +01003802 ssl->out_msg[ 9] = ( ( cur_hs_frag_len >> 16 ) & 0xff );
3803 ssl->out_msg[10] = ( ( cur_hs_frag_len >> 8 ) & 0xff );
3804 ssl->out_msg[11] = ( ( cur_hs_frag_len ) & 0xff );
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003805
3806 MBEDTLS_SSL_DEBUG_BUF( 3, "handshake header", ssl->out_msg, 12 );
3807
Hanno Becker3f7b9732018-08-28 09:53:25 +01003808 /* Copy the handshake message content and set records fields */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003809 memcpy( ssl->out_msg + 12, p, cur_hs_frag_len );
3810 ssl->out_msglen = cur_hs_frag_len + 12;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003811 ssl->out_msgtype = cur->type;
3812
3813 /* Update position inside current message */
Hanno Becker67bc7c32018-08-06 11:33:50 +01003814 ssl->handshake->cur_msg_p += cur_hs_frag_len;
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003815 }
3816
3817 /* If done with the current message move to the next one if any */
3818 if( ssl->handshake->cur_msg_p >= cur->p + cur->len )
3819 {
3820 if( cur->next != NULL )
3821 {
3822 ssl->handshake->cur_msg = cur->next;
3823 ssl->handshake->cur_msg_p = cur->next->p + 12;
3824 }
3825 else
3826 {
3827 ssl->handshake->cur_msg = NULL;
3828 ssl->handshake->cur_msg_p = NULL;
3829 }
3830 }
3831
3832 /* Actually send the message out */
Hanno Becker04da1892018-08-14 13:22:10 +01003833 if( ( ret = mbedtls_ssl_write_record( ssl, force_flush ) ) != 0 )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003835 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003836 return( ret );
3837 }
3838 }
3839
Hanno Becker67bc7c32018-08-06 11:33:50 +01003840 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
3841 return( ret );
3842
Manuel Pégourié-Gonnard28f4bea2017-09-13 14:00:05 +02003843 /* Update state and set timer */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003844 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
3845 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard23b7b702014-09-25 13:50:12 +02003846 else
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003848 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard4e2f2452014-10-02 16:51:56 +02003849 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
3850 }
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003851
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003852 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= mbedtls_ssl_flight_transmit" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003853
3854 return( 0 );
3855}
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003856
3857/*
3858 * To be called when the last message of an incoming flight is received.
3859 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860void mbedtls_ssl_recv_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003861{
3862 /* We won't need to resend that one any more */
3863 ssl_flight_free( ssl->handshake->flight );
3864 ssl->handshake->flight = NULL;
3865 ssl->handshake->cur_msg = NULL;
3866
3867 /* The next incoming flight will start with this msg_seq */
3868 ssl->handshake->in_flight_start_seq = ssl->handshake->in_msg_seq;
3869
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003870 /* We don't want to remember CCS's across flight boundaries. */
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01003871 ssl->handshake->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01003872
Hanno Becker0271f962018-08-16 13:23:47 +01003873 /* Clear future message buffering structure. */
3874 ssl_buffering_free( ssl );
3875
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003876 /* Cancel timer */
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003877 ssl_set_timer( ssl, 0 );
3878
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003879 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3880 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003881 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003882 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003883 }
3884 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02003886}
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003887
3888/*
3889 * To be called when the last message of an outgoing flight is send.
3890 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003891void mbedtls_ssl_send_flight_completed( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003892{
Manuel Pégourié-Gonnard6c1fa3a2014-10-01 16:58:16 +02003893 ssl_reset_retransmit_timeout( ssl );
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +02003894 ssl_set_timer( ssl, ssl->handshake->retransmit_timeout );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003895
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003896 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3897 ssl->in_msg[0] == MBEDTLS_SSL_HS_FINISHED )
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003899 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_FINISHED;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003900 }
3901 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02003903}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003905
Paul Bakker5121ce52009-01-03 21:22:43 +00003906/*
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003907 * Handshake layer functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003908 */
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003909
3910/*
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003911 * Write (DTLS: or queue) current handshake (including CCS) message.
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003912 *
3913 * - fill in handshake headers
3914 * - update handshake checksum
3915 * - DTLS: save message for resending
3916 * - then pass to the record layer
3917 *
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02003918 * DTLS: except for HelloRequest, messages are only queued, and will only be
3919 * actually sent when calling flight_transmit() or resend().
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003920 *
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003921 * Inputs:
3922 * - ssl->out_msglen: 4 + actual handshake message len
3923 * (4 is the size of handshake headers for TLS)
3924 * - ssl->out_msg[0]: the handshake type (ClientHello, ServerHello, etc)
3925 * - ssl->out_msg + 4: the handshake message body
3926 *
Manuel Pégourié-Gonnard065a2a32018-08-20 11:09:26 +02003927 * Outputs, ie state before passing to flight_append() or write_record():
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003928 * - ssl->out_msglen: the length of the record contents
3929 * (including handshake headers but excluding record headers)
3930 * - ssl->out_msg: the record contents (handshake headers + content)
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003931 */
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003932int mbedtls_ssl_write_handshake_msg( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00003933{
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003934 int ret;
3935 const size_t hs_len = ssl->out_msglen - 4;
3936 const unsigned char hs_type = ssl->out_msg[0];
Paul Bakker5121ce52009-01-03 21:22:43 +00003937
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003938 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write handshake message" ) );
3939
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003940 /*
3941 * Sanity checks
3942 */
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003943 if( ssl->out_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003944 ssl->out_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
3945 {
Hanno Beckerc83d2b32018-08-22 16:05:47 +01003946 /* In SSLv3, the client might send a NoCertificate alert. */
3947#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
3948 if( ! ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
3949 ssl->out_msgtype == MBEDTLS_SSL_MSG_ALERT &&
3950 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT ) )
3951#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
3952 {
3953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3954 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3955 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02003956 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003957
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05003958 /* Whenever we send anything different from a
3959 * HelloRequest we should be in a handshake - double check. */
3960 if( ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
3961 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) &&
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003962 ssl->handshake == NULL )
3963 {
3964 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3965 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3966 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02003969 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003970 ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003972 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003973 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
3974 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003975 }
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02003976#endif
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003977
Hanno Beckerb50a2532018-08-06 11:52:54 +01003978 /* Double-check that we did not exceed the bounds
3979 * of the outgoing record buffer.
3980 * This should never fail as the various message
3981 * writing functions must obey the bounds of the
3982 * outgoing record buffer, but better be safe.
3983 *
3984 * Note: We deliberately do not check for the MTU or MFL here.
3985 */
3986 if( ssl->out_msglen > MBEDTLS_SSL_OUT_CONTENT_LEN )
3987 {
3988 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record too large: "
3989 "size %u, maximum %u",
3990 (unsigned) ssl->out_msglen,
3991 (unsigned) MBEDTLS_SSL_OUT_CONTENT_LEN ) );
3992 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
3993 }
3994
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02003995 /*
3996 * Fill handshake headers
3997 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 if( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00003999 {
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004000 ssl->out_msg[1] = (unsigned char)( hs_len >> 16 );
4001 ssl->out_msg[2] = (unsigned char)( hs_len >> 8 );
4002 ssl->out_msg[3] = (unsigned char)( hs_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00004003
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004004 /*
4005 * DTLS has additional fields in the Handshake layer,
4006 * between the length field and the actual payload:
4007 * uint16 message_seq;
4008 * uint24 fragment_offset;
4009 * uint24 fragment_length;
4010 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004012 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004013 {
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004014 /* Make room for the additional DTLS fields */
Angus Grattond8213d02016-05-25 20:56:48 +10004015 if( MBEDTLS_SSL_OUT_CONTENT_LEN - ssl->out_msglen < 8 )
Hanno Becker9648f8b2017-09-18 10:55:54 +01004016 {
4017 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS handshake message too large: "
4018 "size %u, maximum %u",
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004019 (unsigned) ( hs_len ),
Angus Grattond8213d02016-05-25 20:56:48 +10004020 (unsigned) ( MBEDTLS_SSL_OUT_CONTENT_LEN - 12 ) ) );
Hanno Becker9648f8b2017-09-18 10:55:54 +01004021 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4022 }
4023
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004024 memmove( ssl->out_msg + 12, ssl->out_msg + 4, hs_len );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004025 ssl->out_msglen += 8;
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004026
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004027 /* Write message_seq and update it, except for HelloRequest */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004028 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004029 {
Manuel Pégourié-Gonnardd9ba0d92014-09-02 18:30:26 +02004030 ssl->out_msg[4] = ( ssl->handshake->out_msg_seq >> 8 ) & 0xFF;
4031 ssl->out_msg[5] = ( ssl->handshake->out_msg_seq ) & 0xFF;
4032 ++( ssl->handshake->out_msg_seq );
Manuel Pégourié-Gonnardc392b242014-08-19 17:53:11 +02004033 }
4034 else
4035 {
4036 ssl->out_msg[4] = 0;
4037 ssl->out_msg[5] = 0;
4038 }
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004039
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004040 /* Handshake hashes are computed without fragmentation,
4041 * so set frag_offset = 0 and frag_len = hs_len for now */
Manuel Pégourié-Gonnarde89bcf02014-02-18 18:50:02 +01004042 memset( ssl->out_msg + 6, 0x00, 3 );
4043 memcpy( ssl->out_msg + 9, ssl->out_msg + 1, 3 );
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004044 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004046
Hanno Becker0207e532018-08-28 10:28:28 +01004047 /* Update running hashes of handshake messages seen */
Manuel Pégourié-Gonnard9c3a8ca2017-09-13 09:54:27 +02004048 if( hs_type != MBEDTLS_SSL_HS_HELLO_REQUEST )
4049 ssl->handshake->update_checksum( ssl, ssl->out_msg, ssl->out_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00004050 }
4051
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004052 /* Either send now, or just save to be sent (and resent) later */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004053#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004054 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05004055 ! ( ssl->out_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4056 hs_type == MBEDTLS_SSL_HS_HELLO_REQUEST ) )
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004057 {
4058 if( ( ret = ssl_flight_append( ssl ) ) != 0 )
4059 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_flight_append", ret );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004061 return( ret );
4062 }
4063 }
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004064 else
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004065#endif
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004066 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004067 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004068 {
4069 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_write_record", ret );
4070 return( ret );
4071 }
4072 }
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004073
4074 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write handshake message" ) );
4075
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02004076 return( 0 );
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004077}
4078
4079/*
4080 * Record layer functions
4081 */
4082
4083/*
4084 * Write current record.
4085 *
4086 * Uses:
4087 * - ssl->out_msgtype: type of the message (AppData, Handshake, Alert, CCS)
4088 * - ssl->out_msglen: length of the record content (excl headers)
4089 * - ssl->out_msg: record content
4090 */
Hanno Becker67bc7c32018-08-06 11:33:50 +01004091int mbedtls_ssl_write_record( mbedtls_ssl_context *ssl, uint8_t force_flush )
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004092{
4093 int ret, done = 0;
4094 size_t len = ssl->out_msglen;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004095 uint8_t flush = force_flush;
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02004096
4097 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write record" ) );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +02004098
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004099#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00004100 if( ssl->transform_out != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101 ssl->session_out->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00004102 {
4103 if( ( ret = ssl_compress_buf( ssl ) ) != 0 )
4104 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004105 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_compress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00004106 return( ret );
4107 }
4108
4109 len = ssl->out_msglen;
4110 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004111#endif /*MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00004112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004113#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
4114 if( mbedtls_ssl_hw_record_write != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00004115 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004116 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_write()" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004118 ret = mbedtls_ssl_hw_record_write( ssl );
4119 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00004120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004121 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_write", ret );
4122 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00004123 }
Paul Bakkerc7878112012-12-19 14:41:14 +01004124
4125 if( ret == 0 )
4126 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00004127 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004128#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00004129 if( !done )
4130 {
Hanno Becker2b1e3542018-08-06 11:19:13 +01004131 unsigned i;
4132 size_t protected_record_size;
4133
Hanno Becker6430faf2019-05-08 11:57:13 +01004134 /* Skip writing the record content type to after the encryption,
4135 * as it may change when using the CID extension. */
4136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004138 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004139
Hanno Becker19859472018-08-06 09:40:20 +01004140 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004141 ssl->out_len[0] = (unsigned char)( len >> 8 );
4142 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004143
Paul Bakker48916f92012-09-16 19:57:18 +00004144 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004145 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004146 mbedtls_record rec;
4147
4148 rec.buf = ssl->out_iv;
4149 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4150 ( ssl->out_iv - ssl->out_buf );
4151 rec.data_len = ssl->out_msglen;
4152 rec.data_offset = ssl->out_msg - rec.buf;
4153
4154 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4155 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4156 ssl->conf->transport, rec.ver );
4157 rec.type = ssl->out_msgtype;
4158
Hanno Beckera0e20d02019-05-15 14:03:01 +01004159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker43c24b82019-05-01 09:45:57 +01004160 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004161 rec.cid_len = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004162#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004163
Hanno Beckera18d1322018-01-03 14:27:32 +00004164 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004165 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004166 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004167 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004168 return( ret );
4169 }
4170
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004171 if( rec.data_offset != 0 )
4172 {
4173 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4174 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4175 }
4176
Hanno Becker6430faf2019-05-08 11:57:13 +01004177 /* Update the record content type and CID. */
4178 ssl->out_msgtype = rec.type;
Hanno Beckera0e20d02019-05-15 14:03:01 +01004179#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01004180 memcpy( ssl->out_cid, rec.cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01004181#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker78f839d2019-03-14 12:56:23 +00004182 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004183 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4184 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004185 }
4186
Hanno Becker5903de42019-05-03 14:46:38 +01004187 protected_record_size = len + mbedtls_ssl_out_hdr_len( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004188
4189#if defined(MBEDTLS_SSL_PROTO_DTLS)
4190 /* In case of DTLS, double-check that we don't exceed
4191 * the remaining space in the datagram. */
4192 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4193 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004194 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004195 if( ret < 0 )
4196 return( ret );
4197
4198 if( protected_record_size > (size_t) ret )
4199 {
4200 /* Should never happen */
4201 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4202 }
4203 }
4204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004205
Hanno Becker6430faf2019-05-08 11:57:13 +01004206 /* Now write the potentially updated record content type. */
4207 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
4208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004209 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004210 "version = [%d:%d], msglen = %d",
4211 ssl->out_hdr[0], ssl->out_hdr[1],
4212 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004215 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004216
4217 ssl->out_left += protected_record_size;
4218 ssl->out_hdr += protected_record_size;
4219 ssl_update_out_pointers( ssl, ssl->transform_out );
4220
Hanno Becker04484622018-08-06 09:49:38 +01004221 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4222 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4223 break;
4224
4225 /* The loop goes to its end iff the counter is wrapping */
4226 if( i == ssl_ep_len( ssl ) )
4227 {
4228 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4229 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004231 }
4232
Hanno Becker67bc7c32018-08-06 11:33:50 +01004233#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004234 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4235 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004236 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004237 size_t remaining;
4238 ret = ssl_get_remaining_payload_in_datagram( ssl );
4239 if( ret < 0 )
4240 {
4241 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4242 ret );
4243 return( ret );
4244 }
4245
4246 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004247 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004248 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004249 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004250 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004251 else
4252 {
Hanno Becker513815a2018-08-20 11:56:09 +01004253 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004254 }
4255 }
4256#endif /* MBEDTLS_SSL_PROTO_DTLS */
4257
4258 if( ( flush == SSL_FORCE_FLUSH ) &&
4259 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004260 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004262 return( ret );
4263 }
4264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004265 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004266
4267 return( 0 );
4268}
4269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004271
4272static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4273{
4274 if( ssl->in_msglen < ssl->in_hslen ||
4275 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4276 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4277 {
4278 return( 1 );
4279 }
4280 return( 0 );
4281}
Hanno Becker44650b72018-08-16 12:51:11 +01004282
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004283static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004284{
4285 return( ( ssl->in_msg[9] << 16 ) |
4286 ( ssl->in_msg[10] << 8 ) |
4287 ssl->in_msg[11] );
4288}
4289
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004290static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004291{
4292 return( ( ssl->in_msg[6] << 16 ) |
4293 ( ssl->in_msg[7] << 8 ) |
4294 ssl->in_msg[8] );
4295}
4296
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004297static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004298{
4299 uint32_t msg_len, frag_off, frag_len;
4300
4301 msg_len = ssl_get_hs_total_len( ssl );
4302 frag_off = ssl_get_hs_frag_off( ssl );
4303 frag_len = ssl_get_hs_frag_len( ssl );
4304
4305 if( frag_off > msg_len )
4306 return( -1 );
4307
4308 if( frag_len > msg_len - frag_off )
4309 return( -1 );
4310
4311 if( frag_len + 12 > ssl->in_msglen )
4312 return( -1 );
4313
4314 return( 0 );
4315}
4316
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004317/*
4318 * Mark bits in bitmask (used for DTLS HS reassembly)
4319 */
4320static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4321{
4322 unsigned int start_bits, end_bits;
4323
4324 start_bits = 8 - ( offset % 8 );
4325 if( start_bits != 8 )
4326 {
4327 size_t first_byte_idx = offset / 8;
4328
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004329 /* Special case */
4330 if( len <= start_bits )
4331 {
4332 for( ; len != 0; len-- )
4333 mask[first_byte_idx] |= 1 << ( start_bits - len );
4334
4335 /* Avoid potential issues with offset or len becoming invalid */
4336 return;
4337 }
4338
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004339 offset += start_bits; /* Now offset % 8 == 0 */
4340 len -= start_bits;
4341
4342 for( ; start_bits != 0; start_bits-- )
4343 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4344 }
4345
4346 end_bits = len % 8;
4347 if( end_bits != 0 )
4348 {
4349 size_t last_byte_idx = ( offset + len ) / 8;
4350
4351 len -= end_bits; /* Now len % 8 == 0 */
4352
4353 for( ; end_bits != 0; end_bits-- )
4354 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4355 }
4356
4357 memset( mask + offset / 8, 0xFF, len / 8 );
4358}
4359
4360/*
4361 * Check that bitmask is full
4362 */
4363static int ssl_bitmask_check( unsigned char *mask, size_t len )
4364{
4365 size_t i;
4366
4367 for( i = 0; i < len / 8; i++ )
4368 if( mask[i] != 0xFF )
4369 return( -1 );
4370
4371 for( i = 0; i < len % 8; i++ )
4372 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4373 return( -1 );
4374
4375 return( 0 );
4376}
4377
Hanno Becker56e205e2018-08-16 09:06:12 +01004378/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004379static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004380 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004381{
Hanno Becker56e205e2018-08-16 09:06:12 +01004382 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004383
Hanno Becker56e205e2018-08-16 09:06:12 +01004384 alloc_len = 12; /* Handshake header */
4385 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004386
Hanno Beckerd07df862018-08-16 09:14:58 +01004387 if( add_bitmap )
4388 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004389
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004390 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004391}
Hanno Becker56e205e2018-08-16 09:06:12 +01004392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004393#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004394
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004395static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004396{
4397 return( ( ssl->in_msg[1] << 16 ) |
4398 ( ssl->in_msg[2] << 8 ) |
4399 ssl->in_msg[3] );
4400}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004401
Simon Butcher99000142016-10-13 17:21:01 +01004402int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004403{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004404 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004405 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004406 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004407 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004408 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004409 }
4410
Hanno Becker12555c62018-08-16 12:47:53 +01004411 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004412
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004413 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004414 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004415 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004418 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004419 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004420 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004421 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004422
Hanno Becker44650b72018-08-16 12:51:11 +01004423 if( ssl_check_hs_header( ssl ) != 0 )
4424 {
4425 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4426 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4427 }
4428
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004429 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004430 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4431 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4432 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4433 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004434 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004435 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4436 {
4437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4438 recv_msg_seq,
4439 ssl->handshake->in_msg_seq ) );
4440 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4441 }
4442
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004443 /* Retransmit only on last message from previous flight, to avoid
4444 * too many retransmissions.
4445 * Besides, No sane server ever retransmits HelloVerifyRequest */
4446 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004447 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004448 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004449 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004450 "message_seq = %d, start_of_flight = %d",
4451 recv_msg_seq,
4452 ssl->handshake->in_flight_start_seq ) );
4453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004454 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004455 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004456 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004457 return( ret );
4458 }
4459 }
4460 else
4461 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004463 "message_seq = %d, expected = %d",
4464 recv_msg_seq,
4465 ssl->handshake->in_msg_seq ) );
4466 }
4467
Hanno Becker90333da2017-10-10 11:27:13 +01004468 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004469 }
4470 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004471
Hanno Becker6d97ef52018-08-16 13:09:04 +01004472 /* Message reassembly is handled alongside buffering of future
4473 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004474 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004475 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004476 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004478 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004479 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004480 }
4481 }
4482 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004483#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004484 /* With TLS we don't handle fragmentation (for now) */
4485 if( ssl->in_msglen < ssl->in_hslen )
4486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4488 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004489 }
4490
Simon Butcher99000142016-10-13 17:21:01 +01004491 return( 0 );
4492}
4493
4494void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4495{
Hanno Becker0271f962018-08-16 13:23:47 +01004496 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004497
Hanno Becker0271f962018-08-16 13:23:47 +01004498 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004499 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004500 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004501 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004502
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004503 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004504#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004505 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004506 ssl->handshake != NULL )
4507 {
Hanno Becker0271f962018-08-16 13:23:47 +01004508 unsigned offset;
4509 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004510
Hanno Becker0271f962018-08-16 13:23:47 +01004511 /* Increment handshake sequence number */
4512 hs->in_msg_seq++;
4513
4514 /*
4515 * Clear up handshake buffering and reassembly structure.
4516 */
4517
4518 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004519 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004520
4521 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004522 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4523 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004524 offset++, hs_buf++ )
4525 {
4526 *hs_buf = *(hs_buf + 1);
4527 }
4528
4529 /* Create a fresh last entry */
4530 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004531 }
4532#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004533}
4534
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004535/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004536 * DTLS anti-replay: RFC 6347 4.1.2.6
4537 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004538 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4539 * Bit n is set iff record number in_window_top - n has been seen.
4540 *
4541 * Usually, in_window_top is the last record number seen and the lsb of
4542 * in_window is set. The only exception is the initial state (record number 0
4543 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004544 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004545#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4546static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004547{
4548 ssl->in_window_top = 0;
4549 ssl->in_window = 0;
4550}
4551
4552static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4553{
4554 return( ( (uint64_t) buf[0] << 40 ) |
4555 ( (uint64_t) buf[1] << 32 ) |
4556 ( (uint64_t) buf[2] << 24 ) |
4557 ( (uint64_t) buf[3] << 16 ) |
4558 ( (uint64_t) buf[4] << 8 ) |
4559 ( (uint64_t) buf[5] ) );
4560}
4561
4562/*
4563 * Return 0 if sequence number is acceptable, -1 otherwise
4564 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004565int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004566{
4567 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4568 uint64_t bit;
4569
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004570 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004571 return( 0 );
4572
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004573 if( rec_seqnum > ssl->in_window_top )
4574 return( 0 );
4575
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004576 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004577
4578 if( bit >= 64 )
4579 return( -1 );
4580
4581 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4582 return( -1 );
4583
4584 return( 0 );
4585}
4586
4587/*
4588 * Update replay window on new validated record
4589 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004590void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004591{
4592 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4593
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004594 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004595 return;
4596
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004597 if( rec_seqnum > ssl->in_window_top )
4598 {
4599 /* Update window_top and the contents of the window */
4600 uint64_t shift = rec_seqnum - ssl->in_window_top;
4601
4602 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004603 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004604 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004605 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004606 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004607 ssl->in_window |= 1;
4608 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004609
4610 ssl->in_window_top = rec_seqnum;
4611 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612 else
4613 {
4614 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004615 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004616
4617 if( bit < 64 ) /* Always true, but be extra sure */
4618 ssl->in_window |= (uint64_t) 1 << bit;
4619 }
4620}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004621#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004622
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004623#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004624/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004625static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4626
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004627/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004628 * Without any SSL context, check if a datagram looks like a ClientHello with
4629 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004630 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004631 *
4632 * - if cookie is valid, return 0
4633 * - if ClientHello looks superficially valid but cookie is not,
4634 * fill obuf and set olen, then
4635 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4636 * - otherwise return a specific error code
4637 */
4638static int ssl_check_dtls_clihlo_cookie(
4639 mbedtls_ssl_cookie_write_t *f_cookie_write,
4640 mbedtls_ssl_cookie_check_t *f_cookie_check,
4641 void *p_cookie,
4642 const unsigned char *cli_id, size_t cli_id_len,
4643 const unsigned char *in, size_t in_len,
4644 unsigned char *obuf, size_t buf_len, size_t *olen )
4645{
4646 size_t sid_len, cookie_len;
4647 unsigned char *p;
4648
4649 if( f_cookie_write == NULL || f_cookie_check == NULL )
4650 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4651
4652 /*
4653 * Structure of ClientHello with record and handshake headers,
4654 * and expected values. We don't need to check a lot, more checks will be
4655 * done when actually parsing the ClientHello - skipping those checks
4656 * avoids code duplication and does not make cookie forging any easier.
4657 *
4658 * 0-0 ContentType type; copied, must be handshake
4659 * 1-2 ProtocolVersion version; copied
4660 * 3-4 uint16 epoch; copied, must be 0
4661 * 5-10 uint48 sequence_number; copied
4662 * 11-12 uint16 length; (ignored)
4663 *
4664 * 13-13 HandshakeType msg_type; (ignored)
4665 * 14-16 uint24 length; (ignored)
4666 * 17-18 uint16 message_seq; copied
4667 * 19-21 uint24 fragment_offset; copied, must be 0
4668 * 22-24 uint24 fragment_length; (ignored)
4669 *
4670 * 25-26 ProtocolVersion client_version; (ignored)
4671 * 27-58 Random random; (ignored)
4672 * 59-xx SessionID session_id; 1 byte len + sid_len content
4673 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4674 * ...
4675 *
4676 * Minimum length is 61 bytes.
4677 */
4678 if( in_len < 61 ||
4679 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4680 in[3] != 0 || in[4] != 0 ||
4681 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4682 {
4683 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4684 }
4685
4686 sid_len = in[59];
4687 if( sid_len > in_len - 61 )
4688 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4689
4690 cookie_len = in[60 + sid_len];
4691 if( cookie_len > in_len - 60 )
4692 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4693
4694 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4695 cli_id, cli_id_len ) == 0 )
4696 {
4697 /* Valid cookie */
4698 return( 0 );
4699 }
4700
4701 /*
4702 * If we get here, we've got an invalid cookie, let's prepare HVR.
4703 *
4704 * 0-0 ContentType type; copied
4705 * 1-2 ProtocolVersion version; copied
4706 * 3-4 uint16 epoch; copied
4707 * 5-10 uint48 sequence_number; copied
4708 * 11-12 uint16 length; olen - 13
4709 *
4710 * 13-13 HandshakeType msg_type; hello_verify_request
4711 * 14-16 uint24 length; olen - 25
4712 * 17-18 uint16 message_seq; copied
4713 * 19-21 uint24 fragment_offset; copied
4714 * 22-24 uint24 fragment_length; olen - 25
4715 *
4716 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4717 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4718 *
4719 * Minimum length is 28.
4720 */
4721 if( buf_len < 28 )
4722 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4723
4724 /* Copy most fields and adapt others */
4725 memcpy( obuf, in, 25 );
4726 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4727 obuf[25] = 0xfe;
4728 obuf[26] = 0xff;
4729
4730 /* Generate and write actual cookie */
4731 p = obuf + 28;
4732 if( f_cookie_write( p_cookie,
4733 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4734 {
4735 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4736 }
4737
4738 *olen = p - obuf;
4739
4740 /* Go back and fill length fields */
4741 obuf[27] = (unsigned char)( *olen - 28 );
4742
4743 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4744 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4745 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4746
4747 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4748 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4749
4750 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4751}
4752
4753/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004754 * Handle possible client reconnect with the same UDP quadruplet
4755 * (RFC 6347 Section 4.2.8).
4756 *
4757 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4758 * that looks like a ClientHello.
4759 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004760 * - if the input looks like a ClientHello without cookies,
4761 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004762 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004763 * - if the input looks like a ClientHello with a valid cookie,
4764 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004765 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004766 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004767 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004768 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004769 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4770 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004771 */
4772static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4773{
4774 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004775 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004776
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004777 ret = ssl_check_dtls_clihlo_cookie(
4778 ssl->conf->f_cookie_write,
4779 ssl->conf->f_cookie_check,
4780 ssl->conf->p_cookie,
4781 ssl->cli_id, ssl->cli_id_len,
4782 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004783 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004784
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004785 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4786
4787 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004788 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004789 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004790 * If the error is permanent we'll catch it later,
4791 * if it's not, then hopefully it'll work next time. */
4792 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4793
4794 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004795 }
4796
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004797 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004798 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004799 /* Got a valid cookie, partially reset context */
4800 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4801 {
4802 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4803 return( ret );
4804 }
4805
4806 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004807 }
4808
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004809 return( ret );
4810}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004811#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004812
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004813static int ssl_check_record_type( uint8_t record_type )
4814{
4815 if( record_type != MBEDTLS_SSL_MSG_HANDSHAKE &&
4816 record_type != MBEDTLS_SSL_MSG_ALERT &&
4817 record_type != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4818 record_type != MBEDTLS_SSL_MSG_APPLICATION_DATA )
4819 {
4820 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4821 }
4822
4823 return( 0 );
4824}
4825
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004826/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004827 * ContentType type;
4828 * ProtocolVersion version;
4829 * uint16 epoch; // DTLS only
4830 * uint48 sequence_number; // DTLS only
4831 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004832 *
4833 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004834 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004835 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4836 *
4837 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004838 * 1. proceed with the record if this function returns 0
4839 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4840 * 3. return CLIENT_RECONNECT if this function return that value
4841 * 4. drop the whole datagram if this function returns anything else.
4842 * Point 2 is needed when the peer is resending, and we have already received
4843 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004844 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004845static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004846{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004847 int major_ver, minor_ver;
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004848 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00004849
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004850 /* Parse and validate record content type and version */
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004851
Paul Bakker5121ce52009-01-03 21:22:43 +00004852 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004853 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004854
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004855 /* Check record type */
Hanno Beckera0e20d02019-05-15 14:03:01 +01004856#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004857 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4858 ssl->in_msgtype == MBEDTLS_SSL_MSG_CID &&
4859 ssl->conf->cid_len != 0 )
4860 {
4861 /* Shift pointers to account for record header including CID
4862 * struct {
4863 * ContentType special_type = tls12_cid;
4864 * ProtocolVersion version;
4865 * uint16 epoch;
4866 * uint48 sequence_number;
Hanno Becker8e55b0f2019-05-23 17:03:19 +01004867 * opaque cid[cid_length]; // Additional field compared to
4868 * // default DTLS record format
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004869 * uint16 length;
4870 * opaque enc_content[DTLSCiphertext.length];
4871 * } DTLSCiphertext;
4872 */
4873
4874 /* So far, we only support static CID lengths
4875 * fixed in the configuration. */
4876 ssl->in_len = ssl->in_cid + ssl->conf->cid_len;
4877 ssl->in_iv = ssl->in_msg = ssl->in_len + 2;
4878 }
4879 else
Hanno Beckera0e20d02019-05-15 14:03:01 +01004880#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf661c9c2019-05-03 13:25:54 +01004881 if( ssl_check_record_type( ssl->in_msgtype ) )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004882 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004883 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004884
4885#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004886 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4887 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004888 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4889#endif /* MBEDTLS_SSL_PROTO_DTLS */
4890 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4891 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004893 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004894 }
4895
4896 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004897 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004898 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004899 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4900 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004901 }
4902
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004903 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004904 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004905 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4906 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004907 }
4908
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004909 /* Now that the total length of the record header is known, ensure
4910 * that the current datagram is large enough to hold it.
4911 * This would fail, for example, if we received a datagram of
4912 * size 13 + n Bytes where n is less than the size of incoming CIDs. */
4913 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
4914 if( ret != 0 )
4915 {
4916 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
4917 return( ret );
4918 }
4919 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) );
4920
4921 /* Parse and validate record length
4922 * This must happen after the CID parsing because
4923 * its position in the record header depends on
4924 * the presence of a CID. */
4925
4926 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Angus Grattond8213d02016-05-25 20:56:48 +10004927 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004928 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004929 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004930 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4931 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004932 }
4933
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004934 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Hanno Becker92d30f52019-05-23 17:03:44 +01004935 "version = [%d:%d], msglen = %d",
4936 ssl->in_msgtype,
4937 major_ver, minor_ver, ssl->in_msglen ) );
Hanno Beckerca59c2b2019-05-08 12:03:28 +01004938
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004939 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004940 * DTLS-related tests.
4941 * Check epoch before checking length constraint because
4942 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4943 * message gets duplicated before the corresponding Finished message,
4944 * the second ChangeCipherSpec should be discarded because it belongs
4945 * to an old epoch, but not because its length is shorter than
4946 * the minimum record length for packets using the new record transform.
4947 * Note that these two kinds of failures are handled differently,
4948 * as an unexpected record is silently skipped but an invalid
4949 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004950 */
4951#if defined(MBEDTLS_SSL_PROTO_DTLS)
4952 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4953 {
4954 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4955
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004956 /* Check epoch (and sequence number) with DTLS */
4957 if( rec_epoch != ssl->in_epoch )
4958 {
4959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4960 "expected %d, received %d",
4961 ssl->in_epoch, rec_epoch ) );
4962
4963#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4964 /*
4965 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4966 * access the first byte of record content (handshake type), as we
4967 * have an active transform (possibly iv_len != 0), so use the
4968 * fact that the record header len is 13 instead.
4969 */
4970 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4971 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4972 rec_epoch == 0 &&
4973 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4974 ssl->in_left > 13 &&
4975 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4976 {
4977 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4978 "from the same port" ) );
4979 return( ssl_handle_possible_reconnect( ssl ) );
4980 }
4981 else
4982#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004983 {
4984 /* Consider buffering the record. */
4985 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4986 {
4987 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4988 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4989 }
4990
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004991 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004992 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004993 }
4994
4995#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4996 /* Replay detection only works for the current epoch */
4997 if( rec_epoch == ssl->in_epoch &&
4998 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4999 {
5000 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
5001 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
5002 }
5003#endif
5004 }
5005#endif /* MBEDTLS_SSL_PROTO_DTLS */
5006
Hanno Becker52c6dc62017-05-26 16:07:36 +01005007
5008 /* Check length against bounds of the current transform and version */
5009 if( ssl->transform_in == NULL )
5010 {
5011 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10005012 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005013 {
5014 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5015 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5016 }
5017 }
5018 else
5019 {
5020 if( ssl->in_msglen < ssl->transform_in->minlen )
5021 {
5022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5023 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5024 }
5025
5026#if defined(MBEDTLS_SSL_PROTO_SSL3)
5027 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10005028 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005029 {
5030 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5031 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5032 }
5033#endif
5034#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
5035 defined(MBEDTLS_SSL_PROTO_TLS1_2)
5036 /*
5037 * TLS encrypted messages can have up to 256 bytes of padding
5038 */
5039 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
5040 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10005041 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01005042 {
5043 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5044 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5045 }
5046#endif
5047 }
5048
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005049 return( 0 );
5050}
Paul Bakker5121ce52009-01-03 21:22:43 +00005051
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005052/*
5053 * If applicable, decrypt (and decompress) record content
5054 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005055static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005056{
5057 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005059 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
Hanno Becker5903de42019-05-03 14:46:38 +01005060 ssl->in_hdr, mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005062#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5063 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005065 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005067 ret = mbedtls_ssl_hw_record_read( ssl );
5068 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005069 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005070 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5071 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005072 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005073
5074 if( ret == 0 )
5075 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005076 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005077#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005078 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005079 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005080 mbedtls_record rec;
5081
5082 rec.buf = ssl->in_iv;
5083 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5084 - ( ssl->in_iv - ssl->in_buf );
5085 rec.data_len = ssl->in_msglen;
5086 rec.data_offset = 0;
Hanno Beckera0e20d02019-05-15 14:03:01 +01005087#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID )
Hanno Becker2cdc5c32019-05-09 15:54:28 +01005088 rec.cid_len = (uint8_t)( ssl->in_len - ssl->in_cid );
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01005089 memcpy( rec.cid, ssl->in_cid, rec.cid_len );
Hanno Beckera0e20d02019-05-15 14:03:01 +01005090#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005091
5092 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5093 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5094 ssl->conf->transport, rec.ver );
5095 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005096 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5097 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005098 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005099 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Hanno Becker8367ccc2019-05-14 11:30:10 +01005100
Hanno Beckera0e20d02019-05-15 14:03:01 +01005101#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker8367ccc2019-05-14 11:30:10 +01005102 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_CID &&
5103 ssl->conf->ignore_unexpected_cid
5104 == MBEDTLS_SSL_UNEXPECTED_CID_IGNORE )
5105 {
Hanno Beckere8d6afd2019-05-24 10:11:06 +01005106 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ignoring unexpected CID" ) );
Hanno Becker16ded982019-05-08 13:02:55 +01005107 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
Hanno Becker8367ccc2019-05-14 11:30:10 +01005108 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005109#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker16ded982019-05-08 13:02:55 +01005110
Paul Bakker5121ce52009-01-03 21:22:43 +00005111 return( ret );
5112 }
5113
Hanno Becker6430faf2019-05-08 11:57:13 +01005114 if( ssl->in_msgtype != rec.type )
5115 {
5116 MBEDTLS_SSL_DEBUG_MSG( 4, ( "record type after decrypt (before %d): %d",
5117 ssl->in_msgtype, rec.type ) );
5118 }
5119
5120 /* The record content type may change during decryption,
5121 * so re-read it. */
5122 ssl->in_msgtype = rec.type;
5123 /* Also update the input buffer, because unfortunately
5124 * the server-side ssl_parse_client_hello() reparses the
5125 * record header when receiving a ClientHello initiating
5126 * a renegotiation. */
5127 ssl->in_hdr[0] = rec.type;
Hanno Becker79594fd2019-05-08 09:38:41 +01005128 ssl->in_msg = rec.buf + rec.data_offset;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005129 ssl->in_msglen = rec.data_len;
5130 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5131 ssl->in_len[1] = (unsigned char)( rec.data_len );
5132
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005133 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5134 ssl->in_msg, ssl->in_msglen );
5135
Hanno Beckera0e20d02019-05-15 14:03:01 +01005136#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6430faf2019-05-08 11:57:13 +01005137 /* We have already checked the record content type
5138 * in ssl_parse_record_header(), failing or silently
5139 * dropping the record in the case of an unknown type.
5140 *
5141 * Since with the use of CIDs, the record content type
5142 * might change during decryption, re-check the record
5143 * content type, but treat a failure as fatal this time. */
5144 if( ssl_check_record_type( ssl->in_msgtype ) )
5145 {
5146 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
5147 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5148 }
Hanno Beckera0e20d02019-05-15 14:03:01 +01005149#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6430faf2019-05-08 11:57:13 +01005150
Angus Grattond8213d02016-05-25 20:56:48 +10005151 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005152 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005153 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5154 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005155 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005156 else if( ssl->in_msglen == 0 )
5157 {
5158#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5159 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5160 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5161 {
5162 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5163 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5164 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5165 }
5166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5167
5168 ssl->nb_zero++;
5169
5170 /*
5171 * Three or more empty messages may be a DoS attack
5172 * (excessive CPU consumption).
5173 */
5174 if( ssl->nb_zero > 3 )
5175 {
5176 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
Hanno Becker6e7700d2019-05-08 10:38:32 +01005177 "messages, possible DoS attack" ) );
5178 /* Treat the records as if they were not properly authenticated,
5179 * thereby failing the connection if we see more than allowed
5180 * by the configured bad MAC threshold. */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005181 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5182 }
5183 }
5184 else
5185 ssl->nb_zero = 0;
5186
5187#if defined(MBEDTLS_SSL_PROTO_DTLS)
5188 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5189 {
5190 ; /* in_ctr read from peer, not maintained internally */
5191 }
5192 else
5193#endif
5194 {
5195 unsigned i;
5196 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5197 if( ++ssl->in_ctr[i - 1] != 0 )
5198 break;
5199
5200 /* The loop goes to its end iff the counter is wrapping */
5201 if( i == ssl_ep_len( ssl ) )
5202 {
5203 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5204 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5205 }
5206 }
5207
Paul Bakker5121ce52009-01-03 21:22:43 +00005208 }
5209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005210#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005211 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005212 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005213 {
5214 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5215 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005216 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005217 return( ret );
5218 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005220#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005222#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005223 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005225 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005226 }
5227#endif
5228
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005229 return( 0 );
5230}
5231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005232static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005233
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005234/*
5235 * Read a record.
5236 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005237 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5238 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5239 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005240 */
Hanno Becker1097b342018-08-15 14:09:41 +01005241
5242/* Helper functions for mbedtls_ssl_read_record(). */
5243static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005244static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5245static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005246
Hanno Becker327c93b2018-08-15 13:56:18 +01005247int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005248 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005249{
5250 int ret;
5251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005252 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005253
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005254 if( ssl->keep_current_message == 0 )
5255 {
5256 do {
Simon Butcher99000142016-10-13 17:21:01 +01005257
Hanno Becker26994592018-08-15 14:14:59 +01005258 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005259 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005260 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005261
Hanno Beckere74d5562018-08-15 14:26:08 +01005262 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005263 {
Hanno Becker40f50842018-08-15 14:48:01 +01005264#if defined(MBEDTLS_SSL_PROTO_DTLS)
5265 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005266
Hanno Becker40f50842018-08-15 14:48:01 +01005267 /* We only check for buffered messages if the
5268 * current datagram is fully consumed. */
5269 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005270 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005271 {
Hanno Becker40f50842018-08-15 14:48:01 +01005272 if( ssl_load_buffered_message( ssl ) == 0 )
5273 have_buffered = 1;
5274 }
5275
5276 if( have_buffered == 0 )
5277#endif /* MBEDTLS_SSL_PROTO_DTLS */
5278 {
5279 ret = ssl_get_next_record( ssl );
5280 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5281 continue;
5282
5283 if( ret != 0 )
5284 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005285 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005286 return( ret );
5287 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005288 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005289 }
5290
5291 ret = mbedtls_ssl_handle_message_type( ssl );
5292
Hanno Becker40f50842018-08-15 14:48:01 +01005293#if defined(MBEDTLS_SSL_PROTO_DTLS)
5294 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5295 {
5296 /* Buffer future message */
5297 ret = ssl_buffer_message( ssl );
5298 if( ret != 0 )
5299 return( ret );
5300
5301 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5302 }
5303#endif /* MBEDTLS_SSL_PROTO_DTLS */
5304
Hanno Becker90333da2017-10-10 11:27:13 +01005305 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5306 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005307
5308 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005309 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005310 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005311 return( ret );
5312 }
5313
Hanno Becker327c93b2018-08-15 13:56:18 +01005314 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005315 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005316 {
5317 mbedtls_ssl_update_handshake_status( ssl );
5318 }
Simon Butcher99000142016-10-13 17:21:01 +01005319 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005320 else
Simon Butcher99000142016-10-13 17:21:01 +01005321 {
Hanno Becker02f59072018-08-15 14:00:24 +01005322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005323 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005324 }
5325
5326 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5327
5328 return( 0 );
5329}
5330
Hanno Becker40f50842018-08-15 14:48:01 +01005331#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005332static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005333{
Hanno Becker40f50842018-08-15 14:48:01 +01005334 if( ssl->in_left > ssl->next_record_offset )
5335 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005336
Hanno Becker40f50842018-08-15 14:48:01 +01005337 return( 0 );
5338}
5339
5340static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5341{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005342 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005343 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005344 int ret = 0;
5345
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005346 if( hs == NULL )
5347 return( -1 );
5348
Hanno Beckere00ae372018-08-20 09:39:42 +01005349 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5350
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005351 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5352 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5353 {
5354 /* Check if we have seen a ChangeCipherSpec before.
5355 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005356 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005357 {
5358 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5359 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005360 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005361 }
5362
Hanno Becker39b8bc92018-08-28 17:17:13 +01005363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005364 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5365 ssl->in_msglen = 1;
5366 ssl->in_msg[0] = 1;
5367
5368 /* As long as they are equal, the exact value doesn't matter. */
5369 ssl->in_left = 0;
5370 ssl->next_record_offset = 0;
5371
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005372 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005373 goto exit;
5374 }
Hanno Becker37f95322018-08-16 13:55:32 +01005375
Hanno Beckerb8f50142018-08-28 10:01:34 +01005376#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005377 /* Debug only */
5378 {
5379 unsigned offset;
5380 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5381 {
5382 hs_buf = &hs->buffering.hs[offset];
5383 if( hs_buf->is_valid == 1 )
5384 {
5385 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5386 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005387 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005388 }
5389 }
5390 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005391#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005392
5393 /* Check if we have buffered and/or fully reassembled the
5394 * next handshake message. */
5395 hs_buf = &hs->buffering.hs[0];
5396 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5397 {
5398 /* Synthesize a record containing the buffered HS message. */
5399 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5400 ( hs_buf->data[2] << 8 ) |
5401 hs_buf->data[3];
5402
5403 /* Double-check that we haven't accidentally buffered
5404 * a message that doesn't fit into the input buffer. */
5405 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5406 {
5407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5409 }
5410
5411 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5412 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5413 hs_buf->data, msg_len + 12 );
5414
5415 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5416 ssl->in_hslen = msg_len + 12;
5417 ssl->in_msglen = msg_len + 12;
5418 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5419
5420 ret = 0;
5421 goto exit;
5422 }
5423 else
5424 {
5425 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5426 hs->in_msg_seq ) );
5427 }
5428
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005429 ret = -1;
5430
5431exit:
5432
5433 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5434 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005435}
5436
Hanno Beckera02b0b42018-08-21 17:20:27 +01005437static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5438 size_t desired )
5439{
5440 int offset;
5441 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005442 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5443 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005444
Hanno Becker01315ea2018-08-21 17:22:17 +01005445 /* Get rid of future records epoch first, if such exist. */
5446 ssl_free_buffered_record( ssl );
5447
5448 /* Check if we have enough space available now. */
5449 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5450 hs->buffering.total_bytes_buffered ) )
5451 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005453 return( 0 );
5454 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005455
Hanno Becker4f432ad2018-08-28 10:02:32 +01005456 /* We don't have enough space to buffer the next expected handshake
5457 * message. Remove buffers used for future messages to gain space,
5458 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005459 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5460 offset >= 0; offset-- )
5461 {
5462 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5463 offset ) );
5464
Hanno Beckerb309b922018-08-23 13:18:05 +01005465 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005466
5467 /* Check if we have enough space available now. */
5468 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5469 hs->buffering.total_bytes_buffered ) )
5470 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005471 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005472 return( 0 );
5473 }
5474 }
5475
5476 return( -1 );
5477}
5478
Hanno Becker40f50842018-08-15 14:48:01 +01005479static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5480{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005481 int ret = 0;
5482 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5483
5484 if( hs == NULL )
5485 return( 0 );
5486
5487 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5488
5489 switch( ssl->in_msgtype )
5490 {
5491 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5492 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005493
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005494 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005495 break;
5496
5497 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005498 {
5499 unsigned recv_msg_seq_offset;
5500 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5501 mbedtls_ssl_hs_buffer *hs_buf;
5502 size_t msg_len = ssl->in_hslen - 12;
5503
5504 /* We should never receive an old handshake
5505 * message - double-check nonetheless. */
5506 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5507 {
5508 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5509 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5510 }
5511
5512 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5513 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5514 {
5515 /* Silently ignore -- message too far in the future */
5516 MBEDTLS_SSL_DEBUG_MSG( 2,
5517 ( "Ignore future HS message with sequence number %u, "
5518 "buffering window %u - %u",
5519 recv_msg_seq, ssl->handshake->in_msg_seq,
5520 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5521
5522 goto exit;
5523 }
5524
5525 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5526 recv_msg_seq, recv_msg_seq_offset ) );
5527
5528 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5529
5530 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005531 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005532 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005533 size_t reassembly_buf_sz;
5534
Hanno Becker37f95322018-08-16 13:55:32 +01005535 hs_buf->is_fragmented =
5536 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5537
5538 /* We copy the message back into the input buffer
5539 * after reassembly, so check that it's not too large.
5540 * This is an implementation-specific limitation
5541 * and not one from the standard, hence it is not
5542 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005543 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005544 {
5545 /* Ignore message */
5546 goto exit;
5547 }
5548
Hanno Beckere0b150f2018-08-21 15:51:03 +01005549 /* Check if we have enough space to buffer the message. */
5550 if( hs->buffering.total_bytes_buffered >
5551 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5552 {
5553 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5554 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5555 }
5556
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005557 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5558 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005559
5560 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5561 hs->buffering.total_bytes_buffered ) )
5562 {
5563 if( recv_msg_seq_offset > 0 )
5564 {
5565 /* If we can't buffer a future message because
5566 * of space limitations -- ignore. */
5567 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",
5568 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5569 (unsigned) hs->buffering.total_bytes_buffered ) );
5570 goto exit;
5571 }
Hanno Beckere1801392018-08-21 16:51:05 +01005572 else
5573 {
5574 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",
5575 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5576 (unsigned) hs->buffering.total_bytes_buffered ) );
5577 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005578
Hanno Beckera02b0b42018-08-21 17:20:27 +01005579 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005580 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005581 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",
5582 (unsigned) msg_len,
5583 (unsigned) reassembly_buf_sz,
5584 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005585 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005586 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5587 goto exit;
5588 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005589 }
5590
5591 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5592 msg_len ) );
5593
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005594 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5595 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005596 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005597 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005598 goto exit;
5599 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005600 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005601
5602 /* Prepare final header: copy msg_type, length and message_seq,
5603 * then add standardised fragment_offset and fragment_length */
5604 memcpy( hs_buf->data, ssl->in_msg, 6 );
5605 memset( hs_buf->data + 6, 0, 3 );
5606 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5607
5608 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005609
5610 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005611 }
5612 else
5613 {
5614 /* Make sure msg_type and length are consistent */
5615 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5616 {
5617 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5618 /* Ignore */
5619 goto exit;
5620 }
5621 }
5622
Hanno Becker4422bbb2018-08-20 09:40:19 +01005623 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005624 {
5625 size_t frag_len, frag_off;
5626 unsigned char * const msg = hs_buf->data + 12;
5627
5628 /*
5629 * Check and copy current fragment
5630 */
5631
5632 /* Validation of header fields already done in
5633 * mbedtls_ssl_prepare_handshake_record(). */
5634 frag_off = ssl_get_hs_frag_off( ssl );
5635 frag_len = ssl_get_hs_frag_len( ssl );
5636
5637 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5638 frag_off, frag_len ) );
5639 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5640
5641 if( hs_buf->is_fragmented )
5642 {
5643 unsigned char * const bitmask = msg + msg_len;
5644 ssl_bitmask_set( bitmask, frag_off, frag_len );
5645 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5646 msg_len ) == 0 );
5647 }
5648 else
5649 {
5650 hs_buf->is_complete = 1;
5651 }
5652
5653 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5654 hs_buf->is_complete ? "" : "not yet " ) );
5655 }
5656
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005657 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005658 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005659
5660 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005661 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005662 break;
5663 }
5664
5665exit:
5666
5667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5668 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005669}
5670#endif /* MBEDTLS_SSL_PROTO_DTLS */
5671
Hanno Becker1097b342018-08-15 14:09:41 +01005672static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005673{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005674 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005675 * Consume last content-layer message and potentially
5676 * update in_msglen which keeps track of the contents'
5677 * consumption state.
5678 *
5679 * (1) Handshake messages:
5680 * Remove last handshake message, move content
5681 * and adapt in_msglen.
5682 *
5683 * (2) Alert messages:
5684 * Consume whole record content, in_msglen = 0.
5685 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005686 * (3) Change cipher spec:
5687 * Consume whole record content, in_msglen = 0.
5688 *
5689 * (4) Application data:
5690 * Don't do anything - the record layer provides
5691 * the application data as a stream transport
5692 * and consumes through mbedtls_ssl_read only.
5693 *
5694 */
5695
5696 /* Case (1): Handshake messages */
5697 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005698 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005699 /* Hard assertion to be sure that no application data
5700 * is in flight, as corrupting ssl->in_msglen during
5701 * ssl->in_offt != NULL is fatal. */
5702 if( ssl->in_offt != NULL )
5703 {
5704 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5705 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5706 }
5707
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005708 /*
5709 * Get next Handshake message in the current record
5710 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005711
Hanno Becker4a810fb2017-05-24 16:27:30 +01005712 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005713 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005714 * current handshake content: If DTLS handshake
5715 * fragmentation is used, that's the fragment
5716 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005717 * size here is faulty and should be changed at
5718 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005719 * (2) While it doesn't seem to cause problems, one
5720 * has to be very careful not to assume that in_hslen
5721 * is always <= in_msglen in a sensible communication.
5722 * Again, it's wrong for DTLS handshake fragmentation.
5723 * The following check is therefore mandatory, and
5724 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005725 * Additionally, ssl->in_hslen might be arbitrarily out of
5726 * bounds after handling a DTLS message with an unexpected
5727 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005728 */
5729 if( ssl->in_hslen < ssl->in_msglen )
5730 {
5731 ssl->in_msglen -= ssl->in_hslen;
5732 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5733 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005734
Hanno Becker4a810fb2017-05-24 16:27:30 +01005735 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5736 ssl->in_msg, ssl->in_msglen );
5737 }
5738 else
5739 {
5740 ssl->in_msglen = 0;
5741 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005742
Hanno Becker4a810fb2017-05-24 16:27:30 +01005743 ssl->in_hslen = 0;
5744 }
5745 /* Case (4): Application data */
5746 else if( ssl->in_offt != NULL )
5747 {
5748 return( 0 );
5749 }
5750 /* Everything else (CCS & Alerts) */
5751 else
5752 {
5753 ssl->in_msglen = 0;
5754 }
5755
Hanno Becker1097b342018-08-15 14:09:41 +01005756 return( 0 );
5757}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005758
Hanno Beckere74d5562018-08-15 14:26:08 +01005759static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5760{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005761 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005762 return( 1 );
5763
5764 return( 0 );
5765}
5766
Hanno Becker5f066e72018-08-16 14:56:31 +01005767#if defined(MBEDTLS_SSL_PROTO_DTLS)
5768
5769static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5770{
5771 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5772 if( hs == NULL )
5773 return;
5774
Hanno Becker01315ea2018-08-21 17:22:17 +01005775 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005776 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005777 hs->buffering.total_bytes_buffered -=
5778 hs->buffering.future_record.len;
5779
5780 mbedtls_free( hs->buffering.future_record.data );
5781 hs->buffering.future_record.data = NULL;
5782 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005783}
5784
5785static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5786{
5787 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5788 unsigned char * rec;
5789 size_t rec_len;
5790 unsigned rec_epoch;
5791
5792 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5793 return( 0 );
5794
5795 if( hs == NULL )
5796 return( 0 );
5797
Hanno Becker5f066e72018-08-16 14:56:31 +01005798 rec = hs->buffering.future_record.data;
5799 rec_len = hs->buffering.future_record.len;
5800 rec_epoch = hs->buffering.future_record.epoch;
5801
5802 if( rec == NULL )
5803 return( 0 );
5804
Hanno Becker4cb782d2018-08-20 11:19:05 +01005805 /* Only consider loading future records if the
5806 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005807 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005808 return( 0 );
5809
Hanno Becker5f066e72018-08-16 14:56:31 +01005810 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5811
5812 if( rec_epoch != ssl->in_epoch )
5813 {
5814 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5815 goto exit;
5816 }
5817
5818 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5819
5820 /* Double-check that the record is not too large */
5821 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5822 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5823 {
5824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5825 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5826 }
5827
5828 memcpy( ssl->in_hdr, rec, rec_len );
5829 ssl->in_left = rec_len;
5830 ssl->next_record_offset = 0;
5831
5832 ssl_free_buffered_record( ssl );
5833
5834exit:
5835 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5836 return( 0 );
5837}
5838
5839static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5840{
5841 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5842 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005843 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005844
5845 /* Don't buffer future records outside handshakes. */
5846 if( hs == NULL )
5847 return( 0 );
5848
5849 /* Only buffer handshake records (we are only interested
5850 * in Finished messages). */
5851 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5852 return( 0 );
5853
5854 /* Don't buffer more than one future epoch record. */
5855 if( hs->buffering.future_record.data != NULL )
5856 return( 0 );
5857
Hanno Becker01315ea2018-08-21 17:22:17 +01005858 /* Don't buffer record if there's not enough buffering space remaining. */
5859 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5860 hs->buffering.total_bytes_buffered ) )
5861 {
5862 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",
5863 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5864 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005865 return( 0 );
5866 }
5867
Hanno Becker5f066e72018-08-16 14:56:31 +01005868 /* Buffer record */
5869 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5870 ssl->in_epoch + 1 ) );
5871 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5872 rec_hdr_len + ssl->in_msglen );
5873
5874 /* ssl_parse_record_header() only considers records
5875 * of the next epoch as candidates for buffering. */
5876 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005877 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005878
5879 hs->buffering.future_record.data =
5880 mbedtls_calloc( 1, hs->buffering.future_record.len );
5881 if( hs->buffering.future_record.data == NULL )
5882 {
5883 /* If we run out of RAM trying to buffer a
5884 * record from the next epoch, just ignore. */
5885 return( 0 );
5886 }
5887
Hanno Becker01315ea2018-08-21 17:22:17 +01005888 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005889
Hanno Becker01315ea2018-08-21 17:22:17 +01005890 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005891 return( 0 );
5892}
5893
5894#endif /* MBEDTLS_SSL_PROTO_DTLS */
5895
Hanno Beckere74d5562018-08-15 14:26:08 +01005896static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005897{
5898 int ret;
5899
Hanno Becker5f066e72018-08-16 14:56:31 +01005900#if defined(MBEDTLS_SSL_PROTO_DTLS)
5901 /* We might have buffered a future record; if so,
5902 * and if the epoch matches now, load it.
5903 * On success, this call will set ssl->in_left to
5904 * the length of the buffered record, so that
5905 * the calls to ssl_fetch_input() below will
5906 * essentially be no-ops. */
5907 ret = ssl_load_buffered_record( ssl );
5908 if( ret != 0 )
5909 return( ret );
5910#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005911
Hanno Beckerca59c2b2019-05-08 12:03:28 +01005912 /* Reset in pointers to default state for TLS/DTLS records,
5913 * assuming no CID and no offset between record content and
5914 * record plaintext. */
5915 ssl_update_in_pointers( ssl );
5916
5917 /* Ensure that we have enough space available for the default form
5918 * of TLS / DTLS record headers (5 Bytes for TLS, 13 Bytes for DTLS,
5919 * with no space for CIDs counted in). */
5920 ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_in_hdr_len( ssl ) );
5921 if( ret != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005922 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005923 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005924 return( ret );
5925 }
5926
5927 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005928 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005929#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005930 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5931 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005932 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005933 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5934 {
5935 ret = ssl_buffer_future_record( ssl );
5936 if( ret != 0 )
5937 return( ret );
5938
5939 /* Fall through to handling of unexpected records */
5940 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5941 }
5942
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005943 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5944 {
5945 /* Skip unexpected record (but not whole datagram) */
5946 ssl->next_record_offset = ssl->in_msglen
Hanno Becker5903de42019-05-03 14:46:38 +01005947 + mbedtls_ssl_in_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005948
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005949 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5950 "(header)" ) );
5951 }
5952 else
5953 {
5954 /* Skip invalid record and the rest of the datagram */
5955 ssl->next_record_offset = 0;
5956 ssl->in_left = 0;
5957
5958 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5959 "(header)" ) );
5960 }
5961
5962 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005963 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005964 }
5965#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005966 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005967 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005968
5969 /*
5970 * Read and optionally decrypt the message contents
5971 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005972 if( ( ret = mbedtls_ssl_fetch_input( ssl,
Hanno Becker5903de42019-05-03 14:46:38 +01005973 mbedtls_ssl_in_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005974 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005975 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005976 return( ret );
5977 }
5978
5979 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005980#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005981 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005982 {
Hanno Becker5903de42019-05-03 14:46:38 +01005983 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_in_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005984 if( ssl->next_record_offset < ssl->in_left )
5985 {
5986 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5987 }
5988 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005989 else
5990#endif
5991 ssl->in_left = 0;
5992
5993 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005994 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005995#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005996 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005997 {
5998 /* Silently discard invalid records */
Hanno Becker82e2a392019-05-03 16:36:59 +01005999 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006000 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02006001 /* Except when waiting for Finished as a bad mac here
6002 * probably means something went wrong in the handshake
6003 * (eg wrong psk used, mitm downgrade attempt, etc.) */
6004 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
6005 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
6006 {
6007#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6008 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
6009 {
6010 mbedtls_ssl_send_alert_message( ssl,
6011 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6012 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
6013 }
6014#endif
6015 return( ret );
6016 }
6017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006018#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006019 if( ssl->conf->badmac_limit != 0 &&
6020 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006022 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
6023 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02006024 }
6025#endif
6026
Hanno Becker4a810fb2017-05-24 16:27:30 +01006027 /* As above, invalid records cause
6028 * dismissal of the whole datagram. */
6029
6030 ssl->next_record_offset = 0;
6031 ssl->in_left = 0;
6032
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006033 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01006034 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006035 }
6036
6037 return( ret );
6038 }
6039 else
6040#endif
6041 {
6042 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006043#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
6044 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006045 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006046 mbedtls_ssl_send_alert_message( ssl,
6047 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6048 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02006049 }
6050#endif
6051 return( ret );
6052 }
6053 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006054
Simon Butcher99000142016-10-13 17:21:01 +01006055 return( 0 );
6056}
6057
6058int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
6059{
6060 int ret;
6061
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02006062 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02006063 * Handle particular types of records
6064 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006065 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006066 {
Simon Butcher99000142016-10-13 17:21:01 +01006067 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
6068 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01006069 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01006070 }
Paul Bakker5121ce52009-01-03 21:22:43 +00006071 }
6072
Hanno Beckere678eaa2018-08-21 14:57:46 +01006073 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006074 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006075 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006076 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01006077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
6078 ssl->in_msglen ) );
6079 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006080 }
6081
Hanno Beckere678eaa2018-08-21 14:57:46 +01006082 if( ssl->in_msg[0] != 1 )
6083 {
6084 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
6085 ssl->in_msg[0] ) );
6086 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6087 }
6088
6089#if defined(MBEDTLS_SSL_PROTO_DTLS)
6090 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6091 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
6092 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
6093 {
6094 if( ssl->handshake == NULL )
6095 {
6096 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6097 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6098 }
6099
6100 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6101 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6102 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006103#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006104 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006106 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006107 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006108 if( ssl->in_msglen != 2 )
6109 {
6110 /* Note: Standard allows for more than one 2 byte alert
6111 to be packed in a single message, but Mbed TLS doesn't
6112 currently support this. */
6113 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6114 ssl->in_msglen ) );
6115 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6116 }
6117
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006118 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006119 ssl->in_msg[0], ssl->in_msg[1] ) );
6120
6121 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006122 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006124 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006126 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006127 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006128 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006129 }
6130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006131 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6132 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006134 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6135 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006136 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006137
6138#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6139 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6140 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6141 {
Hanno Becker90333da2017-10-10 11:27:13 +01006142 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006143 /* Will be handled when trying to parse ServerHello */
6144 return( 0 );
6145 }
6146#endif
6147
6148#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6149 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6150 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6151 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6152 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6153 {
6154 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6155 /* Will be handled in mbedtls_ssl_parse_certificate() */
6156 return( 0 );
6157 }
6158#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6159
6160 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006161 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006162 }
6163
Hanno Beckerc76c6192017-06-06 10:03:17 +01006164#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker37ae9522019-05-03 16:54:26 +01006165 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006166 {
Hanno Becker37ae9522019-05-03 16:54:26 +01006167 /* Drop unexpected ApplicationData records,
6168 * except at the beginning of renegotiations */
6169 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
6170 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
6171#if defined(MBEDTLS_SSL_RENEGOTIATION)
6172 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
6173 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
Hanno Beckerc76c6192017-06-06 10:03:17 +01006174#endif
Hanno Becker37ae9522019-05-03 16:54:26 +01006175 )
6176 {
6177 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
6178 return( MBEDTLS_ERR_SSL_NON_FATAL );
6179 }
6180
6181 if( ssl->handshake != NULL &&
6182 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6183 {
6184 ssl_handshake_wrapup_free_hs_transform( ssl );
6185 }
6186 }
Hanno Becker4a4af9f2019-05-08 16:26:21 +01006187#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Beckerc76c6192017-06-06 10:03:17 +01006188
Paul Bakker5121ce52009-01-03 21:22:43 +00006189 return( 0 );
6190}
6191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006192int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006193{
6194 int ret;
6195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006196 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6197 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6198 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006199 {
6200 return( ret );
6201 }
6202
6203 return( 0 );
6204}
6205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006206int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006207 unsigned char level,
6208 unsigned char message )
6209{
6210 int ret;
6211
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006212 if( ssl == NULL || ssl->conf == NULL )
6213 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6214
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006215 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006216 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006218 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006219 ssl->out_msglen = 2;
6220 ssl->out_msg[0] = level;
6221 ssl->out_msg[1] = message;
6222
Hanno Becker67bc7c32018-08-06 11:33:50 +01006223 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006224 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006225 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006226 return( ret );
6227 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006228 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006229
6230 return( 0 );
6231}
6232
Hanno Beckerb9d44792019-02-08 07:19:04 +00006233#if defined(MBEDTLS_X509_CRT_PARSE_C)
6234static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6235{
6236#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6237 if( session->peer_cert != NULL )
6238 {
6239 mbedtls_x509_crt_free( session->peer_cert );
6240 mbedtls_free( session->peer_cert );
6241 session->peer_cert = NULL;
6242 }
6243#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6244 if( session->peer_cert_digest != NULL )
6245 {
6246 /* Zeroization is not necessary. */
6247 mbedtls_free( session->peer_cert_digest );
6248 session->peer_cert_digest = NULL;
6249 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6250 session->peer_cert_digest_len = 0;
6251 }
6252#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6253}
6254#endif /* MBEDTLS_X509_CRT_PARSE_C */
6255
Paul Bakker5121ce52009-01-03 21:22:43 +00006256/*
6257 * Handshake functions
6258 */
Hanno Becker21489932019-02-05 13:20:55 +00006259#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006260/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006261int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006263 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6264 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006267
Hanno Becker7177a882019-02-05 13:36:46 +00006268 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006269 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006270 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006271 ssl->state++;
6272 return( 0 );
6273 }
6274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006275 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6276 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006277}
6278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006279int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006280{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006281 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6282 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006285
Hanno Becker7177a882019-02-05 13:36:46 +00006286 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006287 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006288 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006289 ssl->state++;
6290 return( 0 );
6291 }
6292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006293 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6294 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006295}
Gilles Peskinef9828522017-05-03 12:28:43 +02006296
Hanno Becker21489932019-02-05 13:20:55 +00006297#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006298/* Some certificate support -> implement write and parse */
6299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006300int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006301{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006302 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006303 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006304 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006305 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6306 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006308 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006309
Hanno Becker7177a882019-02-05 13:36:46 +00006310 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006311 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006312 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006313 ssl->state++;
6314 return( 0 );
6315 }
6316
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006317#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006318 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006319 {
6320 if( ssl->client_auth == 0 )
6321 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006323 ssl->state++;
6324 return( 0 );
6325 }
6326
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006327#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006328 /*
6329 * If using SSLv3 and got no cert, send an Alert message
6330 * (otherwise an empty Certificate message will be sent).
6331 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006332 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6333 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006334 {
6335 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006336 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6337 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6338 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006341 goto write_msg;
6342 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006343#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006344 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006345#endif /* MBEDTLS_SSL_CLI_C */
6346#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006347 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006348 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006349 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006350 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006351 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6352 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006353 }
6354 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006355#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006357 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006358
6359 /*
6360 * 0 . 0 handshake type
6361 * 1 . 3 handshake length
6362 * 4 . 6 length of all certs
6363 * 7 . 9 length of cert. 1
6364 * 10 . n-1 peer certificate
6365 * n . n+2 length of cert. 2
6366 * n+3 . ... upper level cert, etc.
6367 */
6368 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006369 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006370
Paul Bakker29087132010-03-21 21:03:34 +00006371 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006372 {
6373 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006374 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006375 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006376 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006377 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006378 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006379 }
6380
6381 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6382 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6383 ssl->out_msg[i + 2] = (unsigned char)( n );
6384
6385 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6386 i += n; crt = crt->next;
6387 }
6388
6389 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6390 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6391 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6392
6393 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006394 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6395 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006396
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006397#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006398write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006399#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006400
6401 ssl->state++;
6402
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006403 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006404 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006405 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006406 return( ret );
6407 }
6408
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006409 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006410
Paul Bakkered27a042013-04-18 22:46:23 +02006411 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006412}
6413
Hanno Becker84879e32019-01-31 07:44:03 +00006414#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006415
6416#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006417static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6418 unsigned char *crt_buf,
6419 size_t crt_buf_len )
6420{
6421 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6422
6423 if( peer_crt == NULL )
6424 return( -1 );
6425
6426 if( peer_crt->raw.len != crt_buf_len )
6427 return( -1 );
6428
Hanno Becker46f34d02019-02-08 14:00:04 +00006429 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006430}
Hanno Becker177475a2019-02-05 17:02:46 +00006431#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6432static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6433 unsigned char *crt_buf,
6434 size_t crt_buf_len )
6435{
6436 int ret;
6437 unsigned char const * const peer_cert_digest =
6438 ssl->session->peer_cert_digest;
6439 mbedtls_md_type_t const peer_cert_digest_type =
6440 ssl->session->peer_cert_digest_type;
6441 mbedtls_md_info_t const * const digest_info =
6442 mbedtls_md_info_from_type( peer_cert_digest_type );
6443 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6444 size_t digest_len;
6445
6446 if( peer_cert_digest == NULL || digest_info == NULL )
6447 return( -1 );
6448
6449 digest_len = mbedtls_md_get_size( digest_info );
6450 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6451 return( -1 );
6452
6453 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6454 if( ret != 0 )
6455 return( -1 );
6456
6457 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6458}
6459#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006460#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006461
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006462/*
6463 * Once the certificate message is read, parse it into a cert chain and
6464 * perform basic checks, but leave actual verification to the caller
6465 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006466static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6467 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006468{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006469 int ret;
6470#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6471 int crt_cnt=0;
6472#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006473 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006474 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006475
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006476 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006477 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006478 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006479 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6480 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006481 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006482 }
6483
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006484 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6485 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006486 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006487 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006488 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6489 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006490 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006491 }
6492
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006493 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006494
Paul Bakker5121ce52009-01-03 21:22:43 +00006495 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006496 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006497 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006498 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006499
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006500 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006501 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006502 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006503 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006504 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6505 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006506 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006507 }
6508
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006509 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6510 i += 3;
6511
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006512 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006513 while( i < ssl->in_hslen )
6514 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006515 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006516 if ( i + 3 > ssl->in_hslen ) {
6517 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006518 mbedtls_ssl_send_alert_message( ssl,
6519 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6520 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006521 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6522 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006523 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6524 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006525 if( ssl->in_msg[i] != 0 )
6526 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006527 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006528 mbedtls_ssl_send_alert_message( ssl,
6529 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6530 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006531 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006532 }
6533
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006534 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006535 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6536 | (unsigned int) ssl->in_msg[i + 2];
6537 i += 3;
6538
6539 if( n < 128 || i + n > ssl->in_hslen )
6540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006541 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006542 mbedtls_ssl_send_alert_message( ssl,
6543 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6544 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006545 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006546 }
6547
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006548 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006549#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6550 if( crt_cnt++ == 0 &&
6551 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6552 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006553 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006554 /* During client-side renegotiation, check that the server's
6555 * end-CRTs hasn't changed compared to the initial handshake,
6556 * mitigating the triple handshake attack. On success, reuse
6557 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6559 if( ssl_check_peer_crt_unchanged( ssl,
6560 &ssl->in_msg[i],
6561 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006562 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006563 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6564 mbedtls_ssl_send_alert_message( ssl,
6565 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6566 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6567 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006568 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006569
6570 /* Now we can safely free the original chain. */
6571 ssl_clear_peer_cert( ssl->session );
6572 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006573#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6574
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006575 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006576#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006577 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006578#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006579 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006580 * it in-place from the input buffer instead of making a copy. */
6581 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6582#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006583 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006584 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006585 case 0: /*ok*/
6586 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6587 /* Ignore certificate with an unknown algorithm: maybe a
6588 prior certificate was already trusted. */
6589 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006590
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006591 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6592 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6593 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006594
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006595 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6596 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6597 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006598
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006599 default:
6600 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6601 crt_parse_der_failed:
6602 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6603 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6604 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006605 }
6606
6607 i += n;
6608 }
6609
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006610 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006611 return( 0 );
6612}
6613
Hanno Becker4a55f632019-02-05 12:49:06 +00006614#if defined(MBEDTLS_SSL_SRV_C)
6615static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6616{
6617 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6618 return( -1 );
6619
6620#if defined(MBEDTLS_SSL_PROTO_SSL3)
6621 /*
6622 * Check if the client sent an empty certificate
6623 */
6624 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6625 {
6626 if( ssl->in_msglen == 2 &&
6627 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6628 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6629 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6630 {
6631 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6632 return( 0 );
6633 }
6634
6635 return( -1 );
6636 }
6637#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6638
6639#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6640 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6641 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6642 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6643 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6644 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6645 {
6646 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6647 return( 0 );
6648 }
6649
6650 return( -1 );
6651#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6652 MBEDTLS_SSL_PROTO_TLS1_2 */
6653}
6654#endif /* MBEDTLS_SSL_SRV_C */
6655
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006656/* Check if a certificate message is expected.
6657 * Return either
6658 * - SSL_CERTIFICATE_EXPECTED, or
6659 * - SSL_CERTIFICATE_SKIP
6660 * indicating whether a Certificate message is expected or not.
6661 */
6662#define SSL_CERTIFICATE_EXPECTED 0
6663#define SSL_CERTIFICATE_SKIP 1
6664static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6665 int authmode )
6666{
6667 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006668 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006669
6670 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6671 return( SSL_CERTIFICATE_SKIP );
6672
6673#if defined(MBEDTLS_SSL_SRV_C)
6674 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6675 {
6676 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6677 return( SSL_CERTIFICATE_SKIP );
6678
6679 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6680 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006681 ssl->session_negotiate->verify_result =
6682 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6683 return( SSL_CERTIFICATE_SKIP );
6684 }
6685 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006686#else
6687 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006688#endif /* MBEDTLS_SSL_SRV_C */
6689
6690 return( SSL_CERTIFICATE_EXPECTED );
6691}
6692
Hanno Becker68636192019-02-05 14:36:34 +00006693static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6694 int authmode,
6695 mbedtls_x509_crt *chain,
6696 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006697{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006698 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006699 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006700 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006701 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006702
Hanno Becker8927c832019-04-03 12:52:50 +01006703 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6704 void *p_vrfy;
6705
Hanno Becker68636192019-02-05 14:36:34 +00006706 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6707 return( 0 );
6708
Hanno Becker8927c832019-04-03 12:52:50 +01006709 if( ssl->f_vrfy != NULL )
6710 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006711 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006712 f_vrfy = ssl->f_vrfy;
6713 p_vrfy = ssl->p_vrfy;
6714 }
6715 else
6716 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006717 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006718 f_vrfy = ssl->conf->f_vrfy;
6719 p_vrfy = ssl->conf->p_vrfy;
6720 }
6721
Hanno Becker68636192019-02-05 14:36:34 +00006722 /*
6723 * Main check: verify certificate
6724 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006725#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6726 if( ssl->conf->f_ca_cb != NULL )
6727 {
6728 ((void) rs_ctx);
6729 have_ca_chain = 1;
6730
6731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006732 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006733 chain,
6734 ssl->conf->f_ca_cb,
6735 ssl->conf->p_ca_cb,
6736 ssl->conf->cert_profile,
6737 ssl->hostname,
6738 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006739 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006740 }
6741 else
6742#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6743 {
6744 mbedtls_x509_crt *ca_chain;
6745 mbedtls_x509_crl *ca_crl;
6746
6747#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6748 if( ssl->handshake->sni_ca_chain != NULL )
6749 {
6750 ca_chain = ssl->handshake->sni_ca_chain;
6751 ca_crl = ssl->handshake->sni_ca_crl;
6752 }
6753 else
6754#endif
6755 {
6756 ca_chain = ssl->conf->ca_chain;
6757 ca_crl = ssl->conf->ca_crl;
6758 }
6759
6760 if( ca_chain != NULL )
6761 have_ca_chain = 1;
6762
6763 ret = mbedtls_x509_crt_verify_restartable(
6764 chain,
6765 ca_chain, ca_crl,
6766 ssl->conf->cert_profile,
6767 ssl->hostname,
6768 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006769 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006770 }
Hanno Becker68636192019-02-05 14:36:34 +00006771
6772 if( ret != 0 )
6773 {
6774 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6775 }
6776
6777#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6778 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6779 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6780#endif
6781
6782 /*
6783 * Secondary checks: always done, but change 'ret' only if it was 0
6784 */
6785
6786#if defined(MBEDTLS_ECP_C)
6787 {
6788 const mbedtls_pk_context *pk = &chain->pk;
6789
6790 /* If certificate uses an EC key, make sure the curve is OK */
6791 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6792 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6793 {
6794 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6795
6796 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6797 if( ret == 0 )
6798 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6799 }
6800 }
6801#endif /* MBEDTLS_ECP_C */
6802
6803 if( mbedtls_ssl_check_cert_usage( chain,
6804 ciphersuite_info,
6805 ! ssl->conf->endpoint,
6806 &ssl->session_negotiate->verify_result ) != 0 )
6807 {
6808 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6809 if( ret == 0 )
6810 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6811 }
6812
6813 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6814 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6815 * with details encoded in the verification flags. All other kinds
6816 * of error codes, including those from the user provided f_vrfy
6817 * functions, are treated as fatal and lead to a failure of
6818 * ssl_parse_certificate even if verification was optional. */
6819 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6820 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6821 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6822 {
6823 ret = 0;
6824 }
6825
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006826 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006827 {
6828 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6829 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6830 }
6831
6832 if( ret != 0 )
6833 {
6834 uint8_t alert;
6835
6836 /* The certificate may have been rejected for several reasons.
6837 Pick one and send the corresponding alert. Which alert to send
6838 may be a subject of debate in some cases. */
6839 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6840 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6841 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6842 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6843 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6844 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6845 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6846 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6847 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6848 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6849 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6850 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6851 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6852 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6853 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6854 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6855 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6856 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6857 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6858 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6859 else
6860 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6861 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6862 alert );
6863 }
6864
6865#if defined(MBEDTLS_DEBUG_C)
6866 if( ssl->session_negotiate->verify_result != 0 )
6867 {
6868 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6869 ssl->session_negotiate->verify_result ) );
6870 }
6871 else
6872 {
6873 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6874 }
6875#endif /* MBEDTLS_DEBUG_C */
6876
6877 return( ret );
6878}
6879
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006880#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6881static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6882 unsigned char *start, size_t len )
6883{
6884 int ret;
6885 /* Remember digest of the peer's end-CRT. */
6886 ssl->session_negotiate->peer_cert_digest =
6887 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6888 if( ssl->session_negotiate->peer_cert_digest == NULL )
6889 {
6890 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6891 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6892 mbedtls_ssl_send_alert_message( ssl,
6893 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6894 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6895
6896 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6897 }
6898
6899 ret = mbedtls_md( mbedtls_md_info_from_type(
6900 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6901 start, len,
6902 ssl->session_negotiate->peer_cert_digest );
6903
6904 ssl->session_negotiate->peer_cert_digest_type =
6905 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6906 ssl->session_negotiate->peer_cert_digest_len =
6907 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6908
6909 return( ret );
6910}
6911
6912static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6913 unsigned char *start, size_t len )
6914{
6915 unsigned char *end = start + len;
6916 int ret;
6917
6918 /* Make a copy of the peer's raw public key. */
6919 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6920 ret = mbedtls_pk_parse_subpubkey( &start, end,
6921 &ssl->handshake->peer_pubkey );
6922 if( ret != 0 )
6923 {
6924 /* We should have parsed the public key before. */
6925 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6926 }
6927
6928 return( 0 );
6929}
6930#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6931
Hanno Becker68636192019-02-05 14:36:34 +00006932int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6933{
6934 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006935 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006936#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6937 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6938 ? ssl->handshake->sni_authmode
6939 : ssl->conf->authmode;
6940#else
6941 const int authmode = ssl->conf->authmode;
6942#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006943 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006944 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006945
6946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6947
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006948 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6949 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006950 {
6951 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006952 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006953 }
6954
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006955#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6956 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006957 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006958 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006959 chain = ssl->handshake->ecrs_peer_cert;
6960 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006961 goto crt_verify;
6962 }
6963#endif
6964
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006965 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006966 {
6967 /* mbedtls_ssl_read_record may have sent an alert already. We
6968 let it decide whether to alert. */
6969 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006970 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006971 }
6972
Hanno Becker4a55f632019-02-05 12:49:06 +00006973#if defined(MBEDTLS_SSL_SRV_C)
6974 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6975 {
6976 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006977
6978 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006979 ret = 0;
6980 else
6981 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006982
Hanno Becker6bdfab22019-02-05 13:11:17 +00006983 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006984 }
6985#endif /* MBEDTLS_SSL_SRV_C */
6986
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006987 /* Clear existing peer CRT structure in case we tried to
6988 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006989 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006990
6991 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6992 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006993 {
6994 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6995 sizeof( mbedtls_x509_crt ) ) );
6996 mbedtls_ssl_send_alert_message( ssl,
6997 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6998 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006999
Hanno Becker3dad3112019-02-05 17:19:52 +00007000 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7001 goto exit;
7002 }
7003 mbedtls_x509_crt_init( chain );
7004
7005 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00007006 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007007 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02007008
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007009#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7010 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02007011 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02007012
7013crt_verify:
7014 if( ssl->handshake->ecrs_enabled)
7015 rs_ctx = &ssl->handshake->ecrs_ctx;
7016#endif
7017
Hanno Becker68636192019-02-05 14:36:34 +00007018 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00007019 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00007020 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00007021 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00007022
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007023#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007024 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007025 unsigned char *crt_start, *pk_start;
7026 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00007027
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007028 /* We parse the CRT chain without copying, so
7029 * these pointers point into the input buffer,
7030 * and are hence still valid after freeing the
7031 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007032
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007033 crt_start = chain->raw.p;
7034 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00007035
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007036 pk_start = chain->pk_raw.p;
7037 pk_len = chain->pk_raw.len;
7038
7039 /* Free the CRT structures before computing
7040 * digest and copying the peer's public key. */
7041 mbedtls_x509_crt_free( chain );
7042 mbedtls_free( chain );
7043 chain = NULL;
7044
7045 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00007046 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00007047 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007048
7049 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
7050 if( ret != 0 )
7051 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00007052 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007053#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7054 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00007055 ssl->session_negotiate->peer_cert = chain;
7056 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00007057#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00007058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007059 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007060
Hanno Becker6bdfab22019-02-05 13:11:17 +00007061exit:
7062
Hanno Becker3dad3112019-02-05 17:19:52 +00007063 if( ret == 0 )
7064 ssl->state++;
7065
7066#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
7067 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
7068 {
7069 ssl->handshake->ecrs_peer_cert = chain;
7070 chain = NULL;
7071 }
7072#endif
7073
7074 if( chain != NULL )
7075 {
7076 mbedtls_x509_crt_free( chain );
7077 mbedtls_free( chain );
7078 }
7079
Paul Bakker5121ce52009-01-03 21:22:43 +00007080 return( ret );
7081}
Hanno Becker21489932019-02-05 13:20:55 +00007082#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00007083
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007085{
7086 int ret;
7087
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007088 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007090 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00007091 ssl->out_msglen = 1;
7092 ssl->out_msg[0] = 1;
7093
Paul Bakker5121ce52009-01-03 21:22:43 +00007094 ssl->state++;
7095
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007096 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007097 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007098 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007099 return( ret );
7100 }
7101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007103
7104 return( 0 );
7105}
7106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007107int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007108{
7109 int ret;
7110
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007111 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007112
Hanno Becker327c93b2018-08-15 13:56:18 +01007113 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007114 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007115 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007116 return( ret );
7117 }
7118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007119 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007120 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007121 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007122 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7123 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007124 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007125 }
7126
Hanno Beckere678eaa2018-08-21 14:57:46 +01007127 /* CCS records are only accepted if they have length 1 and content '1',
7128 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007129
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007130 /*
7131 * Switch to our negotiated transform and session parameters for inbound
7132 * data.
7133 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007135 ssl->transform_in = ssl->transform_negotiate;
7136 ssl->session_in = ssl->session_negotiate;
7137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007138#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007139 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007141#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007142 ssl_dtls_replay_reset( ssl );
7143#endif
7144
7145 /* Increment epoch */
7146 if( ++ssl->in_epoch == 0 )
7147 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007148 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007149 /* This is highly unlikely to happen for legitimate reasons, so
7150 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007151 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007152 }
7153 }
7154 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007155#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007156 memset( ssl->in_ctr, 0, 8 );
7157
Hanno Becker79594fd2019-05-08 09:38:41 +01007158 ssl_update_in_pointers( ssl );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007160#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7161 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007163 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007165 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007166 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7167 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007168 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007169 }
7170 }
7171#endif
7172
Paul Bakker5121ce52009-01-03 21:22:43 +00007173 ssl->state++;
7174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007176
7177 return( 0 );
7178}
7179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007180void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7181 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007182{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007183 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7186 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7187 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007188 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007189 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007190#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007191#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7192#if defined(MBEDTLS_SHA512_C)
7193 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007194 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7195 else
7196#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197#if defined(MBEDTLS_SHA256_C)
7198 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007199 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007200 else
7201#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007202#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007203 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007204 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007205 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007206 }
Paul Bakker380da532012-04-18 16:10:25 +00007207}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007208
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007209void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007210{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007211#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7212 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007213 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7214 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007215#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007216#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7217#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007218#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007219 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007220 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7221#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007222 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007223#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007224#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007225#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007226#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007227 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007228 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007229#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007230 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007231#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007232#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007233#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007234}
7235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007236static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007237 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007238{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007239#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7240 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007241 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7242 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007243#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007244#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7245#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007246#if defined(MBEDTLS_USE_PSA_CRYPTO)
7247 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7248#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007249 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007250#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007251#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007252#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007253#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007254 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007255#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007256 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007257#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007258#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007259#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007260}
7261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007262#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7263 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7264static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007265 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007266{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007267 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7268 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007269}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007270#endif
Paul Bakker380da532012-04-18 16:10:25 +00007271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7273#if defined(MBEDTLS_SHA256_C)
7274static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007275 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007276{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007277#if defined(MBEDTLS_USE_PSA_CRYPTO)
7278 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7279#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007280 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007281#endif
Paul Bakker380da532012-04-18 16:10:25 +00007282}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007283#endif
Paul Bakker380da532012-04-18 16:10:25 +00007284
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007285#if defined(MBEDTLS_SHA512_C)
7286static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007287 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007288{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007289#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007290 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007291#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007292 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007293#endif
Paul Bakker380da532012-04-18 16:10:25 +00007294}
Paul Bakker769075d2012-11-24 11:26:46 +01007295#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007296#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007299static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007300 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007301{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007302 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007303 mbedtls_md5_context md5;
7304 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007305
Paul Bakker5121ce52009-01-03 21:22:43 +00007306 unsigned char padbuf[48];
7307 unsigned char md5sum[16];
7308 unsigned char sha1sum[20];
7309
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007310 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007311 if( !session )
7312 session = ssl->session;
7313
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007314 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007315
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007316 mbedtls_md5_init( &md5 );
7317 mbedtls_sha1_init( &sha1 );
7318
7319 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7320 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007321
7322 /*
7323 * SSLv3:
7324 * hash =
7325 * MD5( master + pad2 +
7326 * MD5( handshake + sender + master + pad1 ) )
7327 * + SHA1( master + pad2 +
7328 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007329 */
7330
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007331#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007332 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7333 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007334#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007336#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007337 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7338 sha1.state, sizeof( sha1.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 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007342 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007343
Paul Bakker1ef83d62012-04-11 12:09:53 +00007344 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007345
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007346 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7347 mbedtls_md5_update_ret( &md5, session->master, 48 );
7348 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7349 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007350
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007351 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7352 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7353 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7354 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007355
Paul Bakker1ef83d62012-04-11 12:09:53 +00007356 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007357
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007358 mbedtls_md5_starts_ret( &md5 );
7359 mbedtls_md5_update_ret( &md5, session->master, 48 );
7360 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7361 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7362 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007363
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007364 mbedtls_sha1_starts_ret( &sha1 );
7365 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7366 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7367 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7368 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007369
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007370 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007371
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007372 mbedtls_md5_free( &md5 );
7373 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007374
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007375 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7376 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7377 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007378
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007379 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007380}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007381#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007383#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007384static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007385 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007386{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007387 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007388 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007389 mbedtls_md5_context md5;
7390 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007391 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007392
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007393 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007394 if( !session )
7395 session = ssl->session;
7396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007397 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007398
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007399 mbedtls_md5_init( &md5 );
7400 mbedtls_sha1_init( &sha1 );
7401
7402 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7403 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007404
Paul Bakker1ef83d62012-04-11 12:09:53 +00007405 /*
7406 * TLSv1:
7407 * hash = PRF( master, finished_label,
7408 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7409 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007411#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007412 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7413 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007414#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007415
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007416#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007417 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7418 sha1.state, sizeof( sha1.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 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007422 ? "client finished"
7423 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007424
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007425 mbedtls_md5_finish_ret( &md5, padbuf );
7426 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007427
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007428 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007429 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007430
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007431 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007432
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007433 mbedtls_md5_free( &md5 );
7434 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007435
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007436 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007438 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007439}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007440#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007441
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007442#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7443#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007444static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007445 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007446{
7447 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007448 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007449 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007450#if defined(MBEDTLS_USE_PSA_CRYPTO)
7451 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007452 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007453 psa_status_t status;
7454#else
7455 mbedtls_sha256_context sha256;
7456#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007457
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007458 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007459 if( !session )
7460 session = ssl->session;
7461
Andrzej Kurekeb342242019-01-29 09:14:33 -05007462 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7463 ? "client finished"
7464 : "server finished";
7465
7466#if defined(MBEDTLS_USE_PSA_CRYPTO)
7467 sha256_psa = psa_hash_operation_init();
7468
7469 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7470
7471 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7472 if( status != PSA_SUCCESS )
7473 {
7474 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7475 return;
7476 }
7477
7478 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7479 if( status != PSA_SUCCESS )
7480 {
7481 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7482 return;
7483 }
7484 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7485#else
7486
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007487 mbedtls_sha256_init( &sha256 );
7488
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007489 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007490
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007491 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007492
7493 /*
7494 * TLSv1.2:
7495 * hash = PRF( master, finished_label,
7496 * Hash( handshake ) )[0.11]
7497 */
7498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007499#if !defined(MBEDTLS_SHA256_ALT)
7500 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007501 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007502#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007503
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007504 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007505 mbedtls_sha256_free( &sha256 );
7506#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007507
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007508 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007509 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007512
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007513 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007514
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007515 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007516}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007517#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007519#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007520static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007521 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007522{
7523 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007524 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007525 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007526#if defined(MBEDTLS_USE_PSA_CRYPTO)
7527 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007528 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007529 psa_status_t status;
7530#else
7531 mbedtls_sha512_context sha512;
7532#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007534 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007535 if( !session )
7536 session = ssl->session;
7537
Andrzej Kurekeb342242019-01-29 09:14:33 -05007538 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7539 ? "client finished"
7540 : "server finished";
7541
7542#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007543 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007544
7545 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7546
Andrzej Kurek972fba52019-01-30 03:29:12 -05007547 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007548 if( status != PSA_SUCCESS )
7549 {
7550 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7551 return;
7552 }
7553
Andrzej Kurek972fba52019-01-30 03:29:12 -05007554 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007555 if( status != PSA_SUCCESS )
7556 {
7557 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7558 return;
7559 }
7560 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7561#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007562 mbedtls_sha512_init( &sha512 );
7563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007565
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007566 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007567
7568 /*
7569 * TLSv1.2:
7570 * hash = PRF( master, finished_label,
7571 * Hash( handshake ) )[0.11]
7572 */
7573
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007574#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007575 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7576 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007577#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007578
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007579 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007580 mbedtls_sha512_free( &sha512 );
7581#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007582
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007583 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007584 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007585
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007586 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007587
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007588 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007591}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007592#endif /* MBEDTLS_SHA512_C */
7593#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007594
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007595static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007596{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007597 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007598
7599 /*
7600 * Free our handshake params
7601 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007602 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007603 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007604 ssl->handshake = NULL;
7605
7606 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007607 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007608 */
7609 if( ssl->transform )
7610 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007611 mbedtls_ssl_transform_free( ssl->transform );
7612 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007613 }
7614 ssl->transform = ssl->transform_negotiate;
7615 ssl->transform_negotiate = NULL;
7616
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007617 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007618}
7619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007621{
7622 int resume = ssl->handshake->resume;
7623
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007624 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007626#if defined(MBEDTLS_SSL_RENEGOTIATION)
7627 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007628 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007629 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007630 ssl->renego_records_seen = 0;
7631 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007632#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007633
7634 /*
7635 * Free the previous session and switch in the current one
7636 */
Paul Bakker0a597072012-09-25 21:55:46 +00007637 if( ssl->session )
7638 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007639#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007640 /* RFC 7366 3.1: keep the EtM state */
7641 ssl->session_negotiate->encrypt_then_mac =
7642 ssl->session->encrypt_then_mac;
7643#endif
7644
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645 mbedtls_ssl_session_free( ssl->session );
7646 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007647 }
7648 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007649 ssl->session_negotiate = NULL;
7650
Paul Bakker0a597072012-09-25 21:55:46 +00007651 /*
7652 * Add cache entry
7653 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007654 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007655 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007656 resume == 0 )
7657 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007658 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007659 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007660 }
Paul Bakker0a597072012-09-25 21:55:46 +00007661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007662#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007663 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007664 ssl->handshake->flight != NULL )
7665 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007666 /* Cancel handshake timer */
7667 ssl_set_timer( ssl, 0 );
7668
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007669 /* Keep last flight around in case we need to resend it:
7670 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007671 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007672 }
7673 else
7674#endif
7675 ssl_handshake_wrapup_free_hs_transform( ssl );
7676
Paul Bakker48916f92012-09-16 19:57:18 +00007677 ssl->state++;
7678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007679 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007680}
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007683{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007684 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007686 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007687
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007688 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007689
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007690 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007691
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007692 /*
7693 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7694 * may define some other value. Currently (early 2016), no defined
7695 * ciphersuite does this (and this is unlikely to change as activity has
7696 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7697 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007698 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007699
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007700#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007701 ssl->verify_data_len = hash_len;
7702 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007703#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007704
Paul Bakker5121ce52009-01-03 21:22:43 +00007705 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007706 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7707 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007708
7709 /*
7710 * In case of session resuming, invert the client and server
7711 * ChangeCipherSpec messages order.
7712 */
Paul Bakker0a597072012-09-25 21:55:46 +00007713 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007714 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007715#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007716 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007717 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007718#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007719#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007720 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007721 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007722#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007723 }
7724 else
7725 ssl->state++;
7726
Paul Bakker48916f92012-09-16 19:57:18 +00007727 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007728 * Switch to our negotiated transform and session parameters for outbound
7729 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007730 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007734 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007735 {
7736 unsigned char i;
7737
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007738 /* Remember current epoch settings for resending */
7739 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007740 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007741
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007742 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007743 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007744
7745 /* Increment epoch */
7746 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007747 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007748 break;
7749
7750 /* The loop goes to its end iff the counter is wrapping */
7751 if( i == 0 )
7752 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007753 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7754 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007755 }
7756 }
7757 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007758#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007759 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007760
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007761 ssl->transform_out = ssl->transform_negotiate;
7762 ssl->session_out = ssl->session_negotiate;
7763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7765 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007766 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007769 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7770 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007771 }
7772 }
7773#endif
7774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007775#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007776 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007777 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007778#endif
7779
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007780 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007781 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007782 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007783 return( ret );
7784 }
7785
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007786#if defined(MBEDTLS_SSL_PROTO_DTLS)
7787 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7788 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7789 {
7790 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7791 return( ret );
7792 }
7793#endif
7794
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007795 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007796
7797 return( 0 );
7798}
7799
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007800#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007801#define SSL_MAX_HASH_LEN 36
7802#else
7803#define SSL_MAX_HASH_LEN 12
7804#endif
7805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007806int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007807{
Paul Bakker23986e52011-04-24 08:57:21 +00007808 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007809 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007810 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007812 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007813
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007814 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007815
Hanno Becker327c93b2018-08-15 13:56:18 +01007816 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007817 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007818 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007819 return( ret );
7820 }
7821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007822 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007823 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007824 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007825 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7826 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007827 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007828 }
7829
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007830 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007831#if defined(MBEDTLS_SSL_PROTO_SSL3)
7832 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007833 hash_len = 36;
7834 else
7835#endif
7836 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007837
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007838 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7839 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007840 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007842 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7843 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007844 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007845 }
7846
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007848 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007849 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007850 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007851 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7852 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007853 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007854 }
7855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007856#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007857 ssl->verify_data_len = hash_len;
7858 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007859#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007860
Paul Bakker0a597072012-09-25 21:55:46 +00007861 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007864 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007865 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007866#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007867#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007868 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007869 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007870#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007871 }
7872 else
7873 ssl->state++;
7874
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007875#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007876 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007877 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007878#endif
7879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007880 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007881
7882 return( 0 );
7883}
7884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007885static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007886{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007887 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007888
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007889#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7890 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7891 mbedtls_md5_init( &handshake->fin_md5 );
7892 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007893 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7894 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007895#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007896#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7897#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007898#if defined(MBEDTLS_USE_PSA_CRYPTO)
7899 handshake->fin_sha256_psa = psa_hash_operation_init();
7900 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7901#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007902 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007903 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007904#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007905#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007906#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007907#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007908 handshake->fin_sha384_psa = psa_hash_operation_init();
7909 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007910#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007911 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007912 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007913#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007914#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007915#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007916
7917 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007918
7919#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7920 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7921 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7922#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007923
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007924#if defined(MBEDTLS_DHM_C)
7925 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007926#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007927#if defined(MBEDTLS_ECDH_C)
7928 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007929#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007930#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007931 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007932#if defined(MBEDTLS_SSL_CLI_C)
7933 handshake->ecjpake_cache = NULL;
7934 handshake->ecjpake_cache_len = 0;
7935#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007936#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007937
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007938#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007939 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007940#endif
7941
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007942#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7943 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7944#endif
Hanno Becker75173122019-02-06 16:18:31 +00007945
7946#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7947 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7948 mbedtls_pk_init( &handshake->peer_pubkey );
7949#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007950}
7951
Hanno Beckera18d1322018-01-03 14:27:32 +00007952void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007953{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007954 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007956 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7957 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007958
Hanno Beckerd56ed242018-01-03 15:32:51 +00007959#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007960 mbedtls_md_init( &transform->md_ctx_enc );
7961 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007962#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007963}
7964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007965void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007966{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007967 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007968}
7969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007970static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007971{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007972 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007973 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007974 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007975 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007976 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007977 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007978 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007979
7980 /*
7981 * Either the pointers are now NULL or cleared properly and can be freed.
7982 * Now allocate missing structures.
7983 */
7984 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007985 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007986 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007987 }
Paul Bakker48916f92012-09-16 19:57:18 +00007988
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007989 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007990 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007991 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007992 }
Paul Bakker48916f92012-09-16 19:57:18 +00007993
Paul Bakker82788fb2014-10-20 13:59:19 +02007994 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007995 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007996 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007997 }
Paul Bakker48916f92012-09-16 19:57:18 +00007998
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007999 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00008000 if( ssl->handshake == NULL ||
8001 ssl->transform_negotiate == NULL ||
8002 ssl->session_negotiate == NULL )
8003 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02008004 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008006 mbedtls_free( ssl->handshake );
8007 mbedtls_free( ssl->transform_negotiate );
8008 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008009
8010 ssl->handshake = NULL;
8011 ssl->transform_negotiate = NULL;
8012 ssl->session_negotiate = NULL;
8013
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008014 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00008015 }
8016
Paul Bakkeraccaffe2014-06-26 13:37:14 +02008017 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008018 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00008019 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02008020 ssl_handshake_params_init( ssl->handshake );
8021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008022#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008023 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8024 {
8025 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008026
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008027 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
8028 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
8029 else
8030 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008031
8032 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02008033 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02008034#endif
8035
Paul Bakker48916f92012-09-16 19:57:18 +00008036 return( 0 );
8037}
8038
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008039#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008040/* Dummy cookie callbacks for defaults */
8041static int ssl_cookie_write_dummy( void *ctx,
8042 unsigned char **p, unsigned char *end,
8043 const unsigned char *cli_id, size_t cli_id_len )
8044{
8045 ((void) ctx);
8046 ((void) p);
8047 ((void) end);
8048 ((void) cli_id);
8049 ((void) cli_id_len);
8050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008051 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008052}
8053
8054static int ssl_cookie_check_dummy( void *ctx,
8055 const unsigned char *cookie, size_t cookie_len,
8056 const unsigned char *cli_id, size_t cli_id_len )
8057{
8058 ((void) ctx);
8059 ((void) cookie);
8060 ((void) cookie_len);
8061 ((void) cli_id);
8062 ((void) cli_id_len);
8063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008064 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008065}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008066#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02008067
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008068/* Once ssl->out_hdr as the address of the beginning of the
8069 * next outgoing record is set, deduce the other pointers.
8070 *
8071 * Note: For TLS, we save the implicit record sequence number
8072 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
8073 * and the caller has to make sure there's space for this.
8074 */
8075
8076static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
8077 mbedtls_ssl_transform *transform )
8078{
8079#if defined(MBEDTLS_SSL_PROTO_DTLS)
8080 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8081 {
8082 ssl->out_ctr = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008083#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008084 ssl->out_cid = ssl->out_ctr + 8;
8085 ssl->out_len = ssl->out_cid;
8086 if( transform != NULL )
8087 ssl->out_len += transform->out_cid_len;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008088#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008089 ssl->out_len = ssl->out_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008090#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008091 ssl->out_iv = ssl->out_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008092 }
8093 else
8094#endif
8095 {
8096 ssl->out_ctr = ssl->out_hdr - 8;
8097 ssl->out_len = ssl->out_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008098#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008099 ssl->out_cid = ssl->out_len;
8100#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008101 ssl->out_iv = ssl->out_hdr + 5;
8102 }
8103
8104 /* Adjust out_msg to make space for explicit IV, if used. */
8105 if( transform != NULL &&
8106 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8107 {
8108 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
8109 }
8110 else
8111 ssl->out_msg = ssl->out_iv;
8112}
8113
8114/* Once ssl->in_hdr as the address of the beginning of the
8115 * next incoming record is set, deduce the other pointers.
8116 *
8117 * Note: For TLS, we save the implicit record sequence number
8118 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
8119 * and the caller has to make sure there's space for this.
8120 */
8121
Hanno Becker79594fd2019-05-08 09:38:41 +01008122static void ssl_update_in_pointers( mbedtls_ssl_context *ssl )
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008123{
Hanno Becker79594fd2019-05-08 09:38:41 +01008124 /* This function sets the pointers to match the case
8125 * of unprotected TLS/DTLS records, with both ssl->in_iv
8126 * and ssl->in_msg pointing to the beginning of the record
8127 * content.
8128 *
8129 * When decrypting a protected record, ssl->in_msg
8130 * will be shifted to point to the beginning of the
8131 * record plaintext.
8132 */
8133
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008134#if defined(MBEDTLS_SSL_PROTO_DTLS)
8135 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8136 {
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008137 /* This sets the header pointers to match records
8138 * without CID. When we receive a record containing
8139 * a CID, the fields are shifted accordingly in
8140 * ssl_parse_record_header(). */
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008141 ssl->in_ctr = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008142#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008143 ssl->in_cid = ssl->in_ctr + 8;
8144 ssl->in_len = ssl->in_cid; /* Default: no CID */
Hanno Beckera0e20d02019-05-15 14:03:01 +01008145#else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008146 ssl->in_len = ssl->in_ctr + 8;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf9c6a4b2019-05-03 14:34:53 +01008148 ssl->in_iv = ssl->in_len + 2;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008149 }
8150 else
8151#endif
8152 {
8153 ssl->in_ctr = ssl->in_hdr - 8;
8154 ssl->in_len = ssl->in_hdr + 3;
Hanno Beckera0e20d02019-05-15 14:03:01 +01008155#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker4c3eb7c2019-05-08 16:43:21 +01008156 ssl->in_cid = ssl->in_len;
8157#endif
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008158 ssl->in_iv = ssl->in_hdr + 5;
8159 }
8160
Hanno Becker79594fd2019-05-08 09:38:41 +01008161 /* This will be adjusted at record decryption time. */
8162 ssl->in_msg = ssl->in_iv;
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01008163}
8164
Paul Bakker5121ce52009-01-03 21:22:43 +00008165/*
8166 * Initialize an SSL context
8167 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008168void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8169{
8170 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8171}
8172
8173/*
8174 * Setup an SSL context
8175 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008176
8177static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8178{
8179 /* Set the incoming and outgoing record pointers. */
8180#if defined(MBEDTLS_SSL_PROTO_DTLS)
8181 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8182 {
8183 ssl->out_hdr = ssl->out_buf;
8184 ssl->in_hdr = ssl->in_buf;
8185 }
8186 else
8187#endif /* MBEDTLS_SSL_PROTO_DTLS */
8188 {
8189 ssl->out_hdr = ssl->out_buf + 8;
8190 ssl->in_hdr = ssl->in_buf + 8;
8191 }
8192
8193 /* Derive other internal pointers. */
8194 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
Hanno Becker79594fd2019-05-08 09:38:41 +01008195 ssl_update_in_pointers ( ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008196}
8197
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008198int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008199 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008200{
Paul Bakker48916f92012-09-16 19:57:18 +00008201 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008202
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008203 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008204
8205 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008206 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008207 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008208
8209 /* Set to NULL in case of an error condition */
8210 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008211
Angus Grattond8213d02016-05-25 20:56:48 +10008212 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8213 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008214 {
Angus Grattond8213d02016-05-25 20:56:48 +10008215 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008216 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008217 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008218 }
8219
8220 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8221 if( ssl->out_buf == NULL )
8222 {
8223 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008224 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008225 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008226 }
8227
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008228 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008229
Paul Bakker48916f92012-09-16 19:57:18 +00008230 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008231 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008232
8233 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008234
8235error:
8236 mbedtls_free( ssl->in_buf );
8237 mbedtls_free( ssl->out_buf );
8238
8239 ssl->conf = NULL;
8240
8241 ssl->in_buf = NULL;
8242 ssl->out_buf = NULL;
8243
8244 ssl->in_hdr = NULL;
8245 ssl->in_ctr = NULL;
8246 ssl->in_len = NULL;
8247 ssl->in_iv = NULL;
8248 ssl->in_msg = NULL;
8249
8250 ssl->out_hdr = NULL;
8251 ssl->out_ctr = NULL;
8252 ssl->out_len = NULL;
8253 ssl->out_iv = NULL;
8254 ssl->out_msg = NULL;
8255
k-stachowiak9f7798e2018-07-31 16:52:32 +02008256 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008257}
8258
8259/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008260 * Reset an initialized and used SSL context for re-use while retaining
8261 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008262 *
8263 * If partial is non-zero, keep data in the input buffer and client ID.
8264 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008265 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008266static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008267{
Paul Bakker48916f92012-09-16 19:57:18 +00008268 int ret;
8269
Hanno Becker7e772132018-08-10 12:38:21 +01008270#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8271 !defined(MBEDTLS_SSL_SRV_C)
8272 ((void) partial);
8273#endif
8274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008275 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008276
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008277 /* Cancel any possibly running timer */
8278 ssl_set_timer( ssl, 0 );
8279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008280#if defined(MBEDTLS_SSL_RENEGOTIATION)
8281 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008282 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008283
8284 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008285 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8286 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008287#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008288 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008289
Paul Bakker7eb013f2011-10-06 12:37:39 +00008290 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008291 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008292
8293 ssl->in_msgtype = 0;
8294 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008296 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008297 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008298#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008299#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008300 ssl_dtls_replay_reset( ssl );
8301#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008302
8303 ssl->in_hslen = 0;
8304 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008305
8306 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008307
8308 ssl->out_msgtype = 0;
8309 ssl->out_msglen = 0;
8310 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008311#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8312 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008313 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008314#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008315
Hanno Becker19859472018-08-06 09:40:20 +01008316 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8317
Paul Bakker48916f92012-09-16 19:57:18 +00008318 ssl->transform_in = NULL;
8319 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008320
Hanno Becker78640902018-08-13 16:35:15 +01008321 ssl->session_in = NULL;
8322 ssl->session_out = NULL;
8323
Angus Grattond8213d02016-05-25 20:56:48 +10008324 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008325
8326#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008327 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008328#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8329 {
8330 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008331 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008332 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008334#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8335 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008336 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008337 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8338 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008339 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008340 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8341 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008342 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008343 }
8344#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008345
Paul Bakker48916f92012-09-16 19:57:18 +00008346 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008347 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008348 mbedtls_ssl_transform_free( ssl->transform );
8349 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008350 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008351 }
Paul Bakker48916f92012-09-16 19:57:18 +00008352
Paul Bakkerc0463502013-02-14 11:19:38 +01008353 if( ssl->session )
8354 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008355 mbedtls_ssl_session_free( ssl->session );
8356 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008357 ssl->session = NULL;
8358 }
8359
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008360#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008361 ssl->alpn_chosen = NULL;
8362#endif
8363
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008364#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008365#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008366 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008367#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008368 {
8369 mbedtls_free( ssl->cli_id );
8370 ssl->cli_id = NULL;
8371 ssl->cli_id_len = 0;
8372 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008373#endif
8374
Paul Bakker48916f92012-09-16 19:57:18 +00008375 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8376 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008377
8378 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008379}
8380
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008381/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008382 * Reset an initialized and used SSL context for re-use while retaining
8383 * all application-set variables, function pointers and data.
8384 */
8385int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8386{
8387 return( ssl_session_reset_int( ssl, 0 ) );
8388}
8389
8390/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008391 * SSL set accessors
8392 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008393void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008394{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008395 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008396}
8397
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008398void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008399{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008400 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008401}
8402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008403#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008404void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008405{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008406 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008407}
8408#endif
8409
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008410#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008411void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008412{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008413 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008414}
8415#endif
8416
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008417#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008418
Hanno Becker1841b0a2018-08-24 11:13:57 +01008419void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8420 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008421{
8422 ssl->disable_datagram_packing = !allow_packing;
8423}
8424
8425void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8426 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008427{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008428 conf->hs_timeout_min = min;
8429 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008430}
8431#endif
8432
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008433void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008434{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008435 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008436}
8437
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008438#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008439void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008440 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008441 void *p_vrfy )
8442{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008443 conf->f_vrfy = f_vrfy;
8444 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008445}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008446#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008447
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008448void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008449 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008450 void *p_rng )
8451{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008452 conf->f_rng = f_rng;
8453 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008454}
8455
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008456void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008457 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008458 void *p_dbg )
8459{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008460 conf->f_dbg = f_dbg;
8461 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008462}
8463
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008464void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008465 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008466 mbedtls_ssl_send_t *f_send,
8467 mbedtls_ssl_recv_t *f_recv,
8468 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008469{
8470 ssl->p_bio = p_bio;
8471 ssl->f_send = f_send;
8472 ssl->f_recv = f_recv;
8473 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008474}
8475
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008476#if defined(MBEDTLS_SSL_PROTO_DTLS)
8477void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8478{
8479 ssl->mtu = mtu;
8480}
8481#endif
8482
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008483void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008484{
8485 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008486}
8487
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008488void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8489 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008490 mbedtls_ssl_set_timer_t *f_set_timer,
8491 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008492{
8493 ssl->p_timer = p_timer;
8494 ssl->f_set_timer = f_set_timer;
8495 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008496
8497 /* Make sure we start with no timer running */
8498 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008499}
8500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008501#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008502void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008503 void *p_cache,
8504 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8505 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008506{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008507 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008508 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008509 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008510}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008511#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008513#if defined(MBEDTLS_SSL_CLI_C)
8514int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008515{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008516 int ret;
8517
8518 if( ssl == NULL ||
8519 session == NULL ||
8520 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008521 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008522 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008523 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008524 }
8525
Hanno Becker52055ae2019-02-06 14:30:46 +00008526 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8527 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008528 return( ret );
8529
Paul Bakker0a597072012-09-25 21:55:46 +00008530 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008531
8532 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008533}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008534#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008535
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008536void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008537 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008539 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8540 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8541 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8542 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008543}
8544
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008545void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008546 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008547 int major, int minor )
8548{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008549 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008550 return;
8551
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008552 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008553 return;
8554
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008555 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008556}
8557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008558#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008559void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008560 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008561{
8562 conf->cert_profile = profile;
8563}
8564
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008565/* Append a new keycert entry to a (possibly empty) list */
8566static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8567 mbedtls_x509_crt *cert,
8568 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008569{
niisato8ee24222018-06-25 19:05:48 +09008570 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008571
niisato8ee24222018-06-25 19:05:48 +09008572 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8573 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008574 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008575
niisato8ee24222018-06-25 19:05:48 +09008576 new_cert->cert = cert;
8577 new_cert->key = key;
8578 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008579
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008580 /* Update head is the list was null, else add to the end */
8581 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008582 {
niisato8ee24222018-06-25 19:05:48 +09008583 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008584 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008585 else
8586 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008587 mbedtls_ssl_key_cert *cur = *head;
8588 while( cur->next != NULL )
8589 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008590 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008591 }
8592
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008593 return( 0 );
8594}
8595
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008596int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008597 mbedtls_x509_crt *own_cert,
8598 mbedtls_pk_context *pk_key )
8599{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008600 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008601}
8602
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008603void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008604 mbedtls_x509_crt *ca_chain,
8605 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008606{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008607 conf->ca_chain = ca_chain;
8608 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008609
8610#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8611 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8612 * cannot be used together. */
8613 conf->f_ca_cb = NULL;
8614 conf->p_ca_cb = NULL;
8615#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008616}
Hanno Becker5adaad92019-03-27 16:54:37 +00008617
8618#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8619void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008620 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008621 void *p_ca_cb )
8622{
8623 conf->f_ca_cb = f_ca_cb;
8624 conf->p_ca_cb = p_ca_cb;
8625
8626 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8627 * cannot be used together. */
8628 conf->ca_chain = NULL;
8629 conf->ca_crl = NULL;
8630}
8631#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008632#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008633
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008634#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8635int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8636 mbedtls_x509_crt *own_cert,
8637 mbedtls_pk_context *pk_key )
8638{
8639 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8640 own_cert, pk_key ) );
8641}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008642
8643void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8644 mbedtls_x509_crt *ca_chain,
8645 mbedtls_x509_crl *ca_crl )
8646{
8647 ssl->handshake->sni_ca_chain = ca_chain;
8648 ssl->handshake->sni_ca_crl = ca_crl;
8649}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008650
8651void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8652 int authmode )
8653{
8654 ssl->handshake->sni_authmode = authmode;
8655}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008656#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8657
Hanno Becker8927c832019-04-03 12:52:50 +01008658#if defined(MBEDTLS_X509_CRT_PARSE_C)
8659void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8660 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8661 void *p_vrfy )
8662{
8663 ssl->f_vrfy = f_vrfy;
8664 ssl->p_vrfy = p_vrfy;
8665}
8666#endif
8667
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008668#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008669/*
8670 * Set EC J-PAKE password for current handshake
8671 */
8672int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8673 const unsigned char *pw,
8674 size_t pw_len )
8675{
8676 mbedtls_ecjpake_role role;
8677
Janos Follath8eb64132016-06-03 15:40:57 +01008678 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008679 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8680
8681 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8682 role = MBEDTLS_ECJPAKE_SERVER;
8683 else
8684 role = MBEDTLS_ECJPAKE_CLIENT;
8685
8686 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8687 role,
8688 MBEDTLS_MD_SHA256,
8689 MBEDTLS_ECP_DP_SECP256R1,
8690 pw, pw_len ) );
8691}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008692#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008694#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008695
8696static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8697{
8698 /* Remove reference to existing PSK, if any. */
8699#if defined(MBEDTLS_USE_PSA_CRYPTO)
8700 if( conf->psk_opaque != 0 )
8701 {
8702 /* The maintenance of the PSK key slot is the
8703 * user's responsibility. */
8704 conf->psk_opaque = 0;
8705 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008706 /* This and the following branch should never
8707 * be taken simultaenously as we maintain the
8708 * invariant that raw and opaque PSKs are never
8709 * configured simultaneously. As a safeguard,
8710 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008711#endif /* MBEDTLS_USE_PSA_CRYPTO */
8712 if( conf->psk != NULL )
8713 {
8714 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8715
8716 mbedtls_free( conf->psk );
8717 conf->psk = NULL;
8718 conf->psk_len = 0;
8719 }
8720
8721 /* Remove reference to PSK identity, if any. */
8722 if( conf->psk_identity != NULL )
8723 {
8724 mbedtls_free( conf->psk_identity );
8725 conf->psk_identity = NULL;
8726 conf->psk_identity_len = 0;
8727 }
8728}
8729
Hanno Becker7390c712018-11-15 13:33:04 +00008730/* This function assumes that PSK identity in the SSL config is unset.
8731 * It checks that the provided identity is well-formed and attempts
8732 * to make a copy of it in the SSL config.
8733 * On failure, the PSK identity in the config remains unset. */
8734static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8735 unsigned char const *psk_identity,
8736 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008737{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008738 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008739 if( psk_identity == NULL ||
8740 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008741 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008742 {
8743 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8744 }
8745
Hanno Becker7390c712018-11-15 13:33:04 +00008746 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8747 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008748 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008749
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008750 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008751 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008752
8753 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008754}
8755
Hanno Becker7390c712018-11-15 13:33:04 +00008756int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8757 const unsigned char *psk, size_t psk_len,
8758 const unsigned char *psk_identity, size_t psk_identity_len )
8759{
8760 int ret;
8761 /* Remove opaque/raw PSK + PSK Identity */
8762 ssl_conf_remove_psk( conf );
8763
8764 /* Check and set raw PSK */
8765 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8766 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8767 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8768 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8769 conf->psk_len = psk_len;
8770 memcpy( conf->psk, psk, conf->psk_len );
8771
8772 /* Check and set PSK Identity */
8773 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8774 if( ret != 0 )
8775 ssl_conf_remove_psk( conf );
8776
8777 return( ret );
8778}
8779
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008780static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8781{
8782#if defined(MBEDTLS_USE_PSA_CRYPTO)
8783 if( ssl->handshake->psk_opaque != 0 )
8784 {
8785 ssl->handshake->psk_opaque = 0;
8786 }
8787 else
8788#endif /* MBEDTLS_USE_PSA_CRYPTO */
8789 if( ssl->handshake->psk != NULL )
8790 {
8791 mbedtls_platform_zeroize( ssl->handshake->psk,
8792 ssl->handshake->psk_len );
8793 mbedtls_free( ssl->handshake->psk );
8794 ssl->handshake->psk_len = 0;
8795 }
8796}
8797
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008798int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8799 const unsigned char *psk, size_t psk_len )
8800{
8801 if( psk == NULL || ssl->handshake == NULL )
8802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8803
8804 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8805 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8806
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008807 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008808
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008809 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008810 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008811
8812 ssl->handshake->psk_len = psk_len;
8813 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8814
8815 return( 0 );
8816}
8817
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008818#if defined(MBEDTLS_USE_PSA_CRYPTO)
8819int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008820 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008821 const unsigned char *psk_identity,
8822 size_t psk_identity_len )
8823{
Hanno Becker7390c712018-11-15 13:33:04 +00008824 int ret;
8825 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008826 ssl_conf_remove_psk( conf );
8827
Hanno Becker7390c712018-11-15 13:33:04 +00008828 /* Check and set opaque PSK */
8829 if( psk_slot == 0 )
8830 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008831 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008832
8833 /* Check and set PSK Identity */
8834 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8835 psk_identity_len );
8836 if( ret != 0 )
8837 ssl_conf_remove_psk( conf );
8838
8839 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008840}
8841
8842int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008843 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008844{
8845 if( psk_slot == 0 || ssl->handshake == NULL )
8846 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8847
8848 ssl_remove_psk( ssl );
8849 ssl->handshake->psk_opaque = psk_slot;
8850 return( 0 );
8851}
8852#endif /* MBEDTLS_USE_PSA_CRYPTO */
8853
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008854void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008855 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008856 size_t),
8857 void *p_psk )
8858{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008859 conf->f_psk = f_psk;
8860 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008861}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008862#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008863
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008864#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008865
8866#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008867int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008868{
8869 int ret;
8870
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008871 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8872 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8873 {
8874 mbedtls_mpi_free( &conf->dhm_P );
8875 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008876 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008877 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008878
8879 return( 0 );
8880}
Hanno Becker470a8c42017-10-04 15:28:46 +01008881#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008882
Hanno Beckera90658f2017-10-04 15:29:08 +01008883int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8884 const unsigned char *dhm_P, size_t P_len,
8885 const unsigned char *dhm_G, size_t G_len )
8886{
8887 int ret;
8888
8889 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8890 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8891 {
8892 mbedtls_mpi_free( &conf->dhm_P );
8893 mbedtls_mpi_free( &conf->dhm_G );
8894 return( ret );
8895 }
8896
8897 return( 0 );
8898}
Paul Bakker5121ce52009-01-03 21:22:43 +00008899
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008900int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008901{
8902 int ret;
8903
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008904 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8905 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8906 {
8907 mbedtls_mpi_free( &conf->dhm_P );
8908 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008909 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008910 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008911
8912 return( 0 );
8913}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008914#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008915
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008916#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8917/*
8918 * Set the minimum length for Diffie-Hellman parameters
8919 */
8920void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8921 unsigned int bitlen )
8922{
8923 conf->dhm_min_bitlen = bitlen;
8924}
8925#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8926
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008927#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008928/*
8929 * Set allowed/preferred hashes for handshake signatures
8930 */
8931void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8932 const int *hashes )
8933{
8934 conf->sig_hashes = hashes;
8935}
Hanno Becker947194e2017-04-07 13:25:49 +01008936#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008937
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008938#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008939/*
8940 * Set the allowed elliptic curves
8941 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008942void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008943 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008944{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008945 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008946}
Hanno Becker947194e2017-04-07 13:25:49 +01008947#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008948
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008949#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008950int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008951{
Hanno Becker947194e2017-04-07 13:25:49 +01008952 /* Initialize to suppress unnecessary compiler warning */
8953 size_t hostname_len = 0;
8954
8955 /* Check if new hostname is valid before
8956 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008957 if( hostname != NULL )
8958 {
8959 hostname_len = strlen( hostname );
8960
8961 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8962 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8963 }
8964
8965 /* Now it's clear that we will overwrite the old hostname,
8966 * so we can free it safely */
8967
8968 if( ssl->hostname != NULL )
8969 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008970 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008971 mbedtls_free( ssl->hostname );
8972 }
8973
8974 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008975
Paul Bakker5121ce52009-01-03 21:22:43 +00008976 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008977 {
8978 ssl->hostname = NULL;
8979 }
8980 else
8981 {
8982 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008983 if( ssl->hostname == NULL )
8984 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008985
Hanno Becker947194e2017-04-07 13:25:49 +01008986 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008987
Hanno Becker947194e2017-04-07 13:25:49 +01008988 ssl->hostname[hostname_len] = '\0';
8989 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008990
8991 return( 0 );
8992}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008993#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008994
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008995#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008996void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008997 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008998 const unsigned char *, size_t),
8999 void *p_sni )
9000{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009001 conf->f_sni = f_sni;
9002 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00009003}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009004#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00009005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009006#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009007int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009008{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009009 size_t cur_len, tot_len;
9010 const char **p;
9011
9012 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08009013 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
9014 * MUST NOT be truncated."
9015 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009016 */
9017 tot_len = 0;
9018 for( p = protos; *p != NULL; p++ )
9019 {
9020 cur_len = strlen( *p );
9021 tot_len += cur_len;
9022
9023 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009024 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009025 }
9026
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009027 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02009028
9029 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009030}
9031
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009032const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009033{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009034 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009035}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009036#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02009037
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009038void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00009039{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009040 conf->max_major_ver = major;
9041 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00009042}
9043
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009044void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00009045{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009046 conf->min_major_ver = major;
9047 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00009048}
9049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009050#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009051void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009052{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01009053 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02009054}
9055#endif
9056
Janos Follath088ce432017-04-10 12:42:31 +01009057#if defined(MBEDTLS_SSL_SRV_C)
9058void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
9059 char cert_req_ca_list )
9060{
9061 conf->cert_req_ca_list = cert_req_ca_list;
9062}
9063#endif
9064
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009065#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009066void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009067{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009068 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01009069}
9070#endif
9071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009072#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009073void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009075 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02009076}
9077#endif
9078
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009079#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009080void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009081{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009082 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009083}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02009084#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01009085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009086#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009087int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009088{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009089 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10009090 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009091 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009092 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009093 }
9094
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01009095 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009096
9097 return( 0 );
9098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009099#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02009100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009101#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02009102void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009103{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009104 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009105}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009106#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02009107
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009108#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009109void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009110{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009111 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009112}
9113#endif
9114
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009115void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00009116{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009117 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00009118}
9119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009120#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009121void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009122{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009123 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01009124}
9125
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009126void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009127{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009128 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009129}
9130
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02009131void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009132 const unsigned char period[8] )
9133{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02009134 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01009135}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009136#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009138#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009139#if defined(MBEDTLS_SSL_CLI_C)
9140void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009141{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009142 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009143}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009144#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009145
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009146#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009147void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9148 mbedtls_ssl_ticket_write_t *f_ticket_write,
9149 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9150 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009151{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009152 conf->f_ticket_write = f_ticket_write;
9153 conf->f_ticket_parse = f_ticket_parse;
9154 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009155}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009156#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009157#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009158
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009159#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9160void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9161 mbedtls_ssl_export_keys_t *f_export_keys,
9162 void *p_export_keys )
9163{
9164 conf->f_export_keys = f_export_keys;
9165 conf->p_export_keys = p_export_keys;
9166}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009167
9168void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9169 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9170 void *p_export_keys )
9171{
9172 conf->f_export_keys_ext = f_export_keys_ext;
9173 conf->p_export_keys = p_export_keys;
9174}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009175#endif
9176
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009177#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009178void mbedtls_ssl_conf_async_private_cb(
9179 mbedtls_ssl_config *conf,
9180 mbedtls_ssl_async_sign_t *f_async_sign,
9181 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9182 mbedtls_ssl_async_resume_t *f_async_resume,
9183 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009184 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009185{
9186 conf->f_async_sign_start = f_async_sign;
9187 conf->f_async_decrypt_start = f_async_decrypt;
9188 conf->f_async_resume = f_async_resume;
9189 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009190 conf->p_async_config_data = async_config_data;
9191}
9192
Gilles Peskine8f97af72018-04-26 11:46:10 +02009193void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9194{
9195 return( conf->p_async_config_data );
9196}
9197
Gilles Peskine1febfef2018-04-30 11:54:39 +02009198void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009199{
9200 if( ssl->handshake == NULL )
9201 return( NULL );
9202 else
9203 return( ssl->handshake->user_async_ctx );
9204}
9205
Gilles Peskine1febfef2018-04-30 11:54:39 +02009206void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009207 void *ctx )
9208{
9209 if( ssl->handshake != NULL )
9210 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009211}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009212#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009213
Paul Bakker5121ce52009-01-03 21:22:43 +00009214/*
9215 * SSL get accessors
9216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009217size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009218{
9219 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9220}
9221
Hanno Becker8b170a02017-10-10 11:51:19 +01009222int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9223{
9224 /*
9225 * Case A: We're currently holding back
9226 * a message for further processing.
9227 */
9228
9229 if( ssl->keep_current_message == 1 )
9230 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009232 return( 1 );
9233 }
9234
9235 /*
9236 * Case B: Further records are pending in the current datagram.
9237 */
9238
9239#if defined(MBEDTLS_SSL_PROTO_DTLS)
9240 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9241 ssl->in_left > ssl->next_record_offset )
9242 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009243 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009244 return( 1 );
9245 }
9246#endif /* MBEDTLS_SSL_PROTO_DTLS */
9247
9248 /*
9249 * Case C: A handshake message is being processed.
9250 */
9251
Hanno Becker8b170a02017-10-10 11:51:19 +01009252 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9253 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009254 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009255 return( 1 );
9256 }
9257
9258 /*
9259 * Case D: An application data message is being processed
9260 */
9261 if( ssl->in_offt != NULL )
9262 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009264 return( 1 );
9265 }
9266
9267 /*
9268 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009269 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009270 * we implement support for multiple alerts in single records.
9271 */
9272
9273 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9274 return( 0 );
9275}
9276
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009277uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009278{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009279 if( ssl->session != NULL )
9280 return( ssl->session->verify_result );
9281
9282 if( ssl->session_negotiate != NULL )
9283 return( ssl->session_negotiate->verify_result );
9284
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009285 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009286}
9287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009288const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009289{
Paul Bakker926c8e42013-03-06 10:23:34 +01009290 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009291 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009292
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009293 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009294}
9295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009296const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009297{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009298#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009299 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009300 {
9301 switch( ssl->minor_ver )
9302 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009303 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009304 return( "DTLSv1.0" );
9305
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009306 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009307 return( "DTLSv1.2" );
9308
9309 default:
9310 return( "unknown (DTLS)" );
9311 }
9312 }
9313#endif
9314
Paul Bakker43ca69c2011-01-15 17:35:19 +00009315 switch( ssl->minor_ver )
9316 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009317 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009318 return( "SSLv3.0" );
9319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009320 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009321 return( "TLSv1.0" );
9322
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009323 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009324 return( "TLSv1.1" );
9325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009326 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009327 return( "TLSv1.2" );
9328
Paul Bakker43ca69c2011-01-15 17:35:19 +00009329 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009330 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009331 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009332}
9333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009334int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009335{
Hanno Becker3136ede2018-08-17 15:28:19 +01009336 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009337 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009338 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009339
Hanno Becker5903de42019-05-03 14:46:38 +01009340 size_t out_hdr_len = mbedtls_ssl_out_hdr_len( ssl );
9341
Hanno Becker78640902018-08-13 16:35:15 +01009342 if( transform == NULL )
Hanno Becker5903de42019-05-03 14:46:38 +01009343 return( (int) out_hdr_len );
Hanno Becker78640902018-08-13 16:35:15 +01009344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009345#if defined(MBEDTLS_ZLIB_SUPPORT)
9346 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9347 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009348#endif
9349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009350 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009351 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009352 case MBEDTLS_MODE_GCM:
9353 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009354 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009355 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009356 transform_expansion = transform->minlen;
9357 break;
9358
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009359 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009360
9361 block_size = mbedtls_cipher_get_block_size(
9362 &transform->cipher_ctx_enc );
9363
Hanno Becker3136ede2018-08-17 15:28:19 +01009364 /* Expansion due to the addition of the MAC. */
9365 transform_expansion += transform->maclen;
9366
9367 /* Expansion due to the addition of CBC padding;
9368 * Theoretically up to 256 bytes, but we never use
9369 * more than the block size of the underlying cipher. */
9370 transform_expansion += block_size;
9371
9372 /* For TLS 1.1 or higher, an explicit IV is added
9373 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009374#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9375 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009376 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009377#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009378
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009379 break;
9380
9381 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009382 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009383 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009384 }
9385
Hanno Beckera0e20d02019-05-15 14:03:01 +01009386#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Becker6cbad552019-05-08 15:40:11 +01009387 if( transform->out_cid_len != 0 )
9388 transform_expansion += MBEDTLS_SSL_MAX_CID_EXPANSION;
Hanno Beckera0e20d02019-05-15 14:03:01 +01009389#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Becker6cbad552019-05-08 15:40:11 +01009390
Hanno Becker5903de42019-05-03 14:46:38 +01009391 return( (int)( out_hdr_len + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009392}
9393
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009394#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9395size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9396{
9397 size_t max_len;
9398
9399 /*
9400 * Assume mfl_code is correct since it was checked when set
9401 */
Angus Grattond8213d02016-05-25 20:56:48 +10009402 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009403
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009404 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009405 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009406 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009407 {
Angus Grattond8213d02016-05-25 20:56:48 +10009408 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009409 }
9410
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009411 /* During a handshake, use the value being negotiated */
9412 if( ssl->session_negotiate != NULL &&
9413 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9414 {
9415 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9416 }
9417
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009418 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009419}
9420#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9421
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009422#if defined(MBEDTLS_SSL_PROTO_DTLS)
9423static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9424{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009425 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9426 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9427 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9428 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9429 return ( 0 );
9430
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009431 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9432 return( ssl->mtu );
9433
9434 if( ssl->mtu == 0 )
9435 return( ssl->handshake->mtu );
9436
9437 return( ssl->mtu < ssl->handshake->mtu ?
9438 ssl->mtu : ssl->handshake->mtu );
9439}
9440#endif /* MBEDTLS_SSL_PROTO_DTLS */
9441
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009442int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9443{
9444 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9445
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009446#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9447 !defined(MBEDTLS_SSL_PROTO_DTLS)
9448 (void) ssl;
9449#endif
9450
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009451#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9452 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9453
9454 if( max_len > mfl )
9455 max_len = mfl;
9456#endif
9457
9458#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009459 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009460 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009461 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009462 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9463 const size_t overhead = (size_t) ret;
9464
9465 if( ret < 0 )
9466 return( ret );
9467
9468 if( mtu <= overhead )
9469 {
9470 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9471 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9472 }
9473
9474 if( max_len > mtu - overhead )
9475 max_len = mtu - overhead;
9476 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009477#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009478
Hanno Becker0defedb2018-08-10 12:35:02 +01009479#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9480 !defined(MBEDTLS_SSL_PROTO_DTLS)
9481 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009482#endif
9483
9484 return( (int) max_len );
9485}
9486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009487#if defined(MBEDTLS_X509_CRT_PARSE_C)
9488const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009489{
9490 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009491 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009492
Hanno Beckere6824572019-02-07 13:18:46 +00009493#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009494 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009495#else
9496 return( NULL );
9497#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009498}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009499#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009501#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009502int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9503 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009504{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009505 if( ssl == NULL ||
9506 dst == NULL ||
9507 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009508 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009509 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009510 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009511 }
9512
Hanno Becker52055ae2019-02-06 14:30:46 +00009513 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009514}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009515#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009516
Paul Bakker5121ce52009-01-03 21:22:43 +00009517/*
Paul Bakker1961b702013-01-25 14:49:24 +01009518 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009519 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009520int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009521{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009522 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009523
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009524 if( ssl == NULL || ssl->conf == NULL )
9525 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9526
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009527#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009528 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009529 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009530#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009532 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009533 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009534#endif
9535
Paul Bakker1961b702013-01-25 14:49:24 +01009536 return( ret );
9537}
9538
9539/*
9540 * Perform the SSL handshake
9541 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009542int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009543{
9544 int ret = 0;
9545
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009546 if( ssl == NULL || ssl->conf == NULL )
9547 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009549 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009553 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009554
9555 if( ret != 0 )
9556 break;
9557 }
9558
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009559 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009560
9561 return( ret );
9562}
9563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009564#if defined(MBEDTLS_SSL_RENEGOTIATION)
9565#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009566/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009567 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009568 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009569static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009570{
9571 int ret;
9572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009574
9575 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009576 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9577 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009578
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009579 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009580 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009581 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009582 return( ret );
9583 }
9584
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009585 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009586
9587 return( 0 );
9588}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009589#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009590
9591/*
9592 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009593 * - any side: calling mbedtls_ssl_renegotiate(),
9594 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9595 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009596 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009597 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009598 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009599 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009600static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009601{
9602 int ret;
9603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009605
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009606 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9607 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009608
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009609 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9610 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009611#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009612 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009613 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009614 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009615 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009616 ssl->handshake->out_msg_seq = 1;
9617 else
9618 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009619 }
9620#endif
9621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009622 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9623 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009625 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009627 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009628 return( ret );
9629 }
9630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009632
9633 return( 0 );
9634}
9635
9636/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009637 * Renegotiate current connection on client,
9638 * or request renegotiation on server
9639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009640int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009641{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009642 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009643
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009644 if( ssl == NULL || ssl->conf == NULL )
9645 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009647#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009648 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009649 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009650 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009651 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9652 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009654 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009655
9656 /* Did we already try/start sending HelloRequest? */
9657 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009658 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009659
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009660 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009661 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009662#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009663
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009664#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009665 /*
9666 * On client, either start the renegotiation process or,
9667 * if already in progress, continue the handshake
9668 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009669 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009670 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009671 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9672 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009673
9674 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9675 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009676 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009677 return( ret );
9678 }
9679 }
9680 else
9681 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009682 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009683 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009684 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009685 return( ret );
9686 }
9687 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009688#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009689
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009690 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009691}
9692
9693/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009694 * Check record counters and renegotiate if they're above the limit.
9695 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009697{
Andres AG2196c7f2016-12-15 17:01:16 +00009698 size_t ep_len = ssl_ep_len( ssl );
9699 int in_ctr_cmp;
9700 int out_ctr_cmp;
9701
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009702 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9703 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009704 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009705 {
9706 return( 0 );
9707 }
9708
Andres AG2196c7f2016-12-15 17:01:16 +00009709 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9710 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009711 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009712 ssl->conf->renego_period + ep_len, 8 - ep_len );
9713
9714 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009715 {
9716 return( 0 );
9717 }
9718
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009719 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009720 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009721}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009722#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009723
9724/*
9725 * Receive application data decrypted from the SSL layer
9726 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009727int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009728{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009729 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009730 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009731
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009732 if( ssl == NULL || ssl->conf == NULL )
9733 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9734
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009735 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009737#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009738 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009739 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009740 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009741 return( ret );
9742
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009743 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009744 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009745 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009746 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009747 return( ret );
9748 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009749 }
9750#endif
9751
Hanno Becker4a810fb2017-05-24 16:27:30 +01009752 /*
9753 * Check if renegotiation is necessary and/or handshake is
9754 * in process. If yes, perform/continue, and fall through
9755 * if an unexpected packet is received while the client
9756 * is waiting for the ServerHello.
9757 *
9758 * (There is no equivalent to the last condition on
9759 * the server-side as it is not treated as within
9760 * a handshake while waiting for the ClientHello
9761 * after a renegotiation request.)
9762 */
9763
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009764#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009765 ret = ssl_check_ctr_renegotiate( ssl );
9766 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9767 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009768 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009769 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009770 return( ret );
9771 }
9772#endif
9773
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009774 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009776 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009777 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9778 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009779 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009780 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009781 return( ret );
9782 }
9783 }
9784
Hanno Beckere41158b2017-10-23 13:30:32 +01009785 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009786 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009787 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009788 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009789 if( ssl->f_get_timer != NULL &&
9790 ssl->f_get_timer( ssl->p_timer ) == -1 )
9791 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009792 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009793 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009794
Hanno Becker327c93b2018-08-15 13:56:18 +01009795 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009796 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009797 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9798 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009799
Hanno Becker4a810fb2017-05-24 16:27:30 +01009800 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9801 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009802 }
9803
9804 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009805 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009806 {
9807 /*
9808 * OpenSSL sends empty messages to randomize the IV
9809 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009810 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009811 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009812 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009813 return( 0 );
9814
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009815 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009816 return( ret );
9817 }
9818 }
9819
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009821 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009823
Hanno Becker4a810fb2017-05-24 16:27:30 +01009824 /*
9825 * - For client-side, expect SERVER_HELLO_REQUEST.
9826 * - For server-side, expect CLIENT_HELLO.
9827 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9828 */
9829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009830#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009831 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009832 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009833 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009834 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009835 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009836
9837 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009838#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009839 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009840 {
9841 continue;
9842 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009843#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009844 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009845 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009846#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009847
Hanno Becker4a810fb2017-05-24 16:27:30 +01009848#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009849 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009850 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009853
9854 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009856 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009857 {
9858 continue;
9859 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009860#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009861 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009862 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009863#endif /* MBEDTLS_SSL_SRV_C */
9864
Hanno Becker21df7f92017-10-17 11:03:26 +01009865#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009866 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009867 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9868 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9869 ssl->conf->allow_legacy_renegotiation ==
9870 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9871 {
9872 /*
9873 * Accept renegotiation request
9874 */
Paul Bakker48916f92012-09-16 19:57:18 +00009875
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009876 /* DTLS clients need to know renego is server-initiated */
9877#if defined(MBEDTLS_SSL_PROTO_DTLS)
9878 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9879 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9880 {
9881 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9882 }
9883#endif
9884 ret = ssl_start_renegotiation( ssl );
9885 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9886 ret != 0 )
9887 {
9888 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9889 return( ret );
9890 }
9891 }
9892 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009893#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009894 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009895 /*
9896 * Refuse renegotiation
9897 */
9898
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009899 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009900
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009901#if defined(MBEDTLS_SSL_PROTO_SSL3)
9902 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009903 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009904 /* SSLv3 does not have a "no_renegotiation" warning, so
9905 we send a fatal alert and abort the connection. */
9906 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9907 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9908 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009909 }
9910 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009911#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9912#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9913 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9914 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009915 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009916 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9917 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9918 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009919 {
9920 return( ret );
9921 }
Paul Bakker48916f92012-09-16 19:57:18 +00009922 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009923 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009924#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9925 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009926 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009927 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9928 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009929 }
Paul Bakker48916f92012-09-16 19:57:18 +00009930 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009931
Hanno Becker90333da2017-10-10 11:27:13 +01009932 /* At this point, we don't know whether the renegotiation has been
9933 * completed or not. The cases to consider are the following:
9934 * 1) The renegotiation is complete. In this case, no new record
9935 * has been read yet.
9936 * 2) The renegotiation is incomplete because the client received
9937 * an application data record while awaiting the ServerHello.
9938 * 3) The renegotiation is incomplete because the client received
9939 * a non-handshake, non-application data message while awaiting
9940 * the ServerHello.
9941 * In each of these case, looping will be the proper action:
9942 * - For 1), the next iteration will read a new record and check
9943 * if it's application data.
9944 * - For 2), the loop condition isn't satisfied as application data
9945 * is present, hence continue is the same as break
9946 * - For 3), the loop condition is satisfied and read_record
9947 * will re-deliver the message that was held back by the client
9948 * when expecting the ServerHello.
9949 */
9950 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009951 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009952#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009953 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009954 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009955 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009956 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009957 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009958 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009959 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009960 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009961 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009962 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009963 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009964 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009965#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009966
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009967 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9968 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009969 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009970 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009971 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009972 }
9973
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009974 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009975 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009976 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9977 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009978 }
9979
9980 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009981
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009982 /* We're going to return something now, cancel timer,
9983 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009984 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009985 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009986
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009987#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009988 /* If we requested renego but received AppData, resend HelloRequest.
9989 * Do it now, after setting in_offt, to avoid taking this branch
9990 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009991#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009992 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009994 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009995 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009996 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009997 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009998 return( ret );
9999 }
10000 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010001#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +010010002#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +000010003 }
10004
10005 n = ( len < ssl->in_msglen )
10006 ? len : ssl->in_msglen;
10007
10008 memcpy( buf, ssl->in_offt, n );
10009 ssl->in_msglen -= n;
10010
10011 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +010010012 {
10013 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +000010014 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +010010015 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010016 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010017 else
Hanno Becker4a810fb2017-05-24 16:27:30 +010010018 {
Paul Bakker5121ce52009-01-03 21:22:43 +000010019 /* more data available */
10020 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +010010021 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010023 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010024
Paul Bakker23986e52011-04-24 08:57:21 +000010025 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +000010026}
10027
10028/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010029 * Send application data to be encrypted by the SSL layer, taking care of max
10030 * fragment length and buffer size.
10031 *
10032 * According to RFC 5246 Section 6.2.1:
10033 *
10034 * Zero-length fragments of Application data MAY be sent as they are
10035 * potentially useful as a traffic analysis countermeasure.
10036 *
10037 * Therefore, it is possible that the input message length is 0 and the
10038 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +000010039 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010040static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010041 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +000010042{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +020010043 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
10044 const size_t max_len = (size_t) ret;
10045
10046 if( ret < 0 )
10047 {
10048 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
10049 return( ret );
10050 }
10051
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010052 if( len > max_len )
10053 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010054#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010055 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010057 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010058 "maximum fragment length: %d > %d",
10059 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010060 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010061 }
10062 else
10063#endif
10064 len = max_len;
10065 }
Paul Bakker887bd502011-06-08 13:10:54 +000010066
Paul Bakker5121ce52009-01-03 21:22:43 +000010067 if( ssl->out_left != 0 )
10068 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010069 /*
10070 * The user has previously tried to send the data and
10071 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
10072 * written. In this case, we expect the high-level write function
10073 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
10074 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010075 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010076 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010077 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010078 return( ret );
10079 }
10080 }
Paul Bakker887bd502011-06-08 13:10:54 +000010081 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +000010082 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +010010083 /*
10084 * The user is trying to send a message the first time, so we need to
10085 * copy the data into the internal buffers and setup the data structure
10086 * to keep track of partial writes
10087 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010088 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010089 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010090 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +000010091
Hanno Becker67bc7c32018-08-06 11:33:50 +010010092 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +000010093 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010094 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +000010095 return( ret );
10096 }
Paul Bakker5121ce52009-01-03 21:22:43 +000010097 }
10098
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +020010099 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +000010100}
10101
10102/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010103 * Write application data, doing 1/n-1 splitting if necessary.
10104 *
10105 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010106 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +010010107 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010108 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010109#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010110static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010111 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010112{
10113 int ret;
10114
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010115 if( ssl->conf->cbc_record_splitting ==
10116 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +010010117 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010118 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
10119 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
10120 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010121 {
10122 return( ssl_write_real( ssl, buf, len ) );
10123 }
10124
10125 if( ssl->split_done == 0 )
10126 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010127 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010128 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010129 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010130 }
10131
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +010010132 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
10133 return( ret );
10134 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010135
10136 return( ret + 1 );
10137}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010138#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +010010139
10140/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010141 * Write application data (public-facing wrapper)
10142 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010143int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010144{
10145 int ret;
10146
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010147 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010148
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010149 if( ssl == NULL || ssl->conf == NULL )
10150 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10151
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010152#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010153 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10154 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010155 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010156 return( ret );
10157 }
10158#endif
10159
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010160 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010161 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010162 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010163 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010164 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010165 return( ret );
10166 }
10167 }
10168
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010169#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010170 ret = ssl_write_split( ssl, buf, len );
10171#else
10172 ret = ssl_write_real( ssl, buf, len );
10173#endif
10174
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010176
10177 return( ret );
10178}
10179
10180/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010181 * Notify the peer that the connection is being closed
10182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010183int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010184{
10185 int ret;
10186
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010187 if( ssl == NULL || ssl->conf == NULL )
10188 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010190 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010191
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010192 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010193 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010195 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010196 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010197 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10198 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10199 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010200 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010201 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010202 return( ret );
10203 }
10204 }
10205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010206 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010207
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010208 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010209}
10210
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010212{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010213 if( transform == NULL )
10214 return;
10215
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010217 deflateEnd( &transform->ctx_deflate );
10218 inflateEnd( &transform->ctx_inflate );
10219#endif
10220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010221 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10222 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010223
Hanno Beckerd56ed242018-01-03 15:32:51 +000010224#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010225 mbedtls_md_free( &transform->md_ctx_enc );
10226 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010227#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010228
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010229 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010230}
10231
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010232#if defined(MBEDTLS_X509_CRT_PARSE_C)
10233static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010234{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010235 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010236
10237 while( cur != NULL )
10238 {
10239 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010240 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010241 cur = next;
10242 }
10243}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010244#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010245
Hanno Becker0271f962018-08-16 13:23:47 +010010246#if defined(MBEDTLS_SSL_PROTO_DTLS)
10247
10248static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10249{
10250 unsigned offset;
10251 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10252
10253 if( hs == NULL )
10254 return;
10255
Hanno Becker283f5ef2018-08-24 09:34:47 +010010256 ssl_free_buffered_record( ssl );
10257
Hanno Becker0271f962018-08-16 13:23:47 +010010258 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010259 ssl_buffering_free_slot( ssl, offset );
10260}
10261
10262static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10263 uint8_t slot )
10264{
10265 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10266 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010267
10268 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10269 return;
10270
Hanno Beckere605b192018-08-21 15:59:07 +010010271 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010272 {
Hanno Beckere605b192018-08-21 15:59:07 +010010273 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010274 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010275 mbedtls_free( hs_buf->data );
10276 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010277 }
10278}
10279
10280#endif /* MBEDTLS_SSL_PROTO_DTLS */
10281
Gilles Peskine9b562d52018-04-25 20:32:43 +020010282void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010283{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010284 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10285
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010286 if( handshake == NULL )
10287 return;
10288
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010289#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10290 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10291 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010292 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010293 handshake->async_in_progress = 0;
10294 }
10295#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10296
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010297#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10298 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10299 mbedtls_md5_free( &handshake->fin_md5 );
10300 mbedtls_sha1_free( &handshake->fin_sha1 );
10301#endif
10302#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10303#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010304#if defined(MBEDTLS_USE_PSA_CRYPTO)
10305 psa_hash_abort( &handshake->fin_sha256_psa );
10306#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010307 mbedtls_sha256_free( &handshake->fin_sha256 );
10308#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010309#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010310#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010311#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010312 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010313#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010314 mbedtls_sha512_free( &handshake->fin_sha512 );
10315#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010316#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010317#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10318
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010319#if defined(MBEDTLS_DHM_C)
10320 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010321#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010322#if defined(MBEDTLS_ECDH_C)
10323 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010324#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010325#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010326 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010327#if defined(MBEDTLS_SSL_CLI_C)
10328 mbedtls_free( handshake->ecjpake_cache );
10329 handshake->ecjpake_cache = NULL;
10330 handshake->ecjpake_cache_len = 0;
10331#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010332#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010333
Janos Follath4ae5c292016-02-10 11:27:43 +000010334#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10335 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010336 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010338#endif
10339
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010340#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10341 if( handshake->psk != NULL )
10342 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010343 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010344 mbedtls_free( handshake->psk );
10345 }
10346#endif
10347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010348#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10349 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010350 /*
10351 * Free only the linked list wrapper, not the keys themselves
10352 * since the belong to the SNI callback
10353 */
10354 if( handshake->sni_key_cert != NULL )
10355 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010356 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010357
10358 while( cur != NULL )
10359 {
10360 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010361 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010362 cur = next;
10363 }
10364 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010365#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010366
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010367#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010368 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010369 if( handshake->ecrs_peer_cert != NULL )
10370 {
10371 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10372 mbedtls_free( handshake->ecrs_peer_cert );
10373 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010374#endif
10375
Hanno Becker75173122019-02-06 16:18:31 +000010376#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10377 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10378 mbedtls_pk_free( &handshake->peer_pubkey );
10379#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010381#if defined(MBEDTLS_SSL_PROTO_DTLS)
10382 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010383 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010384 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010385#endif
10386
Hanno Becker4a63ed42019-01-08 11:39:35 +000010387#if defined(MBEDTLS_ECDH_C) && \
10388 defined(MBEDTLS_USE_PSA_CRYPTO)
10389 psa_destroy_key( handshake->ecdh_psa_privkey );
10390#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10391
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010392 mbedtls_platform_zeroize( handshake,
10393 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010394}
10395
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010396void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010397{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010398 if( session == NULL )
10399 return;
10400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010402 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010403#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010404
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010405#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010406 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010407#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010408
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010409 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010410}
10411
Paul Bakker5121ce52009-01-03 21:22:43 +000010412/*
10413 * Free an SSL context
10414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010415void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010416{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010417 if( ssl == NULL )
10418 return;
10419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010420 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010421
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010422 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010423 {
Angus Grattond8213d02016-05-25 20:56:48 +100010424 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010425 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010426 }
10427
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010428 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010429 {
Angus Grattond8213d02016-05-25 20:56:48 +100010430 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010431 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010432 }
10433
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010434#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010435 if( ssl->compress_buf != NULL )
10436 {
Angus Grattond8213d02016-05-25 20:56:48 +100010437 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010438 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010439 }
10440#endif
10441
Paul Bakker48916f92012-09-16 19:57:18 +000010442 if( ssl->transform )
10443 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010444 mbedtls_ssl_transform_free( ssl->transform );
10445 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010446 }
10447
10448 if( ssl->handshake )
10449 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010450 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010451 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10452 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010453
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010454 mbedtls_free( ssl->handshake );
10455 mbedtls_free( ssl->transform_negotiate );
10456 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010457 }
10458
Paul Bakkerc0463502013-02-14 11:19:38 +010010459 if( ssl->session )
10460 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010461 mbedtls_ssl_session_free( ssl->session );
10462 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010463 }
10464
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010465#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010466 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010467 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010468 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010469 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010470 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010471#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010473#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10474 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010475 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010476 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10477 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010478 }
10479#endif
10480
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010481#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010482 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010483#endif
10484
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010485 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010486
Paul Bakker86f04f42013-02-14 11:20:09 +010010487 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010488 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010489}
10490
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010491/*
10492 * Initialze mbedtls_ssl_config
10493 */
10494void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10495{
10496 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10497}
10498
Simon Butcherc97b6972015-12-27 23:48:17 +000010499#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010500static int ssl_preset_default_hashes[] = {
10501#if defined(MBEDTLS_SHA512_C)
10502 MBEDTLS_MD_SHA512,
10503 MBEDTLS_MD_SHA384,
10504#endif
10505#if defined(MBEDTLS_SHA256_C)
10506 MBEDTLS_MD_SHA256,
10507 MBEDTLS_MD_SHA224,
10508#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010509#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010510 MBEDTLS_MD_SHA1,
10511#endif
10512 MBEDTLS_MD_NONE
10513};
Simon Butcherc97b6972015-12-27 23:48:17 +000010514#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010515
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010516static int ssl_preset_suiteb_ciphersuites[] = {
10517 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10518 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10519 0
10520};
10521
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010522#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010523static int ssl_preset_suiteb_hashes[] = {
10524 MBEDTLS_MD_SHA256,
10525 MBEDTLS_MD_SHA384,
10526 MBEDTLS_MD_NONE
10527};
10528#endif
10529
10530#if defined(MBEDTLS_ECP_C)
10531static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +010010532#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010533 MBEDTLS_ECP_DP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010534#endif
10535#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010536 MBEDTLS_ECP_DP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +010010537#endif
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010538 MBEDTLS_ECP_DP_NONE
10539};
10540#endif
10541
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010542/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010543 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010544 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010545int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010546 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010547{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010548#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010549 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010550#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010551
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010552 /* Use the functions here so that they are covered in tests,
10553 * but otherwise access member directly for efficiency */
10554 mbedtls_ssl_conf_endpoint( conf, endpoint );
10555 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010556
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010557 /*
10558 * Things that are common to all presets
10559 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010560#if defined(MBEDTLS_SSL_CLI_C)
10561 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10562 {
10563 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10564#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10565 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10566#endif
10567 }
10568#endif
10569
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010570#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010571 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010572#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010573
10574#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10575 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10576#endif
10577
10578#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10579 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10580#endif
10581
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010582#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10583 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10584#endif
10585
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010586#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010587 conf->f_cookie_write = ssl_cookie_write_dummy;
10588 conf->f_cookie_check = ssl_cookie_check_dummy;
10589#endif
10590
10591#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10592 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10593#endif
10594
Janos Follath088ce432017-04-10 12:42:31 +010010595#if defined(MBEDTLS_SSL_SRV_C)
10596 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10597#endif
10598
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010599#if defined(MBEDTLS_SSL_PROTO_DTLS)
10600 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10601 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10602#endif
10603
10604#if defined(MBEDTLS_SSL_RENEGOTIATION)
10605 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010606 memset( conf->renego_period, 0x00, 2 );
10607 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010608#endif
10609
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010610#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10611 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10612 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010613 const unsigned char dhm_p[] =
10614 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10615 const unsigned char dhm_g[] =
10616 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10617
Hanno Beckera90658f2017-10-04 15:29:08 +010010618 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10619 dhm_p, sizeof( dhm_p ),
10620 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010621 {
10622 return( ret );
10623 }
10624 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010625#endif
10626
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010627 /*
10628 * Preset-specific defaults
10629 */
10630 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010631 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010632 /*
10633 * NSA Suite B
10634 */
10635 case MBEDTLS_SSL_PRESET_SUITEB:
10636 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10637 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10638 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10639 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10640
10641 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10642 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10643 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10644 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10645 ssl_preset_suiteb_ciphersuites;
10646
10647#if defined(MBEDTLS_X509_CRT_PARSE_C)
10648 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010649#endif
10650
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010651#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010652 conf->sig_hashes = ssl_preset_suiteb_hashes;
10653#endif
10654
10655#if defined(MBEDTLS_ECP_C)
10656 conf->curve_list = ssl_preset_suiteb_curves;
10657#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010658 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010659
10660 /*
10661 * Default
10662 */
10663 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010664 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10665 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10666 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10667 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10668 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10669 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10670 MBEDTLS_SSL_MIN_MINOR_VERSION :
10671 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010672 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10673 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10674
10675#if defined(MBEDTLS_SSL_PROTO_DTLS)
10676 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10677 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10678#endif
10679
10680 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10681 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10682 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10683 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10684 mbedtls_ssl_list_ciphersuites();
10685
10686#if defined(MBEDTLS_X509_CRT_PARSE_C)
10687 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10688#endif
10689
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010690#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010691 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010692#endif
10693
10694#if defined(MBEDTLS_ECP_C)
10695 conf->curve_list = mbedtls_ecp_grp_id_list();
10696#endif
10697
10698#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10699 conf->dhm_min_bitlen = 1024;
10700#endif
10701 }
10702
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010703 return( 0 );
10704}
10705
10706/*
10707 * Free mbedtls_ssl_config
10708 */
10709void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10710{
10711#if defined(MBEDTLS_DHM_C)
10712 mbedtls_mpi_free( &conf->dhm_P );
10713 mbedtls_mpi_free( &conf->dhm_G );
10714#endif
10715
10716#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10717 if( conf->psk != NULL )
10718 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010719 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010720 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010721 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010722 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010723 }
10724
10725 if( conf->psk_identity != NULL )
10726 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010727 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010728 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010729 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010730 conf->psk_identity_len = 0;
10731 }
10732#endif
10733
10734#if defined(MBEDTLS_X509_CRT_PARSE_C)
10735 ssl_key_cert_free( conf->key_cert );
10736#endif
10737
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010738 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010739}
10740
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010741#if defined(MBEDTLS_PK_C) && \
10742 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010743/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010744 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010745 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010746unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010747{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010748#if defined(MBEDTLS_RSA_C)
10749 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10750 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010751#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010752#if defined(MBEDTLS_ECDSA_C)
10753 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10754 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010755#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010756 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010757}
10758
Hanno Becker7e5437a2017-04-28 17:15:26 +010010759unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10760{
10761 switch( type ) {
10762 case MBEDTLS_PK_RSA:
10763 return( MBEDTLS_SSL_SIG_RSA );
10764 case MBEDTLS_PK_ECDSA:
10765 case MBEDTLS_PK_ECKEY:
10766 return( MBEDTLS_SSL_SIG_ECDSA );
10767 default:
10768 return( MBEDTLS_SSL_SIG_ANON );
10769 }
10770}
10771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010772mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010773{
10774 switch( sig )
10775 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010776#if defined(MBEDTLS_RSA_C)
10777 case MBEDTLS_SSL_SIG_RSA:
10778 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010779#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010780#if defined(MBEDTLS_ECDSA_C)
10781 case MBEDTLS_SSL_SIG_ECDSA:
10782 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010783#endif
10784 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010785 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010786 }
10787}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010788#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010789
Hanno Becker7e5437a2017-04-28 17:15:26 +010010790#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10791 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10792
10793/* Find an entry in a signature-hash set matching a given hash algorithm. */
10794mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10795 mbedtls_pk_type_t sig_alg )
10796{
10797 switch( sig_alg )
10798 {
10799 case MBEDTLS_PK_RSA:
10800 return( set->rsa );
10801 case MBEDTLS_PK_ECDSA:
10802 return( set->ecdsa );
10803 default:
10804 return( MBEDTLS_MD_NONE );
10805 }
10806}
10807
10808/* Add a signature-hash-pair to a signature-hash set */
10809void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10810 mbedtls_pk_type_t sig_alg,
10811 mbedtls_md_type_t md_alg )
10812{
10813 switch( sig_alg )
10814 {
10815 case MBEDTLS_PK_RSA:
10816 if( set->rsa == MBEDTLS_MD_NONE )
10817 set->rsa = md_alg;
10818 break;
10819
10820 case MBEDTLS_PK_ECDSA:
10821 if( set->ecdsa == MBEDTLS_MD_NONE )
10822 set->ecdsa = md_alg;
10823 break;
10824
10825 default:
10826 break;
10827 }
10828}
10829
10830/* Allow exactly one hash algorithm for each signature. */
10831void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10832 mbedtls_md_type_t md_alg )
10833{
10834 set->rsa = md_alg;
10835 set->ecdsa = md_alg;
10836}
10837
10838#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10839 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10840
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010841/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010842 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010843 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010844mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010845{
10846 switch( hash )
10847 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010848#if defined(MBEDTLS_MD5_C)
10849 case MBEDTLS_SSL_HASH_MD5:
10850 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852#if defined(MBEDTLS_SHA1_C)
10853 case MBEDTLS_SSL_HASH_SHA1:
10854 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010855#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856#if defined(MBEDTLS_SHA256_C)
10857 case MBEDTLS_SSL_HASH_SHA224:
10858 return( MBEDTLS_MD_SHA224 );
10859 case MBEDTLS_SSL_HASH_SHA256:
10860 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010861#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010862#if defined(MBEDTLS_SHA512_C)
10863 case MBEDTLS_SSL_HASH_SHA384:
10864 return( MBEDTLS_MD_SHA384 );
10865 case MBEDTLS_SSL_HASH_SHA512:
10866 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010867#endif
10868 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010869 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010870 }
10871}
10872
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010873/*
10874 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10875 */
10876unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10877{
10878 switch( md )
10879 {
10880#if defined(MBEDTLS_MD5_C)
10881 case MBEDTLS_MD_MD5:
10882 return( MBEDTLS_SSL_HASH_MD5 );
10883#endif
10884#if defined(MBEDTLS_SHA1_C)
10885 case MBEDTLS_MD_SHA1:
10886 return( MBEDTLS_SSL_HASH_SHA1 );
10887#endif
10888#if defined(MBEDTLS_SHA256_C)
10889 case MBEDTLS_MD_SHA224:
10890 return( MBEDTLS_SSL_HASH_SHA224 );
10891 case MBEDTLS_MD_SHA256:
10892 return( MBEDTLS_SSL_HASH_SHA256 );
10893#endif
10894#if defined(MBEDTLS_SHA512_C)
10895 case MBEDTLS_MD_SHA384:
10896 return( MBEDTLS_SSL_HASH_SHA384 );
10897 case MBEDTLS_MD_SHA512:
10898 return( MBEDTLS_SSL_HASH_SHA512 );
10899#endif
10900 default:
10901 return( MBEDTLS_SSL_HASH_NONE );
10902 }
10903}
10904
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010905#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010906/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010907 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010908 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010909 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010910int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010911{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010912 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010913
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010914 if( ssl->conf->curve_list == NULL )
10915 return( -1 );
10916
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010917 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010918 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010919 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010920
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010921 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010922}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010923#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010924
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010925#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010926/*
10927 * Check if a hash proposed by the peer is in our list.
10928 * Return 0 if we're willing to use it, -1 otherwise.
10929 */
10930int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10931 mbedtls_md_type_t md )
10932{
10933 const int *cur;
10934
10935 if( ssl->conf->sig_hashes == NULL )
10936 return( -1 );
10937
10938 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10939 if( *cur == (int) md )
10940 return( 0 );
10941
10942 return( -1 );
10943}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010944#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010946#if defined(MBEDTLS_X509_CRT_PARSE_C)
10947int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10948 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010949 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010950 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010951{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010952 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010953#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010954 int usage = 0;
10955#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010956#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010957 const char *ext_oid;
10958 size_t ext_len;
10959#endif
10960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010961#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10962 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010963 ((void) cert);
10964 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010965 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010966#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010968#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10969 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010970 {
10971 /* Server part of the key exchange */
10972 switch( ciphersuite->key_exchange )
10973 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010974 case MBEDTLS_KEY_EXCHANGE_RSA:
10975 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010976 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010977 break;
10978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010979 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10980 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10981 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10982 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010983 break;
10984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010985 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10986 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010987 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010988 break;
10989
10990 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010991 case MBEDTLS_KEY_EXCHANGE_NONE:
10992 case MBEDTLS_KEY_EXCHANGE_PSK:
10993 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10994 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010995 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010996 usage = 0;
10997 }
10998 }
10999 else
11000 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011001 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
11002 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011003 }
11004
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011005 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011006 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011007 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011008 ret = -1;
11009 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011010#else
11011 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011012#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011014#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
11015 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011017 ext_oid = MBEDTLS_OID_SERVER_AUTH;
11018 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011019 }
11020 else
11021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011022 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
11023 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011024 }
11025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011026 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011027 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010011028 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011029 ret = -1;
11030 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011031#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020011032
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010011033 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020011034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011035#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020011036
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011037/*
11038 * Convert version numbers to/from wire format
11039 * and, for DTLS, to/from TLS equivalent.
11040 *
11041 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080011042 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011043 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
11044 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
11045 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011046void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011047 unsigned char ver[2] )
11048{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011049#if defined(MBEDTLS_SSL_PROTO_DTLS)
11050 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011052 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011053 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11054
11055 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
11056 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
11057 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011058 else
11059#else
11060 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011061#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011062 {
11063 ver[0] = (unsigned char) major;
11064 ver[1] = (unsigned char) minor;
11065 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011066}
11067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011068void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011069 const unsigned char ver[2] )
11070{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011071#if defined(MBEDTLS_SSL_PROTO_DTLS)
11072 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011073 {
11074 *major = 255 - ver[0] + 2;
11075 *minor = 255 - ver[1] + 1;
11076
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011077 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011078 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
11079 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011080 else
11081#else
11082 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011083#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010011084 {
11085 *major = ver[0];
11086 *minor = ver[1];
11087 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010011088}
11089
Simon Butcher99000142016-10-13 17:21:01 +010011090int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
11091{
11092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
11093 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
11094 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11095
11096 switch( md )
11097 {
11098#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
11099#if defined(MBEDTLS_MD5_C)
11100 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010011101 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010011102#endif
11103#if defined(MBEDTLS_SHA1_C)
11104 case MBEDTLS_SSL_HASH_SHA1:
11105 ssl->handshake->calc_verify = ssl_calc_verify_tls;
11106 break;
11107#endif
11108#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
11109#if defined(MBEDTLS_SHA512_C)
11110 case MBEDTLS_SSL_HASH_SHA384:
11111 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
11112 break;
11113#endif
11114#if defined(MBEDTLS_SHA256_C)
11115 case MBEDTLS_SSL_HASH_SHA256:
11116 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
11117 break;
11118#endif
11119 default:
11120 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11121 }
11122
11123 return 0;
11124#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
11125 (void) ssl;
11126 (void) md;
11127
11128 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
11129#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
11130}
11131
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011132#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
11133 defined(MBEDTLS_SSL_PROTO_TLS1_1)
11134int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
11135 unsigned char *output,
11136 unsigned char *data, size_t data_len )
11137{
11138 int ret = 0;
11139 mbedtls_md5_context mbedtls_md5;
11140 mbedtls_sha1_context mbedtls_sha1;
11141
11142 mbedtls_md5_init( &mbedtls_md5 );
11143 mbedtls_sha1_init( &mbedtls_sha1 );
11144
11145 /*
11146 * digitally-signed struct {
11147 * opaque md5_hash[16];
11148 * opaque sha_hash[20];
11149 * };
11150 *
11151 * md5_hash
11152 * MD5(ClientHello.random + ServerHello.random
11153 * + ServerParams);
11154 * sha_hash
11155 * SHA(ClientHello.random + ServerHello.random
11156 * + ServerParams);
11157 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011158 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011159 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011160 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011161 goto exit;
11162 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011163 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011164 ssl->handshake->randbytes, 64 ) ) != 0 )
11165 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011166 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011167 goto exit;
11168 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011169 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011170 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011171 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011172 goto exit;
11173 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011174 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011175 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011176 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 goto exit;
11178 }
11179
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011180 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011181 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011182 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011183 goto exit;
11184 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011185 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011186 ssl->handshake->randbytes, 64 ) ) != 0 )
11187 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011188 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011189 goto exit;
11190 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011191 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011192 data_len ) ) != 0 )
11193 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011194 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011195 goto exit;
11196 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011197 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011198 output + 16 ) ) != 0 )
11199 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011200 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011201 goto exit;
11202 }
11203
11204exit:
11205 mbedtls_md5_free( &mbedtls_md5 );
11206 mbedtls_sha1_free( &mbedtls_sha1 );
11207
11208 if( ret != 0 )
11209 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11210 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11211
11212 return( ret );
11213
11214}
11215#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11216 MBEDTLS_SSL_PROTO_TLS1_1 */
11217
11218#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11219 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011220
11221#if defined(MBEDTLS_USE_PSA_CRYPTO)
11222int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11223 unsigned char *hash, size_t *hashlen,
11224 unsigned char *data, size_t data_len,
11225 mbedtls_md_type_t md_alg )
11226{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011227 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011228 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011229 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11230
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011231 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011232
11233 if( ( status = psa_hash_setup( &hash_operation,
11234 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011235 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011236 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011237 goto exit;
11238 }
11239
Andrzej Kurek814feff2019-01-14 04:35:19 -050011240 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11241 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011242 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011243 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011244 goto exit;
11245 }
11246
Andrzej Kurek814feff2019-01-14 04:35:19 -050011247 if( ( status = psa_hash_update( &hash_operation,
11248 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011249 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011250 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011251 goto exit;
11252 }
11253
Andrzej Kurek814feff2019-01-14 04:35:19 -050011254 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11255 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011256 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011257 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011258 goto exit;
11259 }
11260
11261exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011262 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011263 {
11264 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11265 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011266 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011267 {
11268 case PSA_ERROR_NOT_SUPPORTED:
11269 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011270 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011271 case PSA_ERROR_BUFFER_TOO_SMALL:
11272 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11273 case PSA_ERROR_INSUFFICIENT_MEMORY:
11274 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11275 default:
11276 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11277 }
11278 }
11279 return( 0 );
11280}
11281
11282#else
11283
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011284int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011285 unsigned char *hash, size_t *hashlen,
11286 unsigned char *data, size_t data_len,
11287 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011288{
11289 int ret = 0;
11290 mbedtls_md_context_t ctx;
11291 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011292 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011293
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011294 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011295
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011296 mbedtls_md_init( &ctx );
11297
11298 /*
11299 * digitally-signed struct {
11300 * opaque client_random[32];
11301 * opaque server_random[32];
11302 * ServerDHParams params;
11303 * };
11304 */
11305 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11306 {
11307 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11308 goto exit;
11309 }
11310 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11311 {
11312 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11313 goto exit;
11314 }
11315 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11316 {
11317 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11318 goto exit;
11319 }
11320 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11321 {
11322 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11323 goto exit;
11324 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011325 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011326 {
11327 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11328 goto exit;
11329 }
11330
11331exit:
11332 mbedtls_md_free( &ctx );
11333
11334 if( ret != 0 )
11335 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11336 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11337
11338 return( ret );
11339}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011340#endif /* MBEDTLS_USE_PSA_CRYPTO */
11341
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011342#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11343 MBEDTLS_SSL_PROTO_TLS1_2 */
11344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011345#endif /* MBEDTLS_SSL_TLS_C */