blob: 385631a23d09f298b4c461aa959b2432010194cf [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * SSLv3/TLSv1 shared functions
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21/*
22 * The SSL 3.0 specification was drafted by Netscape in 1996,
23 * and became an IETF standard in 1999.
24 *
25 * http://wp.netscape.com/eng/ssl3/
26 * http://www.ietf.org/rfc/rfc2246.txt
27 * http://www.ietf.org/rfc/rfc4346.txt
28 */
29
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020034#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000037
SimonBd5800b72016-04-26 07:43:27 +010038#if defined(MBEDTLS_PLATFORM_C)
39#include "mbedtls/platform.h"
40#else
41#include <stdlib.h>
42#define mbedtls_calloc calloc
43#define mbedtls_free free
SimonBd5800b72016-04-26 07:43:27 +010044#endif
45
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000046#include "mbedtls/debug.h"
47#include "mbedtls/ssl.h"
Manuel Pégourié-Gonnard5e94dde2015-05-26 11:57:05 +020048#include "mbedtls/ssl_internal.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050049#include "mbedtls/platform_util.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020050
Rich Evans00ab4702015-02-06 13:43:58 +000051#include <string.h>
52
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050053#if defined(MBEDTLS_USE_PSA_CRYPTO)
54#include "mbedtls/psa_util.h"
55#include "psa/crypto.h"
56#endif
57
Janos Follath23bdca02016-10-07 14:47:14 +010058#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000059#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020060#endif
61
Andrzej Kurekc929a822019-01-14 03:51:11 -050062#if defined(MBEDTLS_USE_PSA_CRYPTO)
63#include "mbedtls/psa_util.h"
64#endif
65
Hanno Becker2a43f6f2018-08-10 11:12:52 +010066static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl );
Hanno Beckercd9dcda2018-08-28 17:18:56 +010067static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl );
Hanno Becker2a43f6f2018-08-10 11:12:52 +010068
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010069/* Length of the "epoch" field in the record header */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070static inline size_t ssl_ep_len( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020073 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010074 return( 2 );
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010075#else
76 ((void) ssl);
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +010077#endif
78 return( 0 );
79}
80
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020081/*
82 * Start a timer.
83 * Passing millisecs = 0 cancels a running timer.
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020084 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085static void ssl_set_timer( mbedtls_ssl_context *ssl, uint32_t millisecs )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020086{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020087 if( ssl->f_set_timer == NULL )
88 return;
89
90 MBEDTLS_SSL_DEBUG_MSG( 3, ( "set_timer to %d ms", (int) millisecs ) );
91 ssl->f_set_timer( ssl->p_timer, millisecs / 4, millisecs );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020092}
93
94/*
95 * Return -1 is timer is expired, 0 if it isn't.
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097static int ssl_check_timer( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +020098{
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +020099 if( ssl->f_get_timer == NULL )
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +0200100 return( 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +0200101
102 if( ssl->f_get_timer( ssl->p_timer ) == 2 )
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200103 {
104 MBEDTLS_SSL_DEBUG_MSG( 3, ( "timer expired" ) );
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200105 return( -1 );
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200106 }
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200107
108 return( 0 );
109}
Manuel Pégourié-Gonnarddb2858c2014-09-29 14:04:42 +0200110
Hanno Becker5aa4e2c2018-08-06 09:26:08 +0100111static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
112 mbedtls_ssl_transform *transform );
113static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
114 mbedtls_ssl_transform *transform );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100115
116#define SSL_DONT_FORCE_FLUSH 0
117#define SSL_FORCE_FLUSH 1
118
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +0200119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +0100120
Hanno Becker35c36a62019-04-23 12:31:42 +0100121#if defined(MBEDTLS_SSL_CID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100122/* Top-level Connection ID API */
123
Hanno Beckerca092242019-04-25 16:01:49 +0100124/* WARNING: The CID feature isn't fully implemented yet
125 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100126int mbedtls_ssl_set_cid( mbedtls_ssl_context *ssl,
127 int enable,
128 unsigned char const *own_cid,
129 size_t own_cid_len )
130{
Hanno Beckerca092242019-04-25 16:01:49 +0100131 ssl->negotiate_cid = enable;
132 if( enable == MBEDTLS_SSL_CID_DISABLED )
133 {
134 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Disable use of CID extension." ) );
135 return( 0 );
136 }
137 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Enable use of CID extension." ) );
138
139 if( own_cid_len > MBEDTLS_SSL_CID_IN_LEN_MAX )
140 {
141 MBEDTLS_SSL_DEBUG_MSG( 3, ( "CID too large: Maximum %u, actual %u",
142 (unsigned) MBEDTLS_SSL_CID_IN_LEN_MAX,
143 (unsigned) own_cid_len ) );
144 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
145 }
146
147 memcpy( ssl->own_cid, own_cid, own_cid_len );
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100148 /* Truncation is not an issue here because
149 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
150 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100151
152 MBEDTLS_SSL_DEBUG_BUF( 3, "Own CID", own_cid, own_cid_len );
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100153 return( 0 );
154}
155
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100156/* WARNING: The CID feature isn't fully implemented yet
157 * and will not be used. */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100158int mbedtls_ssl_get_peer_cid( mbedtls_ssl_context *ssl,
159 int *enabled,
160 unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ],
161 size_t *peer_cid_len )
162{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100163 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100164
165 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
166 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
167
Hanno Beckerc5f24222019-05-03 12:54:52 +0100168 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
169 * were used, but client and server requested the empty CID.
170 * This is indistinguishable from not using the CID extension
171 * in the first place. */
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100172 if( ssl->transform_in->in_cid_len == 0 &&
173 ssl->transform_in->out_cid_len == 0 )
174 {
175 return( 0 );
176 }
177
178 *peer_cid_len = ssl->transform_in->out_cid_len;
179 memcpy( peer_cid, ssl->transform_in->out_cid,
180 ssl->transform_in->out_cid_len );
181
182 *enabled = MBEDTLS_SSL_CID_ENABLED;
183
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100184 return( 0 );
185}
Hanno Becker35c36a62019-04-23 12:31:42 +0100186#endif /* MBEDTLS_SSL_CID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100187
Hanno Beckerd5847772018-08-28 10:09:23 +0100188/* Forward declarations for functions related to message buffering. */
189static void ssl_buffering_free( mbedtls_ssl_context *ssl );
190static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
191 uint8_t slot );
192static void ssl_free_buffered_record( mbedtls_ssl_context *ssl );
193static int ssl_load_buffered_message( mbedtls_ssl_context *ssl );
194static int ssl_load_buffered_record( mbedtls_ssl_context *ssl );
195static int ssl_buffer_message( mbedtls_ssl_context *ssl );
196static int ssl_buffer_future_record( mbedtls_ssl_context *ssl );
Hanno Beckeref7afdf2018-08-28 17:16:31 +0100197static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl );
Hanno Beckerd5847772018-08-28 10:09:23 +0100198
Hanno Beckera67dee22018-08-22 10:05:20 +0100199static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl );
Hanno Becker11682cc2018-08-22 14:41:02 +0100200static size_t ssl_get_maximum_datagram_size( mbedtls_ssl_context const *ssl )
Hanno Becker2b1e3542018-08-06 11:19:13 +0100201{
Hanno Becker11682cc2018-08-22 14:41:02 +0100202 size_t mtu = ssl_get_current_mtu( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100203
204 if( mtu != 0 && mtu < MBEDTLS_SSL_OUT_BUFFER_LEN )
Hanno Becker11682cc2018-08-22 14:41:02 +0100205 return( mtu );
Hanno Becker2b1e3542018-08-06 11:19:13 +0100206
207 return( MBEDTLS_SSL_OUT_BUFFER_LEN );
208}
209
Hanno Becker67bc7c32018-08-06 11:33:50 +0100210static int ssl_get_remaining_space_in_datagram( mbedtls_ssl_context const *ssl )
211{
Hanno Becker11682cc2018-08-22 14:41:02 +0100212 size_t const bytes_written = ssl->out_left;
213 size_t const mtu = ssl_get_maximum_datagram_size( ssl );
Hanno Becker67bc7c32018-08-06 11:33:50 +0100214
215 /* Double-check that the write-index hasn't gone
216 * past what we can transmit in a single datagram. */
Hanno Becker11682cc2018-08-22 14:41:02 +0100217 if( bytes_written > mtu )
Hanno Becker67bc7c32018-08-06 11:33:50 +0100218 {
219 /* Should never happen... */
220 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
221 }
222
223 return( (int) ( mtu - bytes_written ) );
224}
225
226static int ssl_get_remaining_payload_in_datagram( mbedtls_ssl_context const *ssl )
227{
228 int ret;
229 size_t remaining, expansion;
Andrzej Kurek748face2018-10-11 07:20:19 -0400230 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100231
232#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
233 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
234
235 if( max_len > mfl )
236 max_len = mfl;
Hanno Beckerf4b010e2018-08-24 10:47:29 +0100237
238 /* By the standard (RFC 6066 Sect. 4), the MFL extension
239 * only limits the maximum record payload size, so in theory
240 * we would be allowed to pack multiple records of payload size
241 * MFL into a single datagram. However, this would mean that there's
242 * no way to explicitly communicate MTU restrictions to the peer.
243 *
244 * The following reduction of max_len makes sure that we never
245 * write datagrams larger than MFL + Record Expansion Overhead.
246 */
247 if( max_len <= ssl->out_left )
248 return( 0 );
249
250 max_len -= ssl->out_left;
Hanno Becker67bc7c32018-08-06 11:33:50 +0100251#endif
252
253 ret = ssl_get_remaining_space_in_datagram( ssl );
254 if( ret < 0 )
255 return( ret );
256 remaining = (size_t) ret;
257
258 ret = mbedtls_ssl_get_record_expansion( ssl );
259 if( ret < 0 )
260 return( ret );
261 expansion = (size_t) ret;
262
263 if( remaining <= expansion )
264 return( 0 );
265
266 remaining -= expansion;
267 if( remaining >= max_len )
268 remaining = max_len;
269
270 return( (int) remaining );
271}
272
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200273/*
274 * Double the retransmit timeout value, within the allowed range,
275 * returning -1 if the maximum value has already been reached.
276 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277static int ssl_double_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200278{
279 uint32_t new_timeout;
280
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200281 if( ssl->handshake->retransmit_timeout >= ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200282 return( -1 );
283
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200284 /* Implement the final paragraph of RFC 6347 section 4.1.1.1
285 * in the following way: after the initial transmission and a first
286 * retransmission, back off to a temporary estimated MTU of 508 bytes.
287 * This value is guaranteed to be deliverable (if not guaranteed to be
288 * delivered) of any compliant IPv4 (and IPv6) network, and should work
289 * on most non-IP stacks too. */
290 if( ssl->handshake->retransmit_timeout != ssl->conf->hs_timeout_min )
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400291 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200292 ssl->handshake->mtu = 508;
Andrzej Kurek6290dae2018-10-05 08:06:01 -0400293 MBEDTLS_SSL_DEBUG_MSG( 2, ( "mtu autoreduction to %d bytes", ssl->handshake->mtu ) );
294 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +0200295
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200296 new_timeout = 2 * ssl->handshake->retransmit_timeout;
297
298 /* Avoid arithmetic overflow and range overflow */
299 if( new_timeout < ssl->handshake->retransmit_timeout ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200300 new_timeout > ssl->conf->hs_timeout_max )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200301 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200302 new_timeout = ssl->conf->hs_timeout_max;
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200303 }
304
305 ssl->handshake->retransmit_timeout = new_timeout;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200307 ssl->handshake->retransmit_timeout ) );
308
309 return( 0 );
310}
311
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312static void ssl_reset_retransmit_timeout( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200313{
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +0200314 ssl->handshake->retransmit_timeout = ssl->conf->hs_timeout_min;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200315 MBEDTLS_SSL_DEBUG_MSG( 3, ( "update timeout value to %d millisecs",
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200316 ssl->handshake->retransmit_timeout ) );
317}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200318#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200319
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200320#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200321/*
322 * Convert max_fragment_length codes to length.
323 * RFC 6066 says:
324 * enum{
325 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
326 * } MaxFragmentLength;
327 * and we add 0 -> extension unused
328 */
Angus Grattond8213d02016-05-25 20:56:48 +1000329static unsigned int ssl_mfl_code_to_length( int mfl )
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200330{
Angus Grattond8213d02016-05-25 20:56:48 +1000331 switch( mfl )
332 {
333 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
334 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
335 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
336 return 512;
337 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
338 return 1024;
339 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
340 return 2048;
341 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
342 return 4096;
343 default:
344 return ( MBEDTLS_TLS_EXT_ADV_CONTENT_LEN );
345 }
346}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200347#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200348
Hanno Becker52055ae2019-02-06 14:30:46 +0000349int mbedtls_ssl_session_copy( mbedtls_ssl_session *dst,
350 const mbedtls_ssl_session *src )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200351{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 mbedtls_ssl_session_free( dst );
353 memcpy( dst, src, sizeof( mbedtls_ssl_session ) );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000356
357#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200358 if( src->peer_cert != NULL )
359 {
Paul Bakker2292d1f2013-09-15 17:06:49 +0200360 int ret;
361
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200362 dst->peer_cert = mbedtls_calloc( 1, sizeof(mbedtls_x509_crt) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200363 if( dst->peer_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200364 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200365
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_x509_crt_init( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200367
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200368 if( ( ret = mbedtls_x509_crt_parse_der( dst->peer_cert, src->peer_cert->raw.p,
Manuel Pégourié-Gonnard4d2a8eb2014-06-13 20:33:27 +0200369 src->peer_cert->raw.len ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200370 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200371 mbedtls_free( dst->peer_cert );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200372 dst->peer_cert = NULL;
373 return( ret );
374 }
375 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000376#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker9198ad12019-02-05 17:00:50 +0000377 if( src->peer_cert_digest != NULL )
378 {
Hanno Becker9198ad12019-02-05 17:00:50 +0000379 dst->peer_cert_digest =
Hanno Beckeraccc5992019-02-25 10:06:59 +0000380 mbedtls_calloc( 1, src->peer_cert_digest_len );
Hanno Becker9198ad12019-02-05 17:00:50 +0000381 if( dst->peer_cert_digest == NULL )
382 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
383
384 memcpy( dst->peer_cert_digest, src->peer_cert_digest,
385 src->peer_cert_digest_len );
386 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000387 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000388 }
389#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200391#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200392
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200393#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200394 if( src->ticket != NULL )
395 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +0200396 dst->ticket = mbedtls_calloc( 1, src->ticket_len );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200397 if( dst->ticket == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +0200398 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200399
400 memcpy( dst->ticket, src->ticket, src->ticket_len );
401 }
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200402#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200403
404 return( 0 );
405}
406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200407#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
408int (*mbedtls_ssl_hw_record_init)( mbedtls_ssl_context *ssl,
Paul Bakker9af723c2014-05-01 13:03:14 +0200409 const unsigned char *key_enc, const unsigned char *key_dec,
410 size_t keylen,
411 const unsigned char *iv_enc, const unsigned char *iv_dec,
412 size_t ivlen,
413 const unsigned char *mac_enc, const unsigned char *mac_dec,
Paul Bakker66d5d072014-06-17 16:39:18 +0200414 size_t maclen ) = NULL;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int (*mbedtls_ssl_hw_record_activate)( mbedtls_ssl_context *ssl, int direction) = NULL;
416int (*mbedtls_ssl_hw_record_reset)( mbedtls_ssl_context *ssl ) = NULL;
417int (*mbedtls_ssl_hw_record_write)( mbedtls_ssl_context *ssl ) = NULL;
418int (*mbedtls_ssl_hw_record_read)( mbedtls_ssl_context *ssl ) = NULL;
419int (*mbedtls_ssl_hw_record_finish)( mbedtls_ssl_context *ssl ) = NULL;
420#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +0000421
Paul Bakker5121ce52009-01-03 21:22:43 +0000422/*
423 * Key material generation
424 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200425#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200426static int ssl3_prf( const unsigned char *secret, size_t slen,
427 const char *label,
428 const unsigned char *random, size_t rlen,
Paul Bakker5f70b252012-09-13 14:23:06 +0000429 unsigned char *dstbuf, size_t dlen )
430{
Andres Amaya Garcia33952502017-07-20 16:29:16 +0100431 int ret = 0;
Paul Bakker5f70b252012-09-13 14:23:06 +0000432 size_t i;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200433 mbedtls_md5_context md5;
434 mbedtls_sha1_context sha1;
Paul Bakker5f70b252012-09-13 14:23:06 +0000435 unsigned char padding[16];
436 unsigned char sha1sum[20];
437 ((void)label);
438
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200439 mbedtls_md5_init( &md5 );
440 mbedtls_sha1_init( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +0200441
Paul Bakker5f70b252012-09-13 14:23:06 +0000442 /*
443 * SSLv3:
444 * block =
445 * MD5( secret + SHA1( 'A' + secret + random ) ) +
446 * MD5( secret + SHA1( 'BB' + secret + random ) ) +
447 * MD5( secret + SHA1( 'CCC' + secret + random ) ) +
448 * ...
449 */
450 for( i = 0; i < dlen / 16; i++ )
451 {
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200452 memset( padding, (unsigned char) ('A' + i), 1 + i );
Paul Bakker5f70b252012-09-13 14:23:06 +0000453
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100454 if( ( ret = mbedtls_sha1_starts_ret( &sha1 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100455 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100456 if( ( ret = mbedtls_sha1_update_ret( &sha1, padding, 1 + i ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100457 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100458 if( ( ret = mbedtls_sha1_update_ret( &sha1, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100459 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100460 if( ( ret = mbedtls_sha1_update_ret( &sha1, random, rlen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100461 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100462 if( ( ret = mbedtls_sha1_finish_ret( &sha1, sha1sum ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100463 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000464
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100465 if( ( ret = mbedtls_md5_starts_ret( &md5 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100466 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100467 if( ( ret = mbedtls_md5_update_ret( &md5, secret, slen ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100468 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100469 if( ( ret = mbedtls_md5_update_ret( &md5, sha1sum, 20 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100470 goto exit;
Gilles Peskine9e4f77c2018-01-22 11:48:08 +0100471 if( ( ret = mbedtls_md5_finish_ret( &md5, dstbuf + i * 16 ) ) != 0 )
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100472 goto exit;
Paul Bakker5f70b252012-09-13 14:23:06 +0000473 }
474
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100475exit:
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +0200476 mbedtls_md5_free( &md5 );
477 mbedtls_sha1_free( &sha1 );
Paul Bakker5f70b252012-09-13 14:23:06 +0000478
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500479 mbedtls_platform_zeroize( padding, sizeof( padding ) );
480 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5f70b252012-09-13 14:23:06 +0000481
Andres Amaya Garcia1a607a12017-06-29 17:09:42 +0100482 return( ret );
Paul Bakker5f70b252012-09-13 14:23:06 +0000483}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +0000485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200487static int tls1_prf( const unsigned char *secret, size_t slen,
488 const char *label,
489 const unsigned char *random, size_t rlen,
Paul Bakker23986e52011-04-24 08:57:21 +0000490 unsigned char *dstbuf, size_t dlen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000491{
Paul Bakker23986e52011-04-24 08:57:21 +0000492 size_t nb, hs;
493 size_t i, j, k;
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200494 const unsigned char *S1, *S2;
Ron Eldor3b350852019-05-07 18:31:49 +0300495 unsigned char *tmp;
496 size_t tmp_len = 0;
Paul Bakker5121ce52009-01-03 21:22:43 +0000497 unsigned char h_i[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 const mbedtls_md_info_t *md_info;
499 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100500 int ret;
501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_md_init( &md_ctx );
Paul Bakker5121ce52009-01-03 21:22:43 +0000503
Ron Eldor3b350852019-05-07 18:31:49 +0300504 tmp_len = 20 + strlen( label ) + rlen;
505 tmp = mbedtls_calloc( 1, tmp_len );
506 if( tmp == NULL )
507 {
508 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
509 goto exit;
510 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000511
512 hs = ( slen + 1 ) / 2;
513 S1 = secret;
514 S2 = secret + slen - hs;
515
516 nb = strlen( label );
517 memcpy( tmp + 20, label, nb );
518 memcpy( tmp + 20 + nb, random, rlen );
519 nb += rlen;
520
521 /*
522 * First compute P_md5(secret,label+random)[0..dlen]
523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200524 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_MD5 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300525 {
526 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
527 goto exit;
528 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100529
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200530 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300531 {
532 goto exit;
533 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_md_hmac_starts( &md_ctx, S1, hs );
536 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
537 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000538
539 for( i = 0; i < dlen; i += 16 )
540 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200541 mbedtls_md_hmac_reset ( &md_ctx );
542 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 + nb );
543 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100544
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 mbedtls_md_hmac_reset ( &md_ctx );
546 mbedtls_md_hmac_update( &md_ctx, 4 + tmp, 16 );
547 mbedtls_md_hmac_finish( &md_ctx, 4 + tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000548
549 k = ( i + 16 > dlen ) ? dlen % 16 : 16;
550
551 for( j = 0; j < k; j++ )
552 dstbuf[i + j] = h_i[j];
553 }
554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200555 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100556
Paul Bakker5121ce52009-01-03 21:22:43 +0000557 /*
558 * XOR out with P_sha1(secret,label+random)[0..dlen]
559 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200560 if( ( md_info = mbedtls_md_info_from_type( MBEDTLS_MD_SHA1 ) ) == NULL )
Ron Eldor3b350852019-05-07 18:31:49 +0300561 {
562 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
563 goto exit;
564 }
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566 if( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300567 {
568 goto exit;
569 }
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100570
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200571 mbedtls_md_hmac_starts( &md_ctx, S2, hs );
572 mbedtls_md_hmac_update( &md_ctx, tmp + 20, nb );
573 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000574
575 for( i = 0; i < dlen; i += 20 )
576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577 mbedtls_md_hmac_reset ( &md_ctx );
578 mbedtls_md_hmac_update( &md_ctx, tmp, 20 + nb );
579 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_md_hmac_reset ( &md_ctx );
582 mbedtls_md_hmac_update( &md_ctx, tmp, 20 );
583 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker5121ce52009-01-03 21:22:43 +0000584
585 k = ( i + 20 > dlen ) ? dlen % 20 : 20;
586
587 for( j = 0; j < k; j++ )
588 dstbuf[i + j] = (unsigned char)( dstbuf[i + j] ^ h_i[j] );
589 }
590
Ron Eldor3b350852019-05-07 18:31:49 +0300591exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200592 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100593
Ron Eldor3b350852019-05-07 18:31:49 +0300594 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500595 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000596
Ron Eldor3b350852019-05-07 18:31:49 +0300597 mbedtls_free( tmp );
598 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000599}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200600#endif /* MBEDTLS_SSL_PROTO_TLS1) || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker5121ce52009-01-03 21:22:43 +0000601
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200602#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekc929a822019-01-14 03:51:11 -0500603#if defined(MBEDTLS_USE_PSA_CRYPTO)
604static int tls_prf_generic( mbedtls_md_type_t md_type,
605 const unsigned char *secret, size_t slen,
606 const char *label,
607 const unsigned char *random, size_t rlen,
608 unsigned char *dstbuf, size_t dlen )
609{
610 psa_status_t status;
611 psa_algorithm_t alg;
612 psa_key_policy_t policy;
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500613 psa_key_handle_t master_slot;
Andrzej Kurekc929a822019-01-14 03:51:11 -0500614 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
615
Andrzej Kurek2f760752019-01-28 08:08:15 -0500616 if( ( status = psa_allocate_key( &master_slot ) ) != PSA_SUCCESS )
Andrzej Kurekac5dc342019-01-23 06:57:34 -0500617 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek2d4faa62019-01-29 03:14:15 -0500618
Andrzej Kurekc929a822019-01-14 03:51:11 -0500619 if( md_type == MBEDTLS_MD_SHA384 )
620 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
621 else
622 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
623
Andrzej Kurek2f760752019-01-28 08:08:15 -0500624 policy = psa_key_policy_init();
Andrzej Kurekc929a822019-01-14 03:51:11 -0500625 psa_key_policy_set_usage( &policy,
626 PSA_KEY_USAGE_DERIVE,
627 alg );
628 status = psa_set_key_policy( master_slot, &policy );
629 if( status != PSA_SUCCESS )
630 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
631
632 status = psa_import_key( master_slot, PSA_KEY_TYPE_DERIVE, secret, slen );
633 if( status != PSA_SUCCESS )
634 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
635
636 status = psa_key_derivation( &generator,
637 master_slot, alg,
638 random, rlen,
639 (unsigned char const *) label,
640 (size_t) strlen( label ),
641 dlen );
642 if( status != PSA_SUCCESS )
643 {
644 psa_generator_abort( &generator );
645 psa_destroy_key( master_slot );
646 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
647 }
648
649 status = psa_generator_read( &generator, dstbuf, dlen );
650 if( status != PSA_SUCCESS )
651 {
652 psa_generator_abort( &generator );
653 psa_destroy_key( master_slot );
654 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
655 }
656
657 status = psa_generator_abort( &generator );
658 if( status != PSA_SUCCESS )
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500659 {
660 psa_destroy_key( master_slot );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500661 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Andrzej Kurek70737ca2019-01-14 05:37:13 -0500662 }
Andrzej Kurekc929a822019-01-14 03:51:11 -0500663
664 status = psa_destroy_key( master_slot );
665 if( status != PSA_SUCCESS )
666 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
667
Andrzej Kurek33171262019-01-15 03:25:18 -0500668 return( 0 );
Andrzej Kurekc929a822019-01-14 03:51:11 -0500669}
670
671#else /* MBEDTLS_USE_PSA_CRYPTO */
672
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200673static int tls_prf_generic( mbedtls_md_type_t md_type,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100674 const unsigned char *secret, size_t slen,
675 const char *label,
676 const unsigned char *random, size_t rlen,
677 unsigned char *dstbuf, size_t dlen )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000678{
679 size_t nb;
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100680 size_t i, j, k, md_len;
Ron Eldor3b350852019-05-07 18:31:49 +0300681 unsigned char *tmp;
682 size_t tmp_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
684 const mbedtls_md_info_t *md_info;
685 mbedtls_md_context_t md_ctx;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100686 int ret;
687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200688 mbedtls_md_init( &md_ctx );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000689
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200690 if( ( md_info = mbedtls_md_info_from_type( md_type ) ) == NULL )
691 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200693 md_len = mbedtls_md_get_size( md_info );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100694
Ron Eldor3b350852019-05-07 18:31:49 +0300695 tmp_len = md_len + strlen( label ) + rlen;
696 tmp = mbedtls_calloc( 1, tmp_len );
697 if( tmp == NULL )
698 {
699 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
700 goto exit;
701 }
Paul Bakker1ef83d62012-04-11 12:09:53 +0000702
703 nb = strlen( label );
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100704 memcpy( tmp + md_len, label, nb );
705 memcpy( tmp + md_len + nb, random, rlen );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000706 nb += rlen;
707
708 /*
709 * Compute P_<hash>(secret, label + random)[0..dlen]
710 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 if ( ( ret = mbedtls_md_setup( &md_ctx, md_info, 1 ) ) != 0 )
Ron Eldor3b350852019-05-07 18:31:49 +0300712 goto exit;
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_md_hmac_starts( &md_ctx, secret, slen );
715 mbedtls_md_hmac_update( &md_ctx, tmp + md_len, nb );
716 mbedtls_md_hmac_finish( &md_ctx, tmp );
Manuel Pégourié-Gonnard7da726b2015-03-24 18:08:19 +0100717
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100718 for( i = 0; i < dlen; i += md_len )
Paul Bakker1ef83d62012-04-11 12:09:53 +0000719 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 mbedtls_md_hmac_reset ( &md_ctx );
721 mbedtls_md_hmac_update( &md_ctx, tmp, md_len + nb );
722 mbedtls_md_hmac_finish( &md_ctx, h_i );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200724 mbedtls_md_hmac_reset ( &md_ctx );
725 mbedtls_md_hmac_update( &md_ctx, tmp, md_len );
726 mbedtls_md_hmac_finish( &md_ctx, tmp );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000727
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100728 k = ( i + md_len > dlen ) ? dlen % md_len : md_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +0000729
730 for( j = 0; j < k; j++ )
731 dstbuf[i + j] = h_i[j];
732 }
733
Ron Eldor3b350852019-05-07 18:31:49 +0300734exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200735 mbedtls_md_free( &md_ctx );
Manuel Pégourié-Gonnardb7fcca32015-03-26 11:41:28 +0100736
Ron Eldor3b350852019-05-07 18:31:49 +0300737 mbedtls_platform_zeroize( tmp, tmp_len );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -0500738 mbedtls_platform_zeroize( h_i, sizeof( h_i ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000739
Ron Eldor3b350852019-05-07 18:31:49 +0300740 mbedtls_free( tmp );
741
742 return( ret );
Paul Bakker1ef83d62012-04-11 12:09:53 +0000743}
Andrzej Kurekc929a822019-01-14 03:51:11 -0500744#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200745#if defined(MBEDTLS_SHA256_C)
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100746static int tls_prf_sha256( const unsigned char *secret, size_t slen,
747 const char *label,
748 const unsigned char *random, size_t rlen,
749 unsigned char *dstbuf, size_t dlen )
750{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200751 return( tls_prf_generic( MBEDTLS_MD_SHA256, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100752 label, random, rlen, dstbuf, dlen ) );
753}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200754#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756#if defined(MBEDTLS_SHA512_C)
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +0200757static int tls_prf_sha384( const unsigned char *secret, size_t slen,
758 const char *label,
759 const unsigned char *random, size_t rlen,
Paul Bakkerca4ab492012-04-18 14:23:57 +0000760 unsigned char *dstbuf, size_t dlen )
761{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200762 return( tls_prf_generic( MBEDTLS_MD_SHA384, secret, slen,
Manuel Pégourié-Gonnard6890c6b2015-03-26 11:11:49 +0100763 label, random, rlen, dstbuf, dlen ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +0000764}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765#endif /* MBEDTLS_SHA512_C */
766#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +0000767
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768static void ssl_update_checksum_start( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200770#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
771 defined(MBEDTLS_SSL_PROTO_TLS1_1)
772static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *, const unsigned char *, size_t );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200773#endif
Paul Bakker380da532012-04-18 16:10:25 +0000774
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775#if defined(MBEDTLS_SSL_PROTO_SSL3)
776static void ssl_calc_verify_ssl( mbedtls_ssl_context *, unsigned char * );
777static void ssl_calc_finished_ssl( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200778#endif
779
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200780#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
781static void ssl_calc_verify_tls( mbedtls_ssl_context *, unsigned char * );
782static void ssl_calc_finished_tls( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200783#endif
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
786#if defined(MBEDTLS_SHA256_C)
787static void ssl_update_checksum_sha256( mbedtls_ssl_context *, const unsigned char *, size_t );
788static void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *,unsigned char * );
789static void ssl_calc_finished_tls_sha256( mbedtls_ssl_context *,unsigned char *, int );
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200790#endif
Paul Bakker769075d2012-11-24 11:26:46 +0100791
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200792#if defined(MBEDTLS_SHA512_C)
793static void ssl_update_checksum_sha384( mbedtls_ssl_context *, const unsigned char *, size_t );
794static void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *, unsigned char * );
795static void ssl_calc_finished_tls_sha384( mbedtls_ssl_context *, unsigned char *, int );
Paul Bakker769075d2012-11-24 11:26:46 +0100796#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200797#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker1ef83d62012-04-11 12:09:53 +0000798
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100799#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED) && \
Hanno Becker7d0a5692018-10-23 15:26:22 +0100800 defined(MBEDTLS_USE_PSA_CRYPTO)
801static int ssl_use_opaque_psk( mbedtls_ssl_context const *ssl )
802{
803 if( ssl->conf->f_psk != NULL )
804 {
805 /* If we've used a callback to select the PSK,
806 * the static configuration is irrelevant. */
807 if( ssl->handshake->psk_opaque != 0 )
808 return( 1 );
809
810 return( 0 );
811 }
812
813 if( ssl->conf->psk_opaque != 0 )
814 return( 1 );
815
816 return( 0 );
817}
818#endif /* MBEDTLS_USE_PSA_CRYPTO &&
Manuel Pégourié-Gonnard45be3d82019-02-18 23:35:14 +0100819 MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
Hanno Becker7d0a5692018-10-23 15:26:22 +0100820
Ron Eldorcf280092019-05-14 20:19:13 +0300821#if defined(MBEDTLS_SSL_EXPORT_KEYS)
822static mbedtls_tls_prf_types tls_prf_get_type( mbedtls_ssl_tls_prf_cb *tls_prf )
823{
824#if defined(MBEDTLS_SSL_PROTO_SSL3)
825 if( tls_prf == ssl3_prf )
826 {
Ron Eldor0810f0b2019-05-15 12:32:32 +0300827 return( MBEDTLS_SSL_TLS_PRF_SSL3 );
Ron Eldorcf280092019-05-14 20:19:13 +0300828 }
829 else
830#endif
831#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
832 if( tls_prf == tls1_prf )
833 {
834 return( MBEDTLS_SSL_TLS_PRF_TLS1 );
835 }
836 else
837#endif
838#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
839#if defined(MBEDTLS_SHA512_C)
840 if( tls_prf == tls_prf_sha384 )
841 {
842 return( MBEDTLS_SSL_TLS_PRF_SHA384 );
843 }
844 else
845#endif
846#if defined(MBEDTLS_SHA256_C)
847 if( tls_prf == tls_prf_sha256 )
848 {
849 return( MBEDTLS_SSL_TLS_PRF_SHA256 );
850 }
851 else
852#endif
853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
854 return( MBEDTLS_SSL_TLS_PRF_NONE );
855}
856#endif /* MBEDTLS_SSL_EXPORT_KEYS */
857
Ron Eldor51d3ab52019-05-12 14:54:30 +0300858int mbedtls_ssl_tls_prf( const mbedtls_tls_prf_types prf,
859 const unsigned char *secret, size_t slen,
860 const char *label,
861 const unsigned char *random, size_t rlen,
862 unsigned char *dstbuf, size_t dlen )
863{
864 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
865
866 switch( prf )
867 {
868#if defined(MBEDTLS_SSL_PROTO_SSL3)
869 case MBEDTLS_SSL_TLS_PRF_SSL3:
870 tls_prf = ssl3_prf;
871 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300872#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300873#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
874 case MBEDTLS_SSL_TLS_PRF_TLS1:
875 tls_prf = tls1_prf;
876 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300877#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
878
879#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300880#if defined(MBEDTLS_SHA512_C)
881 case MBEDTLS_SSL_TLS_PRF_SHA384:
882 tls_prf = tls_prf_sha384;
883 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300884#endif /* MBEDTLS_SHA512_C */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300885#if defined(MBEDTLS_SHA256_C)
886 case MBEDTLS_SSL_TLS_PRF_SHA256:
887 tls_prf = tls_prf_sha256;
888 break;
Ron Eldord2f25f72019-05-15 14:54:22 +0300889#endif /* MBEDTLS_SHA256_C */
890#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ron Eldor51d3ab52019-05-12 14:54:30 +0300891 default:
892 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
893 }
894
895 return( tls_prf( secret, slen, label, random, rlen, dstbuf, dlen ) );
896}
897
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200898int mbedtls_ssl_derive_keys( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +0000899{
Paul Bakkerda02a7f2013-08-31 17:25:14 +0200900 int ret = 0;
Hanno Beckercb1cc802018-11-17 22:27:38 +0000901#if defined(MBEDTLS_USE_PSA_CRYPTO)
902 int psa_fallthrough;
903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +0000904 unsigned char tmp[64];
Paul Bakker5121ce52009-01-03 21:22:43 +0000905 unsigned char keyblk[256];
906 unsigned char *key1;
907 unsigned char *key2;
Paul Bakker68884e32013-01-07 18:20:04 +0100908 unsigned char *mac_enc;
909 unsigned char *mac_dec;
Hanno Becker81c7b182017-11-09 18:39:33 +0000910 size_t mac_key_len;
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200911 size_t iv_copy_len;
Hanno Becker88aaf652017-12-27 08:17:40 +0000912 unsigned keylen;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000913 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200914 const mbedtls_cipher_info_t *cipher_info;
915 const mbedtls_md_info_t *md_info;
Paul Bakker68884e32013-01-07 18:20:04 +0100916
Hanno Beckerf9ed7d52018-11-05 12:45:16 +0000917 /* cf. RFC 5246, Section 8.1:
918 * "The master secret is always exactly 48 bytes in length." */
919 size_t const master_secret_len = 48;
920
Hanno Becker35b23c72018-10-23 12:10:41 +0100921#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
922 unsigned char session_hash[48];
923#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
924
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200925 mbedtls_ssl_session *session = ssl->session_negotiate;
926 mbedtls_ssl_transform *transform = ssl->transform_negotiate;
927 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
Paul Bakker5121ce52009-01-03 21:22:43 +0000928
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200929 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> derive keys" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000930
Hanno Becker9eddaeb2017-12-27 21:37:21 +0000931#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
932 transform->encrypt_then_mac = session->encrypt_then_mac;
933#endif
934 transform->minor_ver = ssl->minor_ver;
Hanno Beckere694c3e2017-12-27 21:34:08 +0000935
936 ciphersuite_info = handshake->ciphersuite_info;
937 cipher_info = mbedtls_cipher_info_from_type( ciphersuite_info->cipher );
Paul Bakker68884e32013-01-07 18:20:04 +0100938 if( cipher_info == NULL )
939 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200940 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cipher info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000941 ciphersuite_info->cipher ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200942 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100943 }
944
Hanno Beckere694c3e2017-12-27 21:34:08 +0000945 md_info = mbedtls_md_info_from_type( ciphersuite_info->mac );
Paul Bakker68884e32013-01-07 18:20:04 +0100946 if( md_info == NULL )
947 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200948 MBEDTLS_SSL_DEBUG_MSG( 1, ( "mbedtls_md info for %d not found",
Hanno Beckere694c3e2017-12-27 21:34:08 +0000949 ciphersuite_info->mac ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200950 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Paul Bakker68884e32013-01-07 18:20:04 +0100951 }
952
Hanno Becker4bf74652019-04-26 16:22:27 +0100953#if defined(MBEDTLS_SSL_CID)
954 /* Copy own and peer's CID if the use of the CID
955 * extension has been negotiated. */
956 if( ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED )
957 {
958 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Copy CIDs into SSL transform" ) );
Hanno Becker8a7f9722019-04-30 13:52:29 +0100959
960 /* Uncomment this once CID-parsing and support for a change
961 * record content type during record decryption are added. */
962 /* transform->in_cid_len = ssl->own_cid_len; */
963 /* transform->out_cid_len = ssl->handshake->peer_cid_len; */
964 /* memcpy( transform->in_cid, ssl->own_cid, ssl->own_cid_len ); */
965 /* memcpy( transform->out_cid, ssl->handshake->peer_cid, */
966 /* ssl->handshake->peer_cid_len ); */
Hanno Becker4bf74652019-04-26 16:22:27 +0100967
968 MBEDTLS_SSL_DEBUG_BUF( 3, "Outgoing CID", transform->out_cid,
969 transform->out_cid_len );
Hanno Becker1c1f0462019-05-03 12:55:51 +0100970 MBEDTLS_SSL_DEBUG_BUF( 3, "Incoming CID", transform->in_cid,
Hanno Becker4bf74652019-04-26 16:22:27 +0100971 transform->in_cid_len );
972 }
973#endif /* MBEDTLS_SSL_CID */
974
Paul Bakker5121ce52009-01-03 21:22:43 +0000975 /*
Paul Bakkerca4ab492012-04-18 14:23:57 +0000976 * Set appropriate PRF function and other SSL / TLS / TLS1.2 functions
Paul Bakker1ef83d62012-04-11 12:09:53 +0000977 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200978#if defined(MBEDTLS_SSL_PROTO_SSL3)
979 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000980 {
Paul Bakker48916f92012-09-16 19:57:18 +0000981 handshake->tls_prf = ssl3_prf;
982 handshake->calc_verify = ssl_calc_verify_ssl;
983 handshake->calc_finished = ssl_calc_finished_ssl;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000984 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200985 else
986#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
988 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +0000989 {
Paul Bakker48916f92012-09-16 19:57:18 +0000990 handshake->tls_prf = tls1_prf;
991 handshake->calc_verify = ssl_calc_verify_tls;
992 handshake->calc_finished = ssl_calc_finished_tls;
Paul Bakkerca4ab492012-04-18 14:23:57 +0000993 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +0200994 else
995#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200996#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
997#if defined(MBEDTLS_SHA512_C)
998 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
Hanno Beckere694c3e2017-12-27 21:34:08 +0000999 ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001000 {
Paul Bakker48916f92012-09-16 19:57:18 +00001001 handshake->tls_prf = tls_prf_sha384;
1002 handshake->calc_verify = ssl_calc_verify_tls_sha384;
1003 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001004 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001005 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001006#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001007#if defined(MBEDTLS_SHA256_C)
1008 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00001009 {
Paul Bakker48916f92012-09-16 19:57:18 +00001010 handshake->tls_prf = tls_prf_sha256;
1011 handshake->calc_verify = ssl_calc_verify_tls_sha256;
1012 handshake->calc_finished = ssl_calc_finished_tls_sha256;
Paul Bakkerca4ab492012-04-18 14:23:57 +00001013 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001014 else
1015#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001016#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02001017 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1019 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02001020 }
Paul Bakker1ef83d62012-04-11 12:09:53 +00001021
1022 /*
Paul Bakker5121ce52009-01-03 21:22:43 +00001023 * SSLv3:
1024 * master =
1025 * MD5( premaster + SHA1( 'A' + premaster + randbytes ) ) +
1026 * MD5( premaster + SHA1( 'BB' + premaster + randbytes ) ) +
1027 * MD5( premaster + SHA1( 'CCC' + premaster + randbytes ) )
Paul Bakkerf7abd422013-04-16 13:15:56 +02001028 *
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001029 * TLSv1+:
Paul Bakker5121ce52009-01-03 21:22:43 +00001030 * master = PRF( premaster, "master secret", randbytes )[0..47]
1031 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001032 if( handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001033 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001034 MBEDTLS_SSL_DEBUG_MSG( 3, ( "no premaster (session resumed)" ) );
1035 }
1036 else
1037 {
1038 /* The label for the KDF used for key expansion.
1039 * This is either "master secret" or "extended master secret"
1040 * depending on whether the Extended Master Secret extension
1041 * is used. */
1042 char const *lbl = "master secret";
1043
1044 /* The salt for the KDF used for key expansion.
1045 * - If the Extended Master Secret extension is not used,
1046 * this is ClientHello.Random + ServerHello.Random
1047 * (see Sect. 8.1 in RFC 5246).
1048 * - If the Extended Master Secret extension is used,
1049 * this is the transcript of the handshake so far.
1050 * (see Sect. 4 in RFC 7627). */
1051 unsigned char const *salt = handshake->randbytes;
1052 size_t salt_len = 64;
1053
1054#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001055 if( ssl->handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001056 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001057 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using extended master secret" ) );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001058
Hanno Becker35b23c72018-10-23 12:10:41 +01001059 lbl = "extended master secret";
1060 salt = session_hash;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001061 ssl->handshake->calc_verify( ssl, session_hash );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1063 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001064 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001065#if defined(MBEDTLS_SHA512_C)
Hanno Beckere694c3e2017-12-27 21:34:08 +00001066 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
1067 {
Hanno Becker35b23c72018-10-23 12:10:41 +01001068 salt_len = 48;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001069 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001070 else
Hanno Becker35b23c72018-10-23 12:10:41 +01001071#endif /* MBEDTLS_SHA512_C */
1072 salt_len = 32;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001073 }
1074 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001075#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker35b23c72018-10-23 12:10:41 +01001076 salt_len = 36;
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001077
Hanno Becker35b23c72018-10-23 12:10:41 +01001078 MBEDTLS_SSL_DEBUG_BUF( 3, "session hash", session_hash, salt_len );
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001079 }
Hanno Becker35b23c72018-10-23 12:10:41 +01001080#endif /* MBEDTLS_SSL_EXTENDED_MS_ENABLED */
1081
Hanno Becker7d0a5692018-10-23 15:26:22 +01001082#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
1083 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1084 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_PSK &&
1085 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 &&
1086 ssl_use_opaque_psk( ssl ) == 1 )
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001087 {
Hanno Becker7d0a5692018-10-23 15:26:22 +01001088 /* Perform PSK-to-MS expansion in a single step. */
1089 psa_status_t status;
1090 psa_algorithm_t alg;
1091 psa_crypto_generator_t generator = PSA_CRYPTO_GENERATOR_INIT;
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05001092 psa_key_handle_t psk;
Hanno Becker7d0a5692018-10-23 15:26:22 +01001093
1094 MBEDTLS_SSL_DEBUG_MSG( 2, ( "perform PSA-based PSK-to-MS expansion" ) );
1095
1096 psk = ssl->conf->psk_opaque;
1097 if( ssl->handshake->psk_opaque != 0 )
1098 psk = ssl->handshake->psk_opaque;
1099
Hanno Becker22bf1452019-04-05 11:21:08 +01001100 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Hanno Becker7d0a5692018-10-23 15:26:22 +01001101 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
1102 else
1103 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
1104
1105 status = psa_key_derivation( &generator, psk, alg,
1106 salt, salt_len,
1107 (unsigned char const *) lbl,
1108 (size_t) strlen( lbl ),
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001109 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001110 if( status != PSA_SUCCESS )
1111 {
1112 psa_generator_abort( &generator );
1113 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1114 }
1115
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001116 status = psa_generator_read( &generator, session->master,
1117 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001118 if( status != PSA_SUCCESS )
1119 {
1120 psa_generator_abort( &generator );
1121 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
1122 }
1123
1124 status = psa_generator_abort( &generator );
1125 if( status != PSA_SUCCESS )
1126 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001127 }
Hanno Becker7d0a5692018-10-23 15:26:22 +01001128 else
1129#endif
1130 {
1131 ret = handshake->tls_prf( handshake->premaster, handshake->pmslen,
1132 lbl, salt, salt_len,
Hanno Beckerf9ed7d52018-11-05 12:45:16 +00001133 session->master,
1134 master_secret_len );
Hanno Becker7d0a5692018-10-23 15:26:22 +01001135 if( ret != 0 )
1136 {
1137 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
1138 return( ret );
1139 }
Manuel Pégourié-Gonnardada30302014-10-20 20:33:10 +02001140
Hanno Becker7d0a5692018-10-23 15:26:22 +01001141 MBEDTLS_SSL_DEBUG_BUF( 3, "premaster secret",
1142 handshake->premaster,
1143 handshake->pmslen );
Hanno Becker35b23c72018-10-23 12:10:41 +01001144
Hanno Becker7d0a5692018-10-23 15:26:22 +01001145 mbedtls_platform_zeroize( handshake->premaster,
1146 sizeof(handshake->premaster) );
1147 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001148 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001149
1150 /*
1151 * Swap the client and server random values.
1152 */
Paul Bakker48916f92012-09-16 19:57:18 +00001153 memcpy( tmp, handshake->randbytes, 64 );
1154 memcpy( handshake->randbytes, tmp + 32, 32 );
1155 memcpy( handshake->randbytes + 32, tmp, 32 );
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05001156 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001157
1158 /*
1159 * SSLv3:
1160 * key block =
1161 * MD5( master + SHA1( 'A' + master + randbytes ) ) +
1162 * MD5( master + SHA1( 'BB' + master + randbytes ) ) +
1163 * MD5( master + SHA1( 'CCC' + master + randbytes ) ) +
1164 * MD5( master + SHA1( 'DDDD' + master + randbytes ) ) +
1165 * ...
1166 *
1167 * TLSv1:
1168 * key block = PRF( master, "key expansion", randbytes )
1169 */
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001170 ret = handshake->tls_prf( session->master, 48, "key expansion",
1171 handshake->randbytes, 64, keyblk, 256 );
1172 if( ret != 0 )
1173 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001174 MBEDTLS_SSL_DEBUG_RET( 1, "prf", ret );
Manuel Pégourié-Gonnarde9608182015-03-26 11:47:47 +01001175 return( ret );
1176 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001178 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ciphersuite = %s",
1179 mbedtls_ssl_get_ciphersuite_name( session->ciphersuite ) ) );
1180 MBEDTLS_SSL_DEBUG_BUF( 3, "master secret", session->master, 48 );
1181 MBEDTLS_SSL_DEBUG_BUF( 4, "random bytes", handshake->randbytes, 64 );
1182 MBEDTLS_SSL_DEBUG_BUF( 4, "key block", keyblk, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001183
Paul Bakker5121ce52009-01-03 21:22:43 +00001184 /*
1185 * Determine the appropriate key, IV and MAC length.
1186 */
Paul Bakker68884e32013-01-07 18:20:04 +01001187
Hanno Becker88aaf652017-12-27 08:17:40 +00001188 keylen = cipher_info->key_bitlen / 8;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001189
Hanno Becker8031d062018-01-03 15:32:31 +00001190#if defined(MBEDTLS_GCM_C) || \
1191 defined(MBEDTLS_CCM_C) || \
1192 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001193 if( cipher_info->mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001194 cipher_info->mode == MBEDTLS_MODE_CCM ||
1195 cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakker5121ce52009-01-03 21:22:43 +00001196 {
Hanno Beckerf704bef2018-11-16 15:21:18 +00001197 size_t explicit_ivlen;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001198
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001199 transform->maclen = 0;
Hanno Becker81c7b182017-11-09 18:39:33 +00001200 mac_key_len = 0;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001201 transform->taglen =
1202 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001203
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001204 /* All modes haves 96-bit IVs;
1205 * GCM and CCM has 4 implicit and 8 explicit bytes
1206 * ChachaPoly has all 12 bytes implicit
1207 */
Paul Bakker68884e32013-01-07 18:20:04 +01001208 transform->ivlen = 12;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001209 if( cipher_info->mode == MBEDTLS_MODE_CHACHAPOLY )
1210 transform->fixed_ivlen = 12;
1211 else
1212 transform->fixed_ivlen = 4;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001213
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02001214 /* Minimum length of encrypted record */
1215 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
Hanno Beckere694c3e2017-12-27 21:34:08 +00001216 transform->minlen = explicit_ivlen + transform->taglen;
Paul Bakker68884e32013-01-07 18:20:04 +01001217 }
1218 else
Hanno Becker8031d062018-01-03 15:32:31 +00001219#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
1220#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
1221 if( cipher_info->mode == MBEDTLS_MODE_STREAM ||
1222 cipher_info->mode == MBEDTLS_MODE_CBC )
Paul Bakker68884e32013-01-07 18:20:04 +01001223 {
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001224 /* Initialize HMAC contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001225 if( ( ret = mbedtls_md_setup( &transform->md_ctx_enc, md_info, 1 ) ) != 0 ||
1226 ( ret = mbedtls_md_setup( &transform->md_ctx_dec, md_info, 1 ) ) != 0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001227 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001228 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001229 goto end;
Paul Bakker68884e32013-01-07 18:20:04 +01001230 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001231
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001232 /* Get MAC length */
Hanno Becker81c7b182017-11-09 18:39:33 +00001233 mac_key_len = mbedtls_md_get_size( md_info );
1234 transform->maclen = mac_key_len;
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001236#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001237 /*
1238 * If HMAC is to be truncated, we shall keep the leftmost bytes,
1239 * (rfc 6066 page 13 or rfc 2104 section 4),
1240 * so we only need to adjust the length here.
1241 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001242 if( session->trunc_hmac == MBEDTLS_SSL_TRUNC_HMAC_ENABLED )
Hanno Beckere89353a2017-11-20 16:36:41 +00001243 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001244 transform->maclen = MBEDTLS_SSL_TRUNCATED_HMAC_LEN;
Hanno Beckere89353a2017-11-20 16:36:41 +00001245
1246#if defined(MBEDTLS_SSL_TRUNCATED_HMAC_COMPAT)
1247 /* Fall back to old, non-compliant version of the truncated
Hanno Becker563423f2017-11-21 17:20:17 +00001248 * HMAC implementation which also truncates the key
1249 * (Mbed TLS versions from 1.3 to 2.6.0) */
Hanno Beckere89353a2017-11-20 16:36:41 +00001250 mac_key_len = transform->maclen;
1251#endif
1252 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001253#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde800cd82014-06-18 15:34:40 +02001254
1255 /* IV length */
Paul Bakker68884e32013-01-07 18:20:04 +01001256 transform->ivlen = cipher_info->iv_size;
Paul Bakker5121ce52009-01-03 21:22:43 +00001257
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001258 /* Minimum length */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001259 if( cipher_info->mode == MBEDTLS_MODE_STREAM )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001260 transform->minlen = transform->maclen;
1261 else
Paul Bakker68884e32013-01-07 18:20:04 +01001262 {
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001263 /*
1264 * GenericBlockCipher:
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001265 * 1. if EtM is in use: one block plus MAC
1266 * otherwise: * first multiple of blocklen greater than maclen
1267 * 2. IV except for SSL3 and TLS 1.0
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001269#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
1270 if( session->encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED )
Manuel Pégourié-Gonnard169dd6a2014-11-04 16:15:39 +01001271 {
1272 transform->minlen = transform->maclen
1273 + cipher_info->block_size;
1274 }
1275 else
1276#endif
1277 {
1278 transform->minlen = transform->maclen
1279 + cipher_info->block_size
1280 - transform->maclen % cipher_info->block_size;
1281 }
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001283#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
1284 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ||
1285 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001286 ; /* No need to adjust minlen */
Paul Bakker68884e32013-01-07 18:20:04 +01001287 else
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001288#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001289#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
1290 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_2 ||
1291 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001292 {
1293 transform->minlen += transform->ivlen;
1294 }
1295 else
1296#endif
1297 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001298 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001299 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1300 goto end;
Manuel Pégourié-Gonnardeaa76f72014-06-18 16:06:02 +02001301 }
Paul Bakker68884e32013-01-07 18:20:04 +01001302 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001303 }
Hanno Becker8031d062018-01-03 15:32:31 +00001304 else
1305#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
1306 {
1307 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1308 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
1309 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001310
Hanno Becker88aaf652017-12-27 08:17:40 +00001311 MBEDTLS_SSL_DEBUG_MSG( 3, ( "keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
1312 (unsigned) keylen,
1313 (unsigned) transform->minlen,
1314 (unsigned) transform->ivlen,
1315 (unsigned) transform->maclen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001316
1317 /*
1318 * Finally setup the cipher contexts, IVs and MAC secrets.
1319 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001320#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001321 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00001322 {
Hanno Becker81c7b182017-11-09 18:39:33 +00001323 key1 = keyblk + mac_key_len * 2;
Hanno Becker88aaf652017-12-27 08:17:40 +00001324 key2 = keyblk + mac_key_len * 2 + keylen;
Paul Bakker5121ce52009-01-03 21:22:43 +00001325
Paul Bakker68884e32013-01-07 18:20:04 +01001326 mac_enc = keyblk;
Hanno Becker81c7b182017-11-09 18:39:33 +00001327 mac_dec = keyblk + mac_key_len;
Paul Bakker5121ce52009-01-03 21:22:43 +00001328
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001329 /*
1330 * This is not used in TLS v1.1.
1331 */
Paul Bakker48916f92012-09-16 19:57:18 +00001332 iv_copy_len = ( transform->fixed_ivlen ) ?
1333 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001334 memcpy( transform->iv_enc, key2 + keylen, iv_copy_len );
1335 memcpy( transform->iv_dec, key2 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001336 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001337 }
1338 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001339#endif /* MBEDTLS_SSL_CLI_C */
1340#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02001341 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00001342 {
Hanno Becker88aaf652017-12-27 08:17:40 +00001343 key1 = keyblk + mac_key_len * 2 + keylen;
Hanno Becker81c7b182017-11-09 18:39:33 +00001344 key2 = keyblk + mac_key_len * 2;
Paul Bakker5121ce52009-01-03 21:22:43 +00001345
Hanno Becker81c7b182017-11-09 18:39:33 +00001346 mac_enc = keyblk + mac_key_len;
Paul Bakker68884e32013-01-07 18:20:04 +01001347 mac_dec = keyblk;
Paul Bakker5121ce52009-01-03 21:22:43 +00001348
Paul Bakker2e11f7d2010-07-25 14:24:53 +00001349 /*
1350 * This is not used in TLS v1.1.
1351 */
Paul Bakker48916f92012-09-16 19:57:18 +00001352 iv_copy_len = ( transform->fixed_ivlen ) ?
1353 transform->fixed_ivlen : transform->ivlen;
Hanno Becker88aaf652017-12-27 08:17:40 +00001354 memcpy( transform->iv_dec, key1 + keylen, iv_copy_len );
1355 memcpy( transform->iv_enc, key1 + keylen + iv_copy_len,
Paul Bakkerca4ab492012-04-18 14:23:57 +00001356 iv_copy_len );
Paul Bakker5121ce52009-01-03 21:22:43 +00001357 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001358 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001359#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001360 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001361 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001362 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1363 goto end;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01001364 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001365
Hanno Beckerd56ed242018-01-03 15:32:51 +00001366#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001367#if defined(MBEDTLS_SSL_PROTO_SSL3)
1368 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker68884e32013-01-07 18:20:04 +01001369 {
Hanno Beckerd56ed242018-01-03 15:32:51 +00001370 if( mac_key_len > sizeof( transform->mac_enc ) )
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001371 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001372 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001373 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1374 goto end;
Manuel Pégourié-Gonnard7cfdcb82014-01-18 18:22:55 +01001375 }
1376
Hanno Becker81c7b182017-11-09 18:39:33 +00001377 memcpy( transform->mac_enc, mac_enc, mac_key_len );
1378 memcpy( transform->mac_dec, mac_dec, mac_key_len );
Paul Bakker68884e32013-01-07 18:20:04 +01001379 }
1380 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001381#endif /* MBEDTLS_SSL_PROTO_SSL3 */
1382#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1383 defined(MBEDTLS_SSL_PROTO_TLS1_2)
1384 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakker68884e32013-01-07 18:20:04 +01001385 {
Gilles Peskine039fd122018-03-19 19:06:08 +01001386 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
1387 For AEAD-based ciphersuites, there is nothing to do here. */
1388 if( mac_key_len != 0 )
1389 {
1390 mbedtls_md_hmac_starts( &transform->md_ctx_enc, mac_enc, mac_key_len );
1391 mbedtls_md_hmac_starts( &transform->md_ctx_dec, mac_dec, mac_key_len );
1392 }
Paul Bakker68884e32013-01-07 18:20:04 +01001393 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02001394 else
1395#endif
Paul Bakker577e0062013-08-28 11:57:20 +02001396 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001397 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
1399 goto end;
Paul Bakker577e0062013-08-28 11:57:20 +02001400 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001401#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker68884e32013-01-07 18:20:04 +01001402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001403#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
1404 if( mbedtls_ssl_hw_record_init != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00001405 {
1406 int ret = 0;
1407
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001408 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_init()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00001409
Hanno Becker88aaf652017-12-27 08:17:40 +00001410 if( ( ret = mbedtls_ssl_hw_record_init( ssl, key1, key2, keylen,
Paul Bakker07eb38b2012-12-19 14:42:06 +01001411 transform->iv_enc, transform->iv_dec,
1412 iv_copy_len,
Paul Bakker68884e32013-01-07 18:20:04 +01001413 mac_enc, mac_dec,
Hanno Becker81c7b182017-11-09 18:39:33 +00001414 mac_key_len ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00001415 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001416 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_init", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001417 ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1418 goto end;
Paul Bakker05ef8352012-05-08 09:17:57 +00001419 }
1420 }
Hanno Beckerd56ed242018-01-03 15:32:51 +00001421#else
1422 ((void) mac_dec);
1423 ((void) mac_enc);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001424#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker05ef8352012-05-08 09:17:57 +00001425
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001426#if defined(MBEDTLS_SSL_EXPORT_KEYS)
1427 if( ssl->conf->f_export_keys != NULL )
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001428 {
Manuel Pégourié-Gonnard024b6df2015-10-19 13:52:53 +02001429 ssl->conf->f_export_keys( ssl->conf->p_export_keys,
1430 session->master, keyblk,
Hanno Becker88aaf652017-12-27 08:17:40 +00001431 mac_key_len, keylen,
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001432 iv_copy_len );
1433 }
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001434
1435 if( ssl->conf->f_export_keys_ext != NULL )
1436 {
1437 ssl->conf->f_export_keys_ext( ssl->conf->p_export_keys,
1438 session->master, keyblk,
Ron Eldorb7fd64c2019-05-12 11:03:32 +03001439 mac_key_len, keylen,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001440 iv_copy_len,
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001441 handshake->randbytes + 32,
Ron Eldor51d3ab52019-05-12 14:54:30 +03001442 handshake->randbytes,
Ron Eldorcf280092019-05-14 20:19:13 +03001443 tls_prf_get_type( handshake->tls_prf ) );
Ron Eldorf5cc10d2019-05-07 18:33:40 +03001444 }
Robert Cragie4feb7ae2015-10-02 13:33:37 +01001445#endif
1446
Hanno Beckerf704bef2018-11-16 15:21:18 +00001447#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001448
1449 /* Only use PSA-based ciphers for TLS-1.2.
1450 * That's relevant at least for TLS-1.0, where
1451 * we assume that mbedtls_cipher_crypt() updates
1452 * the structure field for the IV, which the PSA-based
1453 * implementation currently doesn't. */
1454#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1455 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001456 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001457 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_enc,
Hanno Becker22bf1452019-04-05 11:21:08 +01001458 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001459 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1460 {
1461 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001462 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001463 }
1464
1465 if( ret == 0 )
1466 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001467 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based encryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001468 psa_fallthrough = 0;
1469 }
1470 else
1471 {
1472 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record encryption - fall through to default setup." ) );
1473 psa_fallthrough = 1;
1474 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001475 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001476 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001477 psa_fallthrough = 1;
1478#else
1479 psa_fallthrough = 1;
1480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001481
Hanno Beckercb1cc802018-11-17 22:27:38 +00001482 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001483#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001484 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_enc,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001485 cipher_info ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00001486 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001487 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001488 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001489 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001490
Hanno Beckerf704bef2018-11-16 15:21:18 +00001491#if defined(MBEDTLS_USE_PSA_CRYPTO)
Hanno Beckercb1cc802018-11-17 22:27:38 +00001492 /* Only use PSA-based ciphers for TLS-1.2.
1493 * That's relevant at least for TLS-1.0, where
1494 * we assume that mbedtls_cipher_crypt() updates
1495 * the structure field for the IV, which the PSA-based
1496 * implementation currently doesn't. */
1497#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1498 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001499 {
Hanno Beckercb1cc802018-11-17 22:27:38 +00001500 ret = mbedtls_cipher_setup_psa( &transform->cipher_ctx_dec,
Hanno Becker22bf1452019-04-05 11:21:08 +01001501 cipher_info, transform->taglen );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001502 if( ret != 0 && ret != MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE )
1503 {
1504 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup_psa", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001505 goto end;
Hanno Beckercb1cc802018-11-17 22:27:38 +00001506 }
1507
1508 if( ret == 0 )
1509 {
Hanno Becker4c8c7aa2019-04-10 09:25:41 +01001510 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Successfully setup PSA-based decryption cipher context" ) );
Hanno Beckercb1cc802018-11-17 22:27:38 +00001511 psa_fallthrough = 0;
1512 }
1513 else
1514 {
1515 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to setup PSA-based cipher context for record decryption - fall through to default setup." ) );
1516 psa_fallthrough = 1;
1517 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001518 }
Hanno Beckerf704bef2018-11-16 15:21:18 +00001519 else
Hanno Beckercb1cc802018-11-17 22:27:38 +00001520 psa_fallthrough = 1;
1521#else
1522 psa_fallthrough = 1;
1523#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Beckerf704bef2018-11-16 15:21:18 +00001524
Hanno Beckercb1cc802018-11-17 22:27:38 +00001525 if( psa_fallthrough == 1 )
Hanno Beckerf704bef2018-11-16 15:21:18 +00001526#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001527 if( ( ret = mbedtls_cipher_setup( &transform->cipher_ctx_dec,
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001528 cipher_info ) ) != 0 )
1529 {
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +02001530 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setup", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001531 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001532 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001533
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001534 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_enc, key1,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001535 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001536 MBEDTLS_ENCRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001537 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001538 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001539 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001540 }
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02001541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542 if( ( ret = mbedtls_cipher_setkey( &transform->cipher_ctx_dec, key2,
Manuel Pégourié-Gonnard898e0aa2015-06-18 15:28:12 +02001543 cipher_info->key_bitlen,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544 MBEDTLS_DECRYPT ) ) != 0 )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001545 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_setkey", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001547 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001548 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02001549
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001550#if defined(MBEDTLS_CIPHER_MODE_CBC)
1551 if( cipher_info->mode == MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001552 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001553 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_enc,
1554 MBEDTLS_PADDING_NONE ) ) != 0 )
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001555 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001556 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001557 goto end;
Manuel Pégourié-Gonnard126a66f2013-10-25 18:33:32 +02001558 }
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001559
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001560 if( ( ret = mbedtls_cipher_set_padding_mode( &transform->cipher_ctx_dec,
1561 MBEDTLS_PADDING_NONE ) ) != 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_set_padding_mode", ret );
Ron Eldore6992702019-05-07 18:27:13 +03001564 goto end;
Manuel Pégourié-Gonnard88665912013-10-25 18:42:44 +02001565 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001566 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +00001568
Paul Bakker5121ce52009-01-03 21:22:43 +00001569
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker2770fbd2012-07-03 13:30:23 +00001571 // Initialize compression
1572 //
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001573 if( session->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001574 {
Paul Bakker16770332013-10-11 09:59:44 +02001575 if( ssl->compress_buf == NULL )
1576 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001577 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Allocating compression buffer" ) );
Angus Grattond8213d02016-05-25 20:56:48 +10001578 ssl->compress_buf = mbedtls_calloc( 1, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Paul Bakker16770332013-10-11 09:59:44 +02001579 if( ssl->compress_buf == NULL )
1580 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02001581 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
Angus Grattond8213d02016-05-25 20:56:48 +10001582 MBEDTLS_SSL_COMPRESS_BUFFER_LEN ) );
Ron Eldore6992702019-05-07 18:27:13 +03001583 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
1584 goto end;
Paul Bakker16770332013-10-11 09:59:44 +02001585 }
1586 }
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Initializing zlib states" ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001589
Paul Bakker48916f92012-09-16 19:57:18 +00001590 memset( &transform->ctx_deflate, 0, sizeof( transform->ctx_deflate ) );
1591 memset( &transform->ctx_inflate, 0, sizeof( transform->ctx_inflate ) );
Paul Bakker2770fbd2012-07-03 13:30:23 +00001592
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02001593 if( deflateInit( &transform->ctx_deflate,
1594 Z_DEFAULT_COMPRESSION ) != Z_OK ||
Paul Bakker48916f92012-09-16 19:57:18 +00001595 inflateInit( &transform->ctx_inflate ) != Z_OK )
Paul Bakker2770fbd2012-07-03 13:30:23 +00001596 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001597 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Failed to initialize compression" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001598 ret = MBEDTLS_ERR_SSL_COMPRESSION_FAILED;
1599 goto end;
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600 }
1601 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001602#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001604 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= derive keys" ) );
Ron Eldore6992702019-05-07 18:27:13 +03001605end:
Ron Eldora9f9a732019-05-07 18:29:02 +03001606 mbedtls_platform_zeroize( keyblk, sizeof( keyblk ) );
1607 mbedtls_platform_zeroize( handshake->randbytes,
1608 sizeof( handshake->randbytes ) );
Ron Eldore6992702019-05-07 18:27:13 +03001609 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00001610}
1611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001612#if defined(MBEDTLS_SSL_PROTO_SSL3)
1613void ssl_calc_verify_ssl( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001614{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001615 mbedtls_md5_context md5;
1616 mbedtls_sha1_context sha1;
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 unsigned char pad_1[48];
1618 unsigned char pad_2[48];
1619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001620 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify ssl" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001621
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001622 mbedtls_md5_init( &md5 );
1623 mbedtls_sha1_init( &sha1 );
1624
1625 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1626 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001627
Paul Bakker380da532012-04-18 16:10:25 +00001628 memset( pad_1, 0x36, 48 );
1629 memset( pad_2, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00001630
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001631 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1632 mbedtls_md5_update_ret( &md5, pad_1, 48 );
1633 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001634
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001635 mbedtls_md5_starts_ret( &md5 );
1636 mbedtls_md5_update_ret( &md5, ssl->session_negotiate->master, 48 );
1637 mbedtls_md5_update_ret( &md5, pad_2, 48 );
1638 mbedtls_md5_update_ret( &md5, hash, 16 );
1639 mbedtls_md5_finish_ret( &md5, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001640
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001641 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1642 mbedtls_sha1_update_ret( &sha1, pad_1, 40 );
1643 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001644
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001645 mbedtls_sha1_starts_ret( &sha1 );
1646 mbedtls_sha1_update_ret( &sha1, ssl->session_negotiate->master, 48 );
1647 mbedtls_sha1_update_ret( &sha1, pad_2, 40 );
1648 mbedtls_sha1_update_ret( &sha1, hash + 16, 20 );
1649 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1652 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001653
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001654 mbedtls_md5_free( &md5 );
1655 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001656
Paul Bakker380da532012-04-18 16:10:25 +00001657 return;
1658}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker380da532012-04-18 16:10:25 +00001660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
1662void ssl_calc_verify_tls( mbedtls_ssl_context *ssl, unsigned char hash[36] )
Paul Bakker380da532012-04-18 16:10:25 +00001663{
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001664 mbedtls_md5_context md5;
1665 mbedtls_sha1_context sha1;
Paul Bakker380da532012-04-18 16:10:25 +00001666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify tls" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001668
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001669 mbedtls_md5_init( &md5 );
1670 mbedtls_sha1_init( &sha1 );
1671
1672 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
1673 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker380da532012-04-18 16:10:25 +00001674
Andrzej Kurekeb342242019-01-29 09:14:33 -05001675 mbedtls_md5_finish_ret( &md5, hash );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001676 mbedtls_sha1_finish_ret( &sha1, hash + 16 );
Paul Bakker380da532012-04-18 16:10:25 +00001677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001678 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 36 );
1679 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001680
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001681 mbedtls_md5_free( &md5 );
1682 mbedtls_sha1_free( &sha1 );
Paul Bakker5b4af392014-06-26 12:09:34 +02001683
Paul Bakker380da532012-04-18 16:10:25 +00001684 return;
1685}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001686#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker380da532012-04-18 16:10:25 +00001687
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001688#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
1689#if defined(MBEDTLS_SHA256_C)
1690void ssl_calc_verify_tls_sha256( mbedtls_ssl_context *ssl, unsigned char hash[32] )
Paul Bakker380da532012-04-18 16:10:25 +00001691{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001692#if defined(MBEDTLS_USE_PSA_CRYPTO)
1693 size_t hash_size;
1694 psa_status_t status;
1695 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
1696
1697 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha256" ) );
1698 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
1699 if( status != PSA_SUCCESS )
1700 {
1701 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1702 return;
1703 }
1704
1705 status = psa_hash_finish( &sha256_psa, hash, 32, &hash_size );
1706 if( status != PSA_SUCCESS )
1707 {
1708 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1709 return;
1710 }
1711 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 32 );
1712 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1713#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001714 mbedtls_sha256_context sha256;
Paul Bakker380da532012-04-18 16:10:25 +00001715
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001716 mbedtls_sha256_init( &sha256 );
1717
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001718 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha256" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001719
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001720 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001721 mbedtls_sha256_finish_ret( &sha256, hash );
Paul Bakker380da532012-04-18 16:10:25 +00001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 32 );
1724 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001725
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001726 mbedtls_sha256_free( &sha256 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001727#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker380da532012-04-18 16:10:25 +00001728 return;
1729}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001730#endif /* MBEDTLS_SHA256_C */
Paul Bakker380da532012-04-18 16:10:25 +00001731
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#if defined(MBEDTLS_SHA512_C)
1733void ssl_calc_verify_tls_sha384( mbedtls_ssl_context *ssl, unsigned char hash[48] )
Paul Bakker380da532012-04-18 16:10:25 +00001734{
Andrzej Kurekeb342242019-01-29 09:14:33 -05001735#if defined(MBEDTLS_USE_PSA_CRYPTO)
1736 size_t hash_size;
1737 psa_status_t status;
Andrzej Kurek972fba52019-01-30 03:29:12 -05001738 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05001739
1740 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> PSA calc verify sha384" ) );
Andrzej Kurek972fba52019-01-30 03:29:12 -05001741 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001742 if( status != PSA_SUCCESS )
1743 {
1744 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
1745 return;
1746 }
1747
Andrzej Kurek972fba52019-01-30 03:29:12 -05001748 status = psa_hash_finish( &sha384_psa, hash, 48, &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001749 if( status != PSA_SUCCESS )
1750 {
1751 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
1752 return;
1753 }
1754 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated verify result", hash, 48 );
1755 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= PSA calc verify" ) );
1756#else
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001757 mbedtls_sha512_context sha512;
Paul Bakker380da532012-04-18 16:10:25 +00001758
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02001759 mbedtls_sha512_init( &sha512 );
1760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001761 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc verify sha384" ) );
Paul Bakker380da532012-04-18 16:10:25 +00001762
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001763 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01001764 mbedtls_sha512_finish_ret( &sha512, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +00001765
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001766 MBEDTLS_SSL_DEBUG_BUF( 3, "calculated verify result", hash, 48 );
1767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc verify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00001768
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02001769 mbedtls_sha512_free( &sha512 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05001770#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker5121ce52009-01-03 21:22:43 +00001771 return;
1772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_SHA512_C */
1774#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001776#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
1777int 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 +02001778{
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001779 unsigned char *p = ssl->handshake->premaster;
1780 unsigned char *end = p + sizeof( ssl->handshake->premaster );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001781 const unsigned char *psk = ssl->conf->psk;
1782 size_t psk_len = ssl->conf->psk_len;
1783
1784 /* If the psk callback was called, use its result */
1785 if( ssl->handshake->psk != NULL )
1786 {
1787 psk = ssl->handshake->psk;
1788 psk_len = ssl->handshake->psk_len;
1789 }
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001790
1791 /*
1792 * PMS = struct {
1793 * opaque other_secret<0..2^16-1>;
1794 * opaque psk<0..2^16-1>;
1795 * };
1796 * with "other_secret" depending on the particular key exchange
1797 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001798#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
1799 if( key_ex == MBEDTLS_KEY_EXCHANGE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001800 {
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001801 if( end - p < 2 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001802 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001803
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001804 *(p++) = (unsigned char)( psk_len >> 8 );
1805 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001806
1807 if( end < p || (size_t)( end - p ) < psk_len )
1808 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1809
1810 memset( p, 0, psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001811 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001812 }
1813 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001814#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
1815#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
1816 if( key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001817 {
1818 /*
1819 * other_secret already set by the ClientKeyExchange message,
1820 * and is 48 bytes long
1821 */
Philippe Antoine747fd532018-05-30 09:13:21 +02001822 if( end - p < 2 )
1823 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1824
Manuel Pégourié-Gonnard0fae60b2013-10-14 17:39:48 +02001825 *p++ = 0;
1826 *p++ = 48;
1827 p += 48;
1828 }
1829 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001830#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
1831#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
1832 if( key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001833 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001834 int ret;
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001835 size_t len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001836
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001837 /* Write length only when we know the actual value */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001838 if( ( ret = mbedtls_dhm_calc_secret( &ssl->handshake->dhm_ctx,
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +01001839 p + 2, end - ( p + 2 ), &len,
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001840 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001841 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001842 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_dhm_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001843 return( ret );
1844 }
Manuel Pégourié-Gonnard8df68632014-06-23 17:56:08 +02001845 *(p++) = (unsigned char)( len >> 8 );
1846 *(p++) = (unsigned char)( len );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001847 p += len;
1848
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001849 MBEDTLS_SSL_DEBUG_MPI( 3, "DHM: K ", &ssl->handshake->dhm_ctx.K );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001850 }
1851 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001852#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
1853#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
1854 if( key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001855 {
Manuel Pégourié-Gonnard1b62c7f2013-10-14 14:02:19 +02001856 int ret;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001857 size_t zlen;
1858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001859 if( ( ret = mbedtls_ecdh_calc_secret( &ssl->handshake->ecdh_ctx, &zlen,
Paul Bakker66d5d072014-06-17 16:39:18 +02001860 p + 2, end - ( p + 2 ),
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001861 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001862 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001863 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ecdh_calc_secret", ret );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001864 return( ret );
1865 }
1866
1867 *(p++) = (unsigned char)( zlen >> 8 );
1868 *(p++) = (unsigned char)( zlen );
1869 p += zlen;
1870
Andrzej Kurekc470b6b2019-01-31 08:20:20 -05001871 MBEDTLS_SSL_DEBUG_ECDH( 3, &ssl->handshake->ecdh_ctx,
1872 MBEDTLS_DEBUG_ECDH_Z );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001873 }
1874 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001875#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001877 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
1878 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001879 }
1880
1881 /* opaque psk<0..2^16-1>; */
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001882 if( end - p < 2 )
1883 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnardb2bf5a12014-03-25 16:28:12 +01001884
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001885 *(p++) = (unsigned char)( psk_len >> 8 );
1886 *(p++) = (unsigned char)( psk_len );
Manuel Pégourié-Gonnardbc5e5082015-10-21 12:35:29 +02001887
1888 if( end < p || (size_t)( end - p ) < psk_len )
1889 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
1890
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01001891 memcpy( p, psk, psk_len );
1892 p += psk_len;
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001893
1894 ssl->handshake->pmslen = p - ssl->handshake->premaster;
1895
1896 return( 0 );
1897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Manuel Pégourié-Gonnardbd1ae242013-10-14 13:09:25 +02001899
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001900#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00001901/*
1902 * SSLv3.0 MAC functions
1903 */
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001904#define SSL_MAC_MAX_BYTES 20 /* MD-5 or SHA-1 */
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001905static void ssl_mac( mbedtls_md_context_t *md_ctx,
1906 const unsigned char *secret,
1907 const unsigned char *buf, size_t len,
1908 const unsigned char *ctr, int type,
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01001909 unsigned char out[SSL_MAC_MAX_BYTES] )
Paul Bakker5121ce52009-01-03 21:22:43 +00001910{
1911 unsigned char header[11];
1912 unsigned char padding[48];
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001913 int padlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001914 int md_size = mbedtls_md_get_size( md_ctx->md_info );
1915 int md_type = mbedtls_md_get_type( md_ctx->md_info );
Paul Bakker68884e32013-01-07 18:20:04 +01001916
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001917 /* Only MD5 and SHA-1 supported */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001918 if( md_type == MBEDTLS_MD_MD5 )
Paul Bakker68884e32013-01-07 18:20:04 +01001919 padlen = 48;
Manuel Pégourié-Gonnard8d4ad072014-07-13 14:43:28 +02001920 else
Paul Bakker68884e32013-01-07 18:20:04 +01001921 padlen = 40;
Paul Bakker5121ce52009-01-03 21:22:43 +00001922
1923 memcpy( header, ctr, 8 );
1924 header[ 8] = (unsigned char) type;
1925 header[ 9] = (unsigned char)( len >> 8 );
1926 header[10] = (unsigned char)( len );
1927
Paul Bakker68884e32013-01-07 18:20:04 +01001928 memset( padding, 0x36, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001929 mbedtls_md_starts( md_ctx );
1930 mbedtls_md_update( md_ctx, secret, md_size );
1931 mbedtls_md_update( md_ctx, padding, padlen );
1932 mbedtls_md_update( md_ctx, header, 11 );
1933 mbedtls_md_update( md_ctx, buf, len );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001934 mbedtls_md_finish( md_ctx, out );
Paul Bakker5121ce52009-01-03 21:22:43 +00001935
Paul Bakker68884e32013-01-07 18:20:04 +01001936 memset( padding, 0x5C, padlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001937 mbedtls_md_starts( md_ctx );
1938 mbedtls_md_update( md_ctx, secret, md_size );
1939 mbedtls_md_update( md_ctx, padding, padlen );
Manuel Pégourié-Gonnard464147c2017-12-18 18:04:59 +01001940 mbedtls_md_update( md_ctx, out, md_size );
1941 mbedtls_md_finish( md_ctx, out );
Paul Bakker5f70b252012-09-13 14:23:06 +00001942}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001943#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5f70b252012-09-13 14:23:06 +00001944
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001945/* The function below is only used in the Lucky 13 counter-measure in
Hanno Beckerb2ca87d2018-10-18 15:43:13 +01001946 * mbedtls_ssl_decrypt_buf(). These are the defines that guard the call site. */
Hanno Becker52344c22018-01-03 15:24:20 +00001947#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC) && \
Manuel Pégourié-Gonnard7b420302018-06-28 10:38:35 +02001948 ( defined(MBEDTLS_SSL_PROTO_TLS1) || \
1949 defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
1950 defined(MBEDTLS_SSL_PROTO_TLS1_2) )
1951/* This function makes sure every byte in the memory region is accessed
1952 * (in ascending addresses order) */
1953static void ssl_read_memory( unsigned char *p, size_t len )
1954{
1955 unsigned char acc = 0;
1956 volatile unsigned char force;
1957
1958 for( ; len != 0; p++, len-- )
1959 acc ^= *p;
1960
1961 force = acc;
1962 (void) force;
1963}
1964#endif /* SSL_SOME_MODES_USE_MAC && ( TLS1 || TLS1_1 || TLS1_2 ) */
1965
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01001966/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001967 * Encryption/decryption functions
Paul Bakkerf7abd422013-04-16 13:15:56 +02001968 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00001969
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01001970#if defined(MBEDTLS_SSL_CID)
1971/* This functions transforms a pair of a DTLS plaintext fragment
1972 * and a record content type into an instance of the DTLSInnerPlaintext
1973 * structure:
1974 *
1975 * struct {
1976 * opaque content[DTLSPlaintext.length];
1977 * ContentType real_type;
1978 * uint8 zeros[length_of_padding];
1979 * } DTLSInnerPlaintext;
1980 *
1981 * Input:
1982 * - `content`: The beginning of the buffer holding the
1983 * plaintext to be wrapped.
1984 * - `*content_size`: The length of the plaintext in Bytes.
1985 * - `max_len`: The number of Bytes available starting from
1986 * `content`. This must be `>= *content_size`.
1987 * - `rec_type`: The desired record content type.
1988 *
1989 * Output:
1990 * - `content`: The beginning of the resulting DTLSInnerPlaintext structure.
1991 * - `*content_size`: The length of the resulting DTLSInnerPlaintext structure.
1992 *
1993 * Returns:
1994 * - `0` on success.
1995 * - A negative error code if `max_len` didn't offer enough space
1996 * for the expansion.
1997 */
1998static int ssl_cid_build_inner_plaintext( unsigned char *content,
1999 size_t *content_size,
2000 size_t remaining,
2001 uint8_t rec_type )
2002{
2003 size_t len = *content_size;
2004 size_t pad = ~len & 0xF; /* Pad to a multiple of 16 */
2005
2006 /* Write real content type */
2007 if( remaining == 0 )
2008 return( -1 );
2009 content[ len ] = rec_type;
2010 len++;
2011 remaining--;
2012
2013 if( remaining < pad )
2014 return( -1 );
2015 memset( content + len, 0, pad );
2016 len += pad;
2017 remaining -= pad;
2018
2019 *content_size = len;
2020 return( 0 );
2021}
2022
2023/* This function parses a DTLSInnerPlaintext structure
2024 *
2025 * struct {
2026 * opaque content[DTLSPlaintext.length];
2027 * ContentType real_type;
2028 * uint8 zeros[length_of_padding];
2029 * } DTLSInnerPlaintext;
2030 */
2031static int ssl_cid_parse_inner_plaintext( unsigned char const *content,
2032 size_t *content_size,
2033 uint8_t *rec_type )
2034{
2035 size_t remaining = *content_size;
2036
2037 /* Determine length of padding by skipping zeroes from the back. */
2038 do
2039 {
2040 if( remaining == 0 )
2041 return( -1 );
2042 remaining--;
2043 } while( content[ remaining ] == 0 );
2044
2045 *content_size = remaining;
2046 *rec_type = content[ remaining ];
2047
2048 return( 0 );
2049}
2050#endif /* MBEDTLS_SSL_CID */
2051
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002052/* `add_data` must have size 13 Bytes if the CID extension is disabled,
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002053 * and 13 + 1 + CID-length Bytes if the CID extension is enabled. */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002054static void ssl_extract_add_data_from_record( unsigned char* add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002055 size_t *add_data_len,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002056 mbedtls_record *rec )
2057{
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002058 /* Quoting RFC 5246 (TLS 1.2):
Hanno Beckercab87e62019-04-29 13:52:53 +01002059 *
2060 * additional_data = seq_num + TLSCompressed.type +
2061 * TLSCompressed.version + TLSCompressed.length;
2062 *
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002063 * For the CID extension, this is extended as follows
2064 * (quoting draft-ietf-tls-dtls-connection-id-05,
2065 * https://tools.ietf.org/html/draft-ietf-tls-dtls-connection-id-05):
Hanno Beckercab87e62019-04-29 13:52:53 +01002066 *
2067 * additional_data = seq_num + DTLSPlaintext.type +
2068 * DTLSPlaintext.version +
Hanno Beckerd5aeab12019-05-20 14:50:53 +01002069 * cid +
2070 * cid_length +
Hanno Beckercab87e62019-04-29 13:52:53 +01002071 * length_of_DTLSInnerPlaintext;
2072 */
2073
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002074 memcpy( add_data, rec->ctr, sizeof( rec->ctr ) );
2075 add_data[8] = rec->type;
Hanno Beckercab87e62019-04-29 13:52:53 +01002076 memcpy( add_data + 9, rec->ver, sizeof( rec->ver ) );
2077
2078#if defined(MBEDTLS_SSL_CID)
2079 memcpy( add_data + 11, rec->cid, rec->cid_len );
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002080 add_data[11 + rec->cid_len + 0] = rec->cid_len;
2081 add_data[11 + rec->cid_len + 1] = ( rec->data_len >> 8 ) & 0xFF;
2082 add_data[11 + rec->cid_len + 2] = ( rec->data_len >> 0 ) & 0xFF;
2083 *add_data_len = 13 + 1 + rec->cid_len;
Hanno Becker43c24b82019-05-01 09:45:57 +01002084#else /* MBEDTLS_SSL_CID */
2085 add_data[11 + 0] = ( rec->data_len >> 8 ) & 0xFF;
2086 add_data[11 + 1] = ( rec->data_len >> 0 ) & 0xFF;
Hanno Beckercab87e62019-04-29 13:52:53 +01002087 *add_data_len = 13;
2088#endif /* MBEDTLS_SSL_CID */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002089}
2090
Hanno Beckera18d1322018-01-03 14:27:32 +00002091int mbedtls_ssl_encrypt_buf( mbedtls_ssl_context *ssl,
2092 mbedtls_ssl_transform *transform,
2093 mbedtls_record *rec,
2094 int (*f_rng)(void *, unsigned char *, size_t),
2095 void *p_rng )
Paul Bakker5121ce52009-01-03 21:22:43 +00002096{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002097 mbedtls_cipher_mode_t mode;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002098 int auth_done = 0;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002099 unsigned char * data;
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002100 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002101 size_t add_data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002102 size_t post_avail;
2103
2104 /* The SSL context is only used for debugging purposes! */
Hanno Beckera18d1322018-01-03 14:27:32 +00002105#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002106 ((void) ssl);
2107#endif
2108
2109 /* The PRNG is used for dynamic IV generation that's used
2110 * for CBC transformations in TLS 1.1 and TLS 1.2. */
2111#if !( defined(MBEDTLS_CIPHER_MODE_CBC) && \
2112 ( defined(MBEDTLS_AES_C) || \
2113 defined(MBEDTLS_ARIA_C) || \
2114 defined(MBEDTLS_CAMELLIA_C) ) && \
2115 ( defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2) ) )
2116 ((void) f_rng);
2117 ((void) p_rng);
2118#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002120 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002121
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002122 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002123 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002124 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to encrypt_buf" ) );
2125 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2126 }
Hanno Becker43c24b82019-05-01 09:45:57 +01002127 if( rec == NULL
2128 || rec->buf == NULL
2129 || rec->buf_len < rec->data_offset
2130 || rec->buf_len - rec->data_offset < rec->data_len
2131#if defined(MBEDTLS_SSL_CID)
2132 || rec->cid_len != 0
2133#endif
2134 )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002135 {
2136 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to encrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002137 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002138 }
2139
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002140 data = rec->buf + rec->data_offset;
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002141 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002142 MBEDTLS_SSL_DEBUG_BUF( 4, "before encrypt: output payload",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002143 data, rec->data_len );
2144
2145 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc );
2146
2147 if( rec->data_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
2148 {
2149 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Record content %u too large, maximum %d",
2150 (unsigned) rec->data_len,
2151 MBEDTLS_SSL_OUT_CONTENT_LEN ) );
2152 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
2153 }
Manuel Pégourié-Gonnard60346be2014-11-21 11:38:37 +01002154
Hanno Beckercab87e62019-04-29 13:52:53 +01002155#if defined(MBEDTLS_SSL_CID)
2156 /*
2157 * Add CID information
2158 */
2159 rec->cid_len = transform->out_cid_len;
2160 memcpy( rec->cid, transform->out_cid, transform->out_cid_len );
2161 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002162
2163 if( rec->cid_len != 0 )
2164 {
2165 /*
2166 * Wrap plaintext into DTLSInnerPlaintext structure
2167 *
2168 * struct {
2169 * opaque content[DTLSPlaintext.length];
2170 * ContentType real_type;
2171 * uint8 zeros[length_of_padding];
2172 * } DTLSInnerPlaintext;
2173 *
2174 * and change the record content type.
2175 *
2176 * The rest of the record encryption stays
2177 * unmodified (apart from the inclusion of
2178 * the CID into the additional data for the
2179 * record MAC).
2180 */
2181 if( ssl_cid_build_inner_plaintext( data,
2182 &rec->data_len,
2183 post_avail,
2184 rec->type ) != 0 )
2185 {
2186 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2187 }
2188
2189 rec->type = MBEDTLS_SSL_MSG_CID;
2190 }
Hanno Beckercab87e62019-04-29 13:52:53 +01002191#endif /* MBEDTLS_SSL_CID */
2192
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002193 post_avail = rec->buf_len - ( rec->data_len + rec->data_offset );
2194
Paul Bakker5121ce52009-01-03 21:22:43 +00002195 /*
Manuel Pégourié-Gonnard0098e7d2014-10-28 13:08:59 +01002196 * Add MAC before if needed
Paul Bakker5121ce52009-01-03 21:22:43 +00002197 */
Hanno Becker52344c22018-01-03 15:24:20 +00002198#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002199 if( mode == MBEDTLS_MODE_STREAM ||
2200 ( mode == MBEDTLS_MODE_CBC
2201#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002202 && transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002203#endif
2204 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00002205 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002206 if( post_avail < transform->maclen )
2207 {
2208 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2209 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2210 }
2211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002212#if defined(MBEDTLS_SSL_PROTO_SSL3)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002213 if( transform->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002214 {
Manuel Pégourié-Gonnardb053efb2017-12-19 10:03:46 +01002215 unsigned char mac[SSL_MAC_MAX_BYTES];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002216 ssl_mac( &transform->md_ctx_enc, transform->mac_enc,
2217 data, rec->data_len, rec->ctr, rec->type, mac );
2218 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002219 }
2220 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002221#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002222#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
2223 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002224 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002225 {
Hanno Becker992b6872017-11-09 18:57:39 +00002226 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2227
Hanno Beckercab87e62019-04-29 13:52:53 +01002228 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker992b6872017-11-09 18:57:39 +00002229
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002230 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002231 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002232 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2233 data, rec->data_len );
2234 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2235 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
2236
2237 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002238 }
2239 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02002240#endif
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002241 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002244 }
2245
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002246 MBEDTLS_SSL_DEBUG_BUF( 4, "computed mac", data + rec->data_len,
2247 transform->maclen );
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002248
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002249 rec->data_len += transform->maclen;
2250 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002251 auth_done++;
Paul Bakker577e0062013-08-28 11:57:20 +02002252 }
Hanno Becker52344c22018-01-03 15:24:20 +00002253#endif /* MBEDTLS_SSL_SOME_MODES_USE_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002254
Manuel Pégourié-Gonnard71096242013-10-25 19:31:25 +02002255 /*
2256 * Encrypt
2257 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002258#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2259 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker5121ce52009-01-03 21:22:43 +00002260 {
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002261 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002262 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002263 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002264 "including %d bytes of padding",
2265 rec->data_len, 0 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002266
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002267 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2268 transform->iv_enc, transform->ivlen,
2269 data, rec->data_len,
2270 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002271 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002272 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002273 return( ret );
2274 }
2275
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002276 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002278 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2279 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002280 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002281 }
Paul Bakker68884e32013-01-07 18:20:04 +01002282 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002283#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002284
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002285#if defined(MBEDTLS_GCM_C) || \
2286 defined(MBEDTLS_CCM_C) || \
2287 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002288 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002289 mode == MBEDTLS_MODE_CCM ||
2290 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002291 {
Manuel Pégourié-Gonnard2e5ee322014-05-14 13:09:22 +02002292 int ret;
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002293 unsigned char iv[12];
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002294 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002295
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002296 /* Check that there's space for both the authentication tag
2297 * and the explicit IV before and after the record content. */
2298 if( post_avail < transform->taglen ||
2299 rec->data_offset < explicit_iv_len )
2300 {
2301 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2302 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2303 }
Paul Bakkerca4ab492012-04-18 14:23:57 +00002304
Paul Bakker68884e32013-01-07 18:20:04 +01002305 /*
2306 * Generate IV
2307 */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002308 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2309 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002310 /* GCM and CCM: fixed || explicit (=seqnum) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002311 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002312 memcpy( iv + transform->fixed_ivlen, rec->ctr,
2313 explicit_iv_len );
2314 /* Prefix record content with explicit IV. */
2315 memcpy( data - explicit_iv_len, rec->ctr, explicit_iv_len );
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002316 }
2317 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2318 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002319 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002320 unsigned char i;
2321
2322 memcpy( iv, transform->iv_enc, transform->fixed_ivlen );
2323
2324 for( i = 0; i < 8; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002325 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002326 }
2327 else
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002328 {
2329 /* Reminder if we ever add an AEAD mode with a different size */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002330 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2331 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd056ce02014-10-29 22:29:20 +01002332 }
2333
Hanno Beckercab87e62019-04-29 13:52:53 +01002334 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002335
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002336 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (internal)",
2337 iv, transform->ivlen );
2338 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used (transmitted)",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002339 data - explicit_iv_len, explicit_iv_len );
2340 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002341 add_data, add_data_len );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002342 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002343 "including 0 bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002344 rec->data_len ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00002345
Paul Bakker68884e32013-01-07 18:20:04 +01002346 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002347 * Encrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002348 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002349
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002350 if( ( ret = mbedtls_cipher_auth_encrypt( &transform->cipher_ctx_enc,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002351 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002352 add_data, add_data_len, /* add data */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002353 data, rec->data_len, /* source */
2354 data, &rec->data_len, /* destination */
2355 data + rec->data_len, transform->taglen ) ) != 0 )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002356 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002357 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_encrypt", ret );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002358 return( ret );
2359 }
2360
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002361 MBEDTLS_SSL_DEBUG_BUF( 4, "after encrypt: tag",
2362 data + rec->data_len, transform->taglen );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002363
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002364 rec->data_len += transform->taglen + explicit_iv_len;
2365 rec->data_offset -= explicit_iv_len;
2366 post_avail -= transform->taglen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002367 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002368 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002369 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002370#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C */
2371#if defined(MBEDTLS_CIPHER_MODE_CBC) && \
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002372 ( defined(MBEDTLS_AES_C) || defined(MBEDTLS_CAMELLIA_C) || defined(MBEDTLS_ARIA_C) )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002373 if( mode == MBEDTLS_MODE_CBC )
Paul Bakker5121ce52009-01-03 21:22:43 +00002374 {
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002375 int ret;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002376 size_t padlen, i;
2377 size_t olen;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002378
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002379 /* Currently we're always using minimal padding
2380 * (up to 255 bytes would be allowed). */
2381 padlen = transform->ivlen - ( rec->data_len + 1 ) % transform->ivlen;
2382 if( padlen == transform->ivlen )
Paul Bakker5121ce52009-01-03 21:22:43 +00002383 padlen = 0;
2384
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002385 /* Check there's enough space in the buffer for the padding. */
2386 if( post_avail < padlen + 1 )
2387 {
2388 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2389 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2390 }
2391
Paul Bakker5121ce52009-01-03 21:22:43 +00002392 for( i = 0; i <= padlen; i++ )
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002393 data[rec->data_len + i] = (unsigned char) padlen;
Paul Bakker5121ce52009-01-03 21:22:43 +00002394
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002395 rec->data_len += padlen + 1;
2396 post_avail -= padlen + 1;
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002398#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002399 /*
Paul Bakker1ef83d62012-04-11 12:09:53 +00002400 * Prepend per-record IV for block cipher in TLS v1.1 and up as per
2401 * Method 1 (6.2.3.2. in RFC4346 and RFC5246)
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002402 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002403 if( transform->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002404 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002405 if( f_rng == NULL )
2406 {
2407 MBEDTLS_SSL_DEBUG_MSG( 1, ( "No PRNG provided to encrypt_record routine" ) );
2408 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2409 }
2410
2411 if( rec->data_offset < transform->ivlen )
2412 {
2413 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2414 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2415 }
2416
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002417 /*
2418 * Generate IV
2419 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002420 ret = f_rng( p_rng, transform->iv_enc, transform->ivlen );
Paul Bakkera3d195c2011-11-27 21:07:34 +00002421 if( ret != 0 )
2422 return( ret );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002423
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002424 memcpy( data - transform->ivlen, transform->iv_enc,
2425 transform->ivlen );
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002426
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002427 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002428#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002429
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002430 MBEDTLS_SSL_DEBUG_MSG( 3, ( "before encrypt: msglen = %d, "
Paul Bakker2e11f7d2010-07-25 14:24:53 +00002431 "including %d bytes of IV and %d bytes of padding",
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002432 rec->data_len, transform->ivlen,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02002433 padlen + 1 ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002434
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002435 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_enc,
2436 transform->iv_enc,
2437 transform->ivlen,
2438 data, rec->data_len,
2439 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002440 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002441 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkercca5b812013-08-31 17:40:26 +02002442 return( ret );
2443 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002444
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002445 if( rec->data_len != olen )
Paul Bakkercca5b812013-08-31 17:40:26 +02002446 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002447 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2448 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkercca5b812013-08-31 17:40:26 +02002449 }
Paul Bakkerda02a7f2013-08-31 17:25:14 +02002450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002451#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1)
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002452 if( transform->minor_ver < MBEDTLS_SSL_MINOR_VERSION_2 )
Paul Bakkercca5b812013-08-31 17:40:26 +02002453 {
2454 /*
2455 * Save IV in SSL3 and TLS1
2456 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002457 memcpy( transform->iv_enc, transform->cipher_ctx_enc.iv,
2458 transform->ivlen );
Paul Bakker5121ce52009-01-03 21:22:43 +00002459 }
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002460 else
Paul Bakkercca5b812013-08-31 17:40:26 +02002461#endif
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002462 {
2463 data -= transform->ivlen;
2464 rec->data_offset -= transform->ivlen;
2465 rec->data_len += transform->ivlen;
2466 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002468#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002469 if( auth_done == 0 )
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002470 {
Hanno Becker3d8c9072018-01-05 16:24:22 +00002471 unsigned char mac[MBEDTLS_SSL_MAC_ADD];
2472
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002473 /*
2474 * MAC(MAC_write_key, seq_num +
2475 * TLSCipherText.type +
2476 * TLSCipherText.version +
Manuel Pégourié-Gonnard08558e52014-11-04 14:40:21 +01002477 * length_of( (IV +) ENC(...) ) +
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002478 * IV + // except for TLS 1.0
2479 * ENC(content + padding + padding_length));
2480 */
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002481
2482 if( post_avail < transform->maclen)
2483 {
2484 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Buffer provided for encrypted record not large enough" ) );
2485 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
2486 }
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002487
Hanno Beckercab87e62019-04-29 13:52:53 +01002488 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker1f10d762019-04-26 13:34:37 +01002489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002490 MBEDTLS_SSL_DEBUG_MSG( 3, ( "using encrypt then mac" ) );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002491 MBEDTLS_SSL_DEBUG_BUF( 4, "MAC'd meta-data", add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002492 add_data_len );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002493
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002494 mbedtls_md_hmac_update( &transform->md_ctx_enc, add_data,
Hanno Beckercab87e62019-04-29 13:52:53 +01002495 add_data_len );
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002496 mbedtls_md_hmac_update( &transform->md_ctx_enc,
2497 data, rec->data_len );
2498 mbedtls_md_hmac_finish( &transform->md_ctx_enc, mac );
2499 mbedtls_md_hmac_reset( &transform->md_ctx_enc );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002500
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002501 memcpy( data + rec->data_len, mac, transform->maclen );
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002502
Hanno Becker9eddaeb2017-12-27 21:37:21 +00002503 rec->data_len += transform->maclen;
2504 post_avail -= transform->maclen;
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002505 auth_done++;
Manuel Pégourié-Gonnard313d7962014-10-29 12:07:57 +01002506 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002507#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
Paul Bakker5121ce52009-01-03 21:22:43 +00002508 }
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002509 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002510#endif /* MBEDTLS_CIPHER_MODE_CBC &&
Markku-Juhani O. Saarinenc06e1012017-12-07 11:51:13 +00002511 ( MBEDTLS_AES_C || MBEDTLS_CAMELLIA_C || MBEDTLS_ARIA_C ) */
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002512 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002513 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2514 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardf7dc3782013-09-13 14:10:44 +02002515 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002516
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002517 /* Make extra sure authentication was performed, exactly once */
2518 if( auth_done != 1 )
2519 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002520 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2521 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002522 }
2523
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002524 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= encrypt buf" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00002525
2526 return( 0 );
2527}
2528
Hanno Beckera18d1322018-01-03 14:27:32 +00002529int mbedtls_ssl_decrypt_buf( mbedtls_ssl_context *ssl,
2530 mbedtls_ssl_transform *transform,
2531 mbedtls_record *rec )
Paul Bakker5121ce52009-01-03 21:22:43 +00002532{
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002533 size_t olen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002534 mbedtls_cipher_mode_t mode;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002535 int ret, auth_done = 0;
Hanno Becker52344c22018-01-03 15:24:20 +00002536#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Paul Bakker1e5369c2013-12-19 16:40:57 +01002537 size_t padlen = 0, correct = 1;
2538#endif
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002539 unsigned char* data;
Hanno Beckerc4a190b2019-05-08 18:15:21 +01002540 unsigned char add_data[13 + 1 + MBEDTLS_SSL_CID_LEN_MAX ];
Hanno Beckercab87e62019-04-29 13:52:53 +01002541 size_t add_data_len;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002542
Hanno Beckera18d1322018-01-03 14:27:32 +00002543#if !defined(MBEDTLS_DEBUG_C)
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002544 ((void) ssl);
2545#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00002546
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002547 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> decrypt buf" ) );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002548 if( transform == NULL )
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002549 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002550 MBEDTLS_SSL_DEBUG_MSG( 1, ( "no transform provided to decrypt_buf" ) );
2551 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2552 }
2553 if( rec == NULL ||
2554 rec->buf == NULL ||
2555 rec->buf_len < rec->data_offset ||
2556 rec->buf_len - rec->data_offset < rec->data_len )
2557 {
2558 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad record structure provided to decrypt_buf" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002559 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002560 }
2561
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002562 data = rec->buf + rec->data_offset;
2563 mode = mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_dec );
Paul Bakker5121ce52009-01-03 21:22:43 +00002564
Hanno Beckercab87e62019-04-29 13:52:53 +01002565#if defined(MBEDTLS_SSL_CID)
2566 /*
2567 * Match record's CID with incoming CID.
2568 */
2569
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002570 /* Uncomment this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002571 /* if( rec->cid_len != transform->in_cid_len || */
2572 /* memcmp( rec->cid, transform->in_cid, rec->cid_len ) != 0 ) */
2573 /* { */
2574 /* return( MBEDTLS_ERR_SSL_INVALID_RECORD ); */
2575 /* } */
Hanno Beckerf44e55d2019-04-30 16:56:40 +01002576
2577 /* Remove this once CID parsing is in place */
Hanno Beckercab87e62019-04-29 13:52:53 +01002578 rec->cid_len = transform->in_cid_len;
2579 memcpy( rec->cid, transform->in_cid, transform->in_cid_len );
2580 MBEDTLS_SSL_DEBUG_BUF( 3, "CID", rec->cid, rec->cid_len );
2581#endif /* MBEDTLS_SSL_CID */
2582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002583#if defined(MBEDTLS_ARC4_C) || defined(MBEDTLS_CIPHER_NULL_CIPHER)
2584 if( mode == MBEDTLS_MODE_STREAM )
Paul Bakker68884e32013-01-07 18:20:04 +01002585 {
2586 padlen = 0;
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002587 if( ( ret = mbedtls_cipher_crypt( &transform->cipher_ctx_dec,
2588 transform->iv_dec,
2589 transform->ivlen,
2590 data, rec->data_len,
2591 data, &olen ) ) != 0 )
Paul Bakker45125bc2013-09-04 16:47:11 +02002592 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002593 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_crypt", ret );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002594 return( ret );
2595 }
2596
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002597 if( rec->data_len != olen )
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002598 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002599 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2600 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakkerea6ad3f2013-09-02 14:57:01 +02002601 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002602 }
Paul Bakker68884e32013-01-07 18:20:04 +01002603 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002604#endif /* MBEDTLS_ARC4_C || MBEDTLS_CIPHER_NULL_CIPHER */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002605#if defined(MBEDTLS_GCM_C) || \
2606 defined(MBEDTLS_CCM_C) || \
2607 defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002608 if( mode == MBEDTLS_MODE_GCM ||
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002609 mode == MBEDTLS_MODE_CCM ||
2610 mode == MBEDTLS_MODE_CHACHAPOLY )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002611 {
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002612 unsigned char iv[12];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002613 size_t explicit_iv_len = transform->ivlen - transform->fixed_ivlen;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002614
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002615 /*
2616 * Compute and update sizes
2617 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002618 if( rec->data_len < explicit_iv_len + transform->taglen )
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002619 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002620 MBEDTLS_SSL_DEBUG_MSG( 1, ( "msglen (%d) < explicit_iv_len (%d) "
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002621 "+ taglen (%d)", rec->data_len,
2622 explicit_iv_len, transform->taglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002623 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnard0bcc4e12014-06-17 10:54:17 +02002624 }
Paul Bakker68884e32013-01-07 18:20:04 +01002625
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002626 /*
2627 * Prepare IV
2628 */
2629 if( transform->ivlen == 12 && transform->fixed_ivlen == 4 )
2630 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002631 /* GCM and CCM: fixed || explicit (transmitted) */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002632 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002633 memcpy( iv + transform->fixed_ivlen, data, 8 );
Paul Bakker68884e32013-01-07 18:20:04 +01002634
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002635 }
2636 else if( transform->ivlen == 12 && transform->fixed_ivlen == 12 )
2637 {
Manuel Pégourié-Gonnard8744a022018-07-11 12:30:40 +02002638 /* ChachaPoly: fixed XOR sequence number */
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002639 unsigned char i;
2640
2641 memcpy( iv, transform->iv_dec, transform->fixed_ivlen );
2642
2643 for( i = 0; i < 8; i++ )
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002644 iv[i+4] ^= rec->ctr[i];
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002645 }
2646 else
2647 {
2648 /* Reminder if we ever add an AEAD mode with a different size */
2649 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2650 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
2651 }
2652
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002653 data += explicit_iv_len;
2654 rec->data_offset += explicit_iv_len;
2655 rec->data_len -= explicit_iv_len + transform->taglen;
2656
Hanno Beckercab87e62019-04-29 13:52:53 +01002657 ssl_extract_add_data_from_record( add_data, &add_data_len, rec );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002658 MBEDTLS_SSL_DEBUG_BUF( 4, "additional data used for AEAD",
Hanno Beckercab87e62019-04-29 13:52:53 +01002659 add_data, add_data_len );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002660
2661 memcpy( transform->iv_dec + transform->fixed_ivlen,
2662 data - explicit_iv_len, explicit_iv_len );
2663
Manuel Pégourié-Gonnard2e58e8e2018-06-18 11:16:43 +02002664 MBEDTLS_SSL_DEBUG_BUF( 4, "IV used", iv, transform->ivlen );
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002665 MBEDTLS_SSL_DEBUG_BUF( 4, "TAG used", data + rec->data_len,
Hanno Beckere694c3e2017-12-27 21:34:08 +00002666 transform->taglen );
Paul Bakker68884e32013-01-07 18:20:04 +01002667
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002668
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002669 /*
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002670 * Decrypt and authenticate
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002671 */
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002672 if( ( ret = mbedtls_cipher_auth_decrypt( &transform->cipher_ctx_dec,
2673 iv, transform->ivlen,
Hanno Beckercab87e62019-04-29 13:52:53 +01002674 add_data, add_data_len,
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002675 data, rec->data_len,
2676 data, &olen,
2677 data + rec->data_len,
2678 transform->taglen ) ) != 0 )
Paul Bakkerca4ab492012-04-18 14:23:57 +00002679 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002680 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_cipher_auth_decrypt", ret );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002682 if( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED )
2683 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardde7bb442014-05-13 12:41:10 +02002684
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002685 return( ret );
2686 }
Manuel Pégourié-Gonnard352143f2015-01-13 10:59:51 +01002687 auth_done++;
Paul Bakkerca4ab492012-04-18 14:23:57 +00002688
Hanno Becker2e24c3b2017-12-27 21:28:58 +00002689 if( olen != rec->data_len )
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002690 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002691 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
2692 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnardd13a4092013-09-05 16:10:41 +02002693 }
Hanno Becker8b3eb5a2019-04-29 17:31:37 +01002694
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 Becker8b3eb5a2019-04-29 17:31:37 +01003120#if defined(MBEDTLS_SSL_CID)
3121 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 }
3128#endif /* MBEDTLS_SSL_CID */
3129
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",
3528 mbedtls_ssl_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
Paul Bakker05ef8352012-05-08 09:17:57 +00004134 ssl->out_hdr[0] = (unsigned char) ssl->out_msgtype;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004135 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004136 ssl->conf->transport, ssl->out_hdr + 1 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004137
Hanno Becker19859472018-08-06 09:40:20 +01004138 memcpy( ssl->out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004139 ssl->out_len[0] = (unsigned char)( len >> 8 );
4140 ssl->out_len[1] = (unsigned char)( len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004141
Paul Bakker48916f92012-09-16 19:57:18 +00004142 if( ssl->transform_out != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00004143 {
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004144 mbedtls_record rec;
4145
4146 rec.buf = ssl->out_iv;
4147 rec.buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN -
4148 ( ssl->out_iv - ssl->out_buf );
4149 rec.data_len = ssl->out_msglen;
4150 rec.data_offset = ssl->out_msg - rec.buf;
4151
4152 memcpy( &rec.ctr[0], ssl->out_ctr, 8 );
4153 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
4154 ssl->conf->transport, rec.ver );
4155 rec.type = ssl->out_msgtype;
4156
Hanno Becker43c24b82019-05-01 09:45:57 +01004157#if defined(MBEDTLS_SSL_CID)
4158 /* The CID is set by mbedtls_ssl_encrypt_buf(). */
Hanno Beckercab87e62019-04-29 13:52:53 +01004159 rec.cid_len = 0;
Hanno Becker43c24b82019-05-01 09:45:57 +01004160#endif /* MBEDTLS_SSL_CID */
Hanno Beckercab87e62019-04-29 13:52:53 +01004161
Hanno Beckera18d1322018-01-03 14:27:32 +00004162 if( ( ret = mbedtls_ssl_encrypt_buf( ssl, ssl->transform_out, &rec,
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004163 ssl->conf->f_rng, ssl->conf->p_rng ) ) != 0 )
Paul Bakker05ef8352012-05-08 09:17:57 +00004164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_encrypt_buf", ret );
Paul Bakker05ef8352012-05-08 09:17:57 +00004166 return( ret );
4167 }
4168
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004169 if( rec.data_offset != 0 )
4170 {
4171 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
4172 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4173 }
4174
Hanno Becker78f839d2019-03-14 12:56:23 +00004175 ssl->out_msglen = len = rec.data_len;
Hanno Becker9eddaeb2017-12-27 21:37:21 +00004176 ssl->out_len[0] = (unsigned char)( rec.data_len >> 8 );
4177 ssl->out_len[1] = (unsigned char)( rec.data_len );
Paul Bakker05ef8352012-05-08 09:17:57 +00004178 }
4179
Hanno Becker2b1e3542018-08-06 11:19:13 +01004180 protected_record_size = len + mbedtls_ssl_hdr_len( ssl );
4181
4182#if defined(MBEDTLS_SSL_PROTO_DTLS)
4183 /* In case of DTLS, double-check that we don't exceed
4184 * the remaining space in the datagram. */
4185 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4186 {
Hanno Becker554b0af2018-08-22 20:33:41 +01004187 ret = ssl_get_remaining_space_in_datagram( ssl );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004188 if( ret < 0 )
4189 return( ret );
4190
4191 if( protected_record_size > (size_t) ret )
4192 {
4193 /* Should never happen */
4194 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4195 }
4196 }
4197#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker05ef8352012-05-08 09:17:57 +00004198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004199 MBEDTLS_SSL_DEBUG_MSG( 3, ( "output record: msgtype = %d, "
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004200 "version = [%d:%d], msglen = %d",
4201 ssl->out_hdr[0], ssl->out_hdr[1],
4202 ssl->out_hdr[2], len ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00004203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004204 MBEDTLS_SSL_DEBUG_BUF( 4, "output record sent to network",
Hanno Beckerecbdf1c2018-08-28 09:53:54 +01004205 ssl->out_hdr, protected_record_size );
Hanno Becker2b1e3542018-08-06 11:19:13 +01004206
4207 ssl->out_left += protected_record_size;
4208 ssl->out_hdr += protected_record_size;
4209 ssl_update_out_pointers( ssl, ssl->transform_out );
4210
Hanno Becker04484622018-08-06 09:49:38 +01004211 for( i = 8; i > ssl_ep_len( ssl ); i-- )
4212 if( ++ssl->cur_out_ctr[i - 1] != 0 )
4213 break;
4214
4215 /* The loop goes to its end iff the counter is wrapping */
4216 if( i == ssl_ep_len( ssl ) )
4217 {
4218 MBEDTLS_SSL_DEBUG_MSG( 1, ( "outgoing message counter would wrap" ) );
4219 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
4220 }
Paul Bakker5121ce52009-01-03 21:22:43 +00004221 }
4222
Hanno Becker67bc7c32018-08-06 11:33:50 +01004223#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker47db8772018-08-21 13:32:13 +01004224 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4225 flush == SSL_DONT_FORCE_FLUSH )
Hanno Becker67bc7c32018-08-06 11:33:50 +01004226 {
Hanno Becker1f5a15d2018-08-21 13:31:31 +01004227 size_t remaining;
4228 ret = ssl_get_remaining_payload_in_datagram( ssl );
4229 if( ret < 0 )
4230 {
4231 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_get_remaining_payload_in_datagram",
4232 ret );
4233 return( ret );
4234 }
4235
4236 remaining = (size_t) ret;
Hanno Becker67bc7c32018-08-06 11:33:50 +01004237 if( remaining == 0 )
Hanno Beckerf0da6672018-08-28 09:55:10 +01004238 {
Hanno Becker67bc7c32018-08-06 11:33:50 +01004239 flush = SSL_FORCE_FLUSH;
Hanno Beckerf0da6672018-08-28 09:55:10 +01004240 }
Hanno Becker67bc7c32018-08-06 11:33:50 +01004241 else
4242 {
Hanno Becker513815a2018-08-20 11:56:09 +01004243 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Still %u bytes available in current datagram", (unsigned) remaining ) );
Hanno Becker67bc7c32018-08-06 11:33:50 +01004244 }
4245 }
4246#endif /* MBEDTLS_SSL_PROTO_DTLS */
4247
4248 if( ( flush == SSL_FORCE_FLUSH ) &&
4249 ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00004250 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004251 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00004252 return( ret );
4253 }
4254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004255 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write record" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004256
4257 return( 0 );
4258}
4259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckere25e3b72018-08-16 09:30:53 +01004261
4262static int ssl_hs_is_proper_fragment( mbedtls_ssl_context *ssl )
4263{
4264 if( ssl->in_msglen < ssl->in_hslen ||
4265 memcmp( ssl->in_msg + 6, "\0\0\0", 3 ) != 0 ||
4266 memcmp( ssl->in_msg + 9, ssl->in_msg + 1, 3 ) != 0 )
4267 {
4268 return( 1 );
4269 }
4270 return( 0 );
4271}
Hanno Becker44650b72018-08-16 12:51:11 +01004272
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004273static uint32_t ssl_get_hs_frag_len( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004274{
4275 return( ( ssl->in_msg[9] << 16 ) |
4276 ( ssl->in_msg[10] << 8 ) |
4277 ssl->in_msg[11] );
4278}
4279
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004280static uint32_t ssl_get_hs_frag_off( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004281{
4282 return( ( ssl->in_msg[6] << 16 ) |
4283 ( ssl->in_msg[7] << 8 ) |
4284 ssl->in_msg[8] );
4285}
4286
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004287static int ssl_check_hs_header( mbedtls_ssl_context const *ssl )
Hanno Becker44650b72018-08-16 12:51:11 +01004288{
4289 uint32_t msg_len, frag_off, frag_len;
4290
4291 msg_len = ssl_get_hs_total_len( ssl );
4292 frag_off = ssl_get_hs_frag_off( ssl );
4293 frag_len = ssl_get_hs_frag_len( ssl );
4294
4295 if( frag_off > msg_len )
4296 return( -1 );
4297
4298 if( frag_len > msg_len - frag_off )
4299 return( -1 );
4300
4301 if( frag_len + 12 > ssl->in_msglen )
4302 return( -1 );
4303
4304 return( 0 );
4305}
4306
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004307/*
4308 * Mark bits in bitmask (used for DTLS HS reassembly)
4309 */
4310static void ssl_bitmask_set( unsigned char *mask, size_t offset, size_t len )
4311{
4312 unsigned int start_bits, end_bits;
4313
4314 start_bits = 8 - ( offset % 8 );
4315 if( start_bits != 8 )
4316 {
4317 size_t first_byte_idx = offset / 8;
4318
Manuel Pégourié-Gonnardac030522014-09-02 14:23:40 +02004319 /* Special case */
4320 if( len <= start_bits )
4321 {
4322 for( ; len != 0; len-- )
4323 mask[first_byte_idx] |= 1 << ( start_bits - len );
4324
4325 /* Avoid potential issues with offset or len becoming invalid */
4326 return;
4327 }
4328
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004329 offset += start_bits; /* Now offset % 8 == 0 */
4330 len -= start_bits;
4331
4332 for( ; start_bits != 0; start_bits-- )
4333 mask[first_byte_idx] |= 1 << ( start_bits - 1 );
4334 }
4335
4336 end_bits = len % 8;
4337 if( end_bits != 0 )
4338 {
4339 size_t last_byte_idx = ( offset + len ) / 8;
4340
4341 len -= end_bits; /* Now len % 8 == 0 */
4342
4343 for( ; end_bits != 0; end_bits-- )
4344 mask[last_byte_idx] |= 1 << ( 8 - end_bits );
4345 }
4346
4347 memset( mask + offset / 8, 0xFF, len / 8 );
4348}
4349
4350/*
4351 * Check that bitmask is full
4352 */
4353static int ssl_bitmask_check( unsigned char *mask, size_t len )
4354{
4355 size_t i;
4356
4357 for( i = 0; i < len / 8; i++ )
4358 if( mask[i] != 0xFF )
4359 return( -1 );
4360
4361 for( i = 0; i < len % 8; i++ )
4362 if( ( mask[len / 8] & ( 1 << ( 7 - i ) ) ) == 0 )
4363 return( -1 );
4364
4365 return( 0 );
4366}
4367
Hanno Becker56e205e2018-08-16 09:06:12 +01004368/* msg_len does not include the handshake header */
Hanno Becker65dc8852018-08-23 09:40:49 +01004369static size_t ssl_get_reassembly_buffer_size( size_t msg_len,
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004370 unsigned add_bitmap )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004371{
Hanno Becker56e205e2018-08-16 09:06:12 +01004372 size_t alloc_len;
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004373
Hanno Becker56e205e2018-08-16 09:06:12 +01004374 alloc_len = 12; /* Handshake header */
4375 alloc_len += msg_len; /* Content buffer */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004376
Hanno Beckerd07df862018-08-16 09:14:58 +01004377 if( add_bitmap )
4378 alloc_len += msg_len / 8 + ( msg_len % 8 != 0 ); /* Bitmap */
Manuel Pégourié-Gonnard502bf302014-08-20 13:12:58 +02004379
Hanno Becker2a97b0e2018-08-21 15:47:49 +01004380 return( alloc_len );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004381}
Hanno Becker56e205e2018-08-16 09:06:12 +01004382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004383#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004384
Hanno Beckercd9dcda2018-08-28 17:18:56 +01004385static uint32_t ssl_get_hs_total_len( mbedtls_ssl_context const *ssl )
Hanno Becker12555c62018-08-16 12:47:53 +01004386{
4387 return( ( ssl->in_msg[1] << 16 ) |
4388 ( ssl->in_msg[2] << 8 ) |
4389 ssl->in_msg[3] );
4390}
Hanno Beckere25e3b72018-08-16 09:30:53 +01004391
Simon Butcher99000142016-10-13 17:21:01 +01004392int mbedtls_ssl_prepare_handshake_record( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004393{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004394 if( ssl->in_msglen < mbedtls_ssl_hs_hdr_len( ssl ) )
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004395 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004396 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake message too short: %d",
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004397 ssl->in_msglen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004398 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnard9d1d7192014-09-03 11:01:14 +02004399 }
4400
Hanno Becker12555c62018-08-16 12:47:53 +01004401 ssl->in_hslen = mbedtls_ssl_hs_hdr_len( ssl ) + ssl_get_hs_total_len( ssl );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004402
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004403 MBEDTLS_SSL_DEBUG_MSG( 3, ( "handshake message: msglen ="
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004404 " %d, type = %d, hslen = %d",
Manuel Pégourié-Gonnardce441b32014-02-18 17:40:52 +01004405 ssl->in_msglen, ssl->in_msg[0], ssl->in_hslen ) );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004406
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004408 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004409 {
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004410 int ret;
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004411 unsigned int recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004412
Hanno Becker44650b72018-08-16 12:51:11 +01004413 if( ssl_check_hs_header( ssl ) != 0 )
4414 {
4415 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid handshake header" ) );
4416 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4417 }
4418
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004419 if( ssl->handshake != NULL &&
Hanno Beckerc76c6192017-06-06 10:03:17 +01004420 ( ( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER &&
4421 recv_msg_seq != ssl->handshake->in_msg_seq ) ||
4422 ( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4423 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO ) ) )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004424 {
Hanno Becker9e1ec222018-08-15 15:54:43 +01004425 if( recv_msg_seq > ssl->handshake->in_msg_seq )
4426 {
4427 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received future handshake message of sequence number %u (next %u)",
4428 recv_msg_seq,
4429 ssl->handshake->in_msg_seq ) );
4430 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4431 }
4432
Manuel Pégourié-Gonnardfc572dd2014-10-09 17:56:57 +02004433 /* Retransmit only on last message from previous flight, to avoid
4434 * too many retransmissions.
4435 * Besides, No sane server ever retransmits HelloVerifyRequest */
4436 if( recv_msg_seq == ssl->handshake->in_flight_start_seq - 1 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004437 ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004438 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004439 MBEDTLS_SSL_DEBUG_MSG( 2, ( "received message from last flight, "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004440 "message_seq = %d, start_of_flight = %d",
4441 recv_msg_seq,
4442 ssl->handshake->in_flight_start_seq ) );
4443
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004444 if( ( ret = mbedtls_ssl_resend( ssl ) ) != 0 )
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004445 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004446 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_resend", ret );
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004447 return( ret );
4448 }
4449 }
4450 else
4451 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004452 MBEDTLS_SSL_DEBUG_MSG( 2, ( "dropping out-of-sequence message: "
Manuel Pégourié-Gonnard6a2bdfa2014-09-19 21:18:23 +02004453 "message_seq = %d, expected = %d",
4454 recv_msg_seq,
4455 ssl->handshake->in_msg_seq ) );
4456 }
4457
Hanno Becker90333da2017-10-10 11:27:13 +01004458 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004459 }
4460 /* Wait until message completion to increment in_msg_seq */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004461
Hanno Becker6d97ef52018-08-16 13:09:04 +01004462 /* Message reassembly is handled alongside buffering of future
4463 * messages; the commonality is that both handshake fragments and
Hanno Becker83ab41c2018-08-28 17:19:38 +01004464 * future messages cannot be forwarded immediately to the
Hanno Becker6d97ef52018-08-16 13:09:04 +01004465 * handshake logic layer. */
Hanno Beckere25e3b72018-08-16 09:30:53 +01004466 if( ssl_hs_is_proper_fragment( ssl ) == 1 )
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004467 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004468 MBEDTLS_SSL_DEBUG_MSG( 2, ( "found fragmented DTLS handshake message" ) );
Hanno Becker6d97ef52018-08-16 13:09:04 +01004469 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004470 }
4471 }
4472 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004473#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnarded79a4b2014-08-20 10:43:01 +02004474 /* With TLS we don't handle fragmentation (for now) */
4475 if( ssl->in_msglen < ssl->in_hslen )
4476 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004477 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLS handshake fragmentation not supported" ) );
4478 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004479 }
4480
Simon Butcher99000142016-10-13 17:21:01 +01004481 return( 0 );
4482}
4483
4484void mbedtls_ssl_update_handshake_status( mbedtls_ssl_context *ssl )
4485{
Hanno Becker0271f962018-08-16 13:23:47 +01004486 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Simon Butcher99000142016-10-13 17:21:01 +01004487
Hanno Becker0271f962018-08-16 13:23:47 +01004488 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER && hs != NULL )
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004489 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004490 ssl->handshake->update_checksum( ssl, ssl->in_msg, ssl->in_hslen );
Manuel Pégourié-Gonnard14bf7062015-06-23 14:07:13 +02004491 }
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004492
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004493 /* Handshake message is complete, increment counter */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004494#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004495 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004496 ssl->handshake != NULL )
4497 {
Hanno Becker0271f962018-08-16 13:23:47 +01004498 unsigned offset;
4499 mbedtls_ssl_hs_buffer *hs_buf;
Hanno Beckere25e3b72018-08-16 09:30:53 +01004500
Hanno Becker0271f962018-08-16 13:23:47 +01004501 /* Increment handshake sequence number */
4502 hs->in_msg_seq++;
4503
4504 /*
4505 * Clear up handshake buffering and reassembly structure.
4506 */
4507
4508 /* Free first entry */
Hanno Beckere605b192018-08-21 15:59:07 +01004509 ssl_buffering_free_slot( ssl, 0 );
Hanno Becker0271f962018-08-16 13:23:47 +01004510
4511 /* Shift all other entries */
Hanno Beckere605b192018-08-21 15:59:07 +01004512 for( offset = 0, hs_buf = &hs->buffering.hs[0];
4513 offset + 1 < MBEDTLS_SSL_MAX_BUFFERED_HS;
Hanno Becker0271f962018-08-16 13:23:47 +01004514 offset++, hs_buf++ )
4515 {
4516 *hs_buf = *(hs_buf + 1);
4517 }
4518
4519 /* Create a fresh last entry */
4520 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004521 }
4522#endif
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01004523}
4524
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004525/*
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004526 * DTLS anti-replay: RFC 6347 4.1.2.6
4527 *
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004528 * in_window is a field of bits numbered from 0 (lsb) to 63 (msb).
4529 * Bit n is set iff record number in_window_top - n has been seen.
4530 *
4531 * Usually, in_window_top is the last record number seen and the lsb of
4532 * in_window is set. The only exception is the initial state (record number 0
4533 * not seen yet).
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004534 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004535#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4536static void ssl_dtls_replay_reset( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004537{
4538 ssl->in_window_top = 0;
4539 ssl->in_window = 0;
4540}
4541
4542static inline uint64_t ssl_load_six_bytes( unsigned char *buf )
4543{
4544 return( ( (uint64_t) buf[0] << 40 ) |
4545 ( (uint64_t) buf[1] << 32 ) |
4546 ( (uint64_t) buf[2] << 24 ) |
4547 ( (uint64_t) buf[3] << 16 ) |
4548 ( (uint64_t) buf[4] << 8 ) |
4549 ( (uint64_t) buf[5] ) );
4550}
4551
4552/*
4553 * Return 0 if sequence number is acceptable, -1 otherwise
4554 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004555int mbedtls_ssl_dtls_replay_check( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004556{
4557 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4558 uint64_t bit;
4559
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004560 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004561 return( 0 );
4562
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004563 if( rec_seqnum > ssl->in_window_top )
4564 return( 0 );
4565
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004566 bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004567
4568 if( bit >= 64 )
4569 return( -1 );
4570
4571 if( ( ssl->in_window & ( (uint64_t) 1 << bit ) ) != 0 )
4572 return( -1 );
4573
4574 return( 0 );
4575}
4576
4577/*
4578 * Update replay window on new validated record
4579 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004580void mbedtls_ssl_dtls_replay_update( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004581{
4582 uint64_t rec_seqnum = ssl_load_six_bytes( ssl->in_ctr + 2 );
4583
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004584 if( ssl->conf->anti_replay == MBEDTLS_SSL_ANTI_REPLAY_DISABLED )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02004585 return;
4586
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004587 if( rec_seqnum > ssl->in_window_top )
4588 {
4589 /* Update window_top and the contents of the window */
4590 uint64_t shift = rec_seqnum - ssl->in_window_top;
4591
4592 if( shift >= 64 )
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004593 ssl->in_window = 1;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004594 else
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004595 {
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004596 ssl->in_window <<= shift;
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004597 ssl->in_window |= 1;
4598 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004599
4600 ssl->in_window_top = rec_seqnum;
4601 }
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004602 else
4603 {
4604 /* Mark that number as seen in the current window */
Manuel Pégourié-Gonnard4956fd72014-09-24 11:13:44 +02004605 uint64_t bit = ssl->in_window_top - rec_seqnum;
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004606
4607 if( bit < 64 ) /* Always true, but be extra sure */
4608 ssl->in_window |= (uint64_t) 1 << bit;
4609 }
4610}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004611#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004612
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004613#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004614/* Forward declaration */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02004615static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial );
4616
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004617/*
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004618 * Without any SSL context, check if a datagram looks like a ClientHello with
4619 * a valid cookie, and if it doesn't, generate a HelloVerifyRequest message.
Simon Butcher0789aed2015-09-11 17:15:17 +01004620 * Both input and output include full DTLS headers.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004621 *
4622 * - if cookie is valid, return 0
4623 * - if ClientHello looks superficially valid but cookie is not,
4624 * fill obuf and set olen, then
4625 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
4626 * - otherwise return a specific error code
4627 */
4628static int ssl_check_dtls_clihlo_cookie(
4629 mbedtls_ssl_cookie_write_t *f_cookie_write,
4630 mbedtls_ssl_cookie_check_t *f_cookie_check,
4631 void *p_cookie,
4632 const unsigned char *cli_id, size_t cli_id_len,
4633 const unsigned char *in, size_t in_len,
4634 unsigned char *obuf, size_t buf_len, size_t *olen )
4635{
4636 size_t sid_len, cookie_len;
4637 unsigned char *p;
4638
4639 if( f_cookie_write == NULL || f_cookie_check == NULL )
4640 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
4641
4642 /*
4643 * Structure of ClientHello with record and handshake headers,
4644 * and expected values. We don't need to check a lot, more checks will be
4645 * done when actually parsing the ClientHello - skipping those checks
4646 * avoids code duplication and does not make cookie forging any easier.
4647 *
4648 * 0-0 ContentType type; copied, must be handshake
4649 * 1-2 ProtocolVersion version; copied
4650 * 3-4 uint16 epoch; copied, must be 0
4651 * 5-10 uint48 sequence_number; copied
4652 * 11-12 uint16 length; (ignored)
4653 *
4654 * 13-13 HandshakeType msg_type; (ignored)
4655 * 14-16 uint24 length; (ignored)
4656 * 17-18 uint16 message_seq; copied
4657 * 19-21 uint24 fragment_offset; copied, must be 0
4658 * 22-24 uint24 fragment_length; (ignored)
4659 *
4660 * 25-26 ProtocolVersion client_version; (ignored)
4661 * 27-58 Random random; (ignored)
4662 * 59-xx SessionID session_id; 1 byte len + sid_len content
4663 * 60+ opaque cookie<0..2^8-1>; 1 byte len + content
4664 * ...
4665 *
4666 * Minimum length is 61 bytes.
4667 */
4668 if( in_len < 61 ||
4669 in[0] != MBEDTLS_SSL_MSG_HANDSHAKE ||
4670 in[3] != 0 || in[4] != 0 ||
4671 in[19] != 0 || in[20] != 0 || in[21] != 0 )
4672 {
4673 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4674 }
4675
4676 sid_len = in[59];
4677 if( sid_len > in_len - 61 )
4678 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4679
4680 cookie_len = in[60 + sid_len];
4681 if( cookie_len > in_len - 60 )
4682 return( MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO );
4683
4684 if( f_cookie_check( p_cookie, in + sid_len + 61, cookie_len,
4685 cli_id, cli_id_len ) == 0 )
4686 {
4687 /* Valid cookie */
4688 return( 0 );
4689 }
4690
4691 /*
4692 * If we get here, we've got an invalid cookie, let's prepare HVR.
4693 *
4694 * 0-0 ContentType type; copied
4695 * 1-2 ProtocolVersion version; copied
4696 * 3-4 uint16 epoch; copied
4697 * 5-10 uint48 sequence_number; copied
4698 * 11-12 uint16 length; olen - 13
4699 *
4700 * 13-13 HandshakeType msg_type; hello_verify_request
4701 * 14-16 uint24 length; olen - 25
4702 * 17-18 uint16 message_seq; copied
4703 * 19-21 uint24 fragment_offset; copied
4704 * 22-24 uint24 fragment_length; olen - 25
4705 *
4706 * 25-26 ProtocolVersion server_version; 0xfe 0xff
4707 * 27-27 opaque cookie<0..2^8-1>; cookie_len = olen - 27, cookie
4708 *
4709 * Minimum length is 28.
4710 */
4711 if( buf_len < 28 )
4712 return( MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL );
4713
4714 /* Copy most fields and adapt others */
4715 memcpy( obuf, in, 25 );
4716 obuf[13] = MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST;
4717 obuf[25] = 0xfe;
4718 obuf[26] = 0xff;
4719
4720 /* Generate and write actual cookie */
4721 p = obuf + 28;
4722 if( f_cookie_write( p_cookie,
4723 &p, obuf + buf_len, cli_id, cli_id_len ) != 0 )
4724 {
4725 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
4726 }
4727
4728 *olen = p - obuf;
4729
4730 /* Go back and fill length fields */
4731 obuf[27] = (unsigned char)( *olen - 28 );
4732
4733 obuf[14] = obuf[22] = (unsigned char)( ( *olen - 25 ) >> 16 );
4734 obuf[15] = obuf[23] = (unsigned char)( ( *olen - 25 ) >> 8 );
4735 obuf[16] = obuf[24] = (unsigned char)( ( *olen - 25 ) );
4736
4737 obuf[11] = (unsigned char)( ( *olen - 13 ) >> 8 );
4738 obuf[12] = (unsigned char)( ( *olen - 13 ) );
4739
4740 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
4741}
4742
4743/*
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004744 * Handle possible client reconnect with the same UDP quadruplet
4745 * (RFC 6347 Section 4.2.8).
4746 *
4747 * Called by ssl_parse_record_header() in case we receive an epoch 0 record
4748 * that looks like a ClientHello.
4749 *
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004750 * - if the input looks like a ClientHello without cookies,
4751 * send back HelloVerifyRequest, then
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004752 * return MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004753 * - if the input looks like a ClientHello with a valid cookie,
4754 * reset the session of the current context, and
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02004755 * return MBEDTLS_ERR_SSL_CLIENT_RECONNECT
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004756 * - if anything goes wrong, return a specific error code
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004757 *
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004758 * mbedtls_ssl_read_record() will ignore the record if anything else than
Simon Butcherd0bf6a32015-09-11 17:34:49 +01004759 * MBEDTLS_ERR_SSL_CLIENT_RECONNECT or 0 is returned, although this function
4760 * cannot not return 0.
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004761 */
4762static int ssl_handle_possible_reconnect( mbedtls_ssl_context *ssl )
4763{
4764 int ret;
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004765 size_t len;
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004766
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004767 ret = ssl_check_dtls_clihlo_cookie(
4768 ssl->conf->f_cookie_write,
4769 ssl->conf->f_cookie_check,
4770 ssl->conf->p_cookie,
4771 ssl->cli_id, ssl->cli_id_len,
4772 ssl->in_buf, ssl->in_left,
Angus Grattond8213d02016-05-25 20:56:48 +10004773 ssl->out_buf, MBEDTLS_SSL_OUT_CONTENT_LEN, &len );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004774
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004775 MBEDTLS_SSL_DEBUG_RET( 2, "ssl_check_dtls_clihlo_cookie", ret );
4776
4777 if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004778 {
Brian J Murray1903fb32016-11-06 04:45:15 -08004779 /* Don't check write errors as we can't do anything here.
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004780 * If the error is permanent we'll catch it later,
4781 * if it's not, then hopefully it'll work next time. */
4782 (void) ssl->f_send( ssl->p_bio, ssl->out_buf, len );
4783
4784 return( MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004785 }
4786
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004787 if( ret == 0 )
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004788 {
Manuel Pégourié-Gonnard62c74bb2015-09-08 17:50:29 +02004789 /* Got a valid cookie, partially reset context */
4790 if( ( ret = ssl_session_reset_int( ssl, 1 ) ) != 0 )
4791 {
4792 MBEDTLS_SSL_DEBUG_RET( 1, "reset", ret );
4793 return( ret );
4794 }
4795
4796 return( MBEDTLS_ERR_SSL_CLIENT_RECONNECT );
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004797 }
4798
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004799 return( ret );
4800}
Manuel Pégourié-Gonnardddfe5d22015-09-09 12:46:16 +02004801#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard11331fc2015-09-08 10:30:55 +02004802
Manuel Pégourié-Gonnard7a7e1402014-09-24 10:52:58 +02004803/*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004804 * ContentType type;
4805 * ProtocolVersion version;
4806 * uint16 epoch; // DTLS only
4807 * uint48 sequence_number; // DTLS only
4808 * uint16 length;
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004809 *
4810 * Return 0 if header looks sane (and, for DTLS, the record is expected)
Simon Butcher207990d2015-12-16 01:51:30 +00004811 * MBEDTLS_ERR_SSL_INVALID_RECORD if the header looks bad,
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004812 * MBEDTLS_ERR_SSL_UNEXPECTED_RECORD (DTLS only) if sane but unexpected.
4813 *
4814 * With DTLS, mbedtls_ssl_read_record() will:
Simon Butcher207990d2015-12-16 01:51:30 +00004815 * 1. proceed with the record if this function returns 0
4816 * 2. drop only the current record if this function returns UNEXPECTED_RECORD
4817 * 3. return CLIENT_RECONNECT if this function return that value
4818 * 4. drop the whole datagram if this function returns anything else.
4819 * Point 2 is needed when the peer is resending, and we have already received
4820 * the first record from a datagram but are still waiting for the others.
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02004821 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004822static int ssl_parse_record_header( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00004823{
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004824 int major_ver, minor_ver;
Paul Bakker5121ce52009-01-03 21:22:43 +00004825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004826 MBEDTLS_SSL_DEBUG_BUF( 4, "input record header", ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) );
Manuel Pégourié-Gonnard64dffc52014-09-02 13:39:16 +02004827
Paul Bakker5121ce52009-01-03 21:22:43 +00004828 ssl->in_msgtype = ssl->in_hdr[0];
Manuel Pégourié-Gonnard507e1e42014-02-13 11:17:34 +01004829 ssl->in_msglen = ( ssl->in_len[0] << 8 ) | ssl->in_len[1];
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004830 mbedtls_ssl_read_version( &major_ver, &minor_ver, ssl->conf->transport, ssl->in_hdr + 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00004831
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004832 MBEDTLS_SSL_DEBUG_MSG( 3, ( "input record: msgtype = %d, "
Paul Bakker5121ce52009-01-03 21:22:43 +00004833 "version = [%d:%d], msglen = %d",
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004834 ssl->in_msgtype,
4835 major_ver, minor_ver, ssl->in_msglen ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00004836
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004837 /* Check record type */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004838 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE &&
4839 ssl->in_msgtype != MBEDTLS_SSL_MSG_ALERT &&
4840 ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC &&
4841 ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004842 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004843 MBEDTLS_SSL_DEBUG_MSG( 1, ( "unknown record type" ) );
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004844
4845#if defined(MBEDTLS_SSL_PROTO_DTLS)
Andres Amaya Garcia01692532017-06-28 09:26:46 +01004846 /* Silently ignore invalid DTLS records as recommended by RFC 6347
4847 * Section 4.1.2.7 */
Andres Amaya Garcia2fad94b2017-06-26 15:11:59 +01004848 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4849#endif /* MBEDTLS_SSL_PROTO_DTLS */
4850 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
4851 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
4852
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004853 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004854 }
4855
4856 /* Check version */
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +01004857 if( major_ver != ssl->major_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004858 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004859 MBEDTLS_SSL_DEBUG_MSG( 1, ( "major version mismatch" ) );
4860 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 }
4862
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02004863 if( minor_ver > ssl->conf->max_minor_ver )
Paul Bakker5121ce52009-01-03 21:22:43 +00004864 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004865 MBEDTLS_SSL_DEBUG_MSG( 1, ( "minor version mismatch" ) );
4866 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00004867 }
4868
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004869 /* Check length against the size of our buffer */
Angus Grattond8213d02016-05-25 20:56:48 +10004870 if( ssl->in_msglen > MBEDTLS_SSL_IN_BUFFER_LEN
Manuel Pégourié-Gonnardedcbe542014-08-11 19:27:24 +02004871 - (size_t)( ssl->in_msg - ssl->in_buf ) )
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004873 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4874 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker1a1fbba2014-04-30 14:38:05 +02004875 }
4876
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004877 /*
Hanno Becker52c6dc62017-05-26 16:07:36 +01004878 * DTLS-related tests.
4879 * Check epoch before checking length constraint because
4880 * the latter varies with the epoch. E.g., if a ChangeCipherSpec
4881 * message gets duplicated before the corresponding Finished message,
4882 * the second ChangeCipherSpec should be discarded because it belongs
4883 * to an old epoch, but not because its length is shorter than
4884 * the minimum record length for packets using the new record transform.
4885 * Note that these two kinds of failures are handled differently,
4886 * as an unexpected record is silently skipped but an invalid
4887 * record leads to the entire datagram being dropped.
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004888 */
4889#if defined(MBEDTLS_SSL_PROTO_DTLS)
4890 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
4891 {
4892 unsigned int rec_epoch = ( ssl->in_ctr[0] << 8 ) | ssl->in_ctr[1];
4893
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004894 /* Check epoch (and sequence number) with DTLS */
4895 if( rec_epoch != ssl->in_epoch )
4896 {
4897 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record from another epoch: "
4898 "expected %d, received %d",
4899 ssl->in_epoch, rec_epoch ) );
4900
4901#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
4902 /*
4903 * Check for an epoch 0 ClientHello. We can't use in_msg here to
4904 * access the first byte of record content (handshake type), as we
4905 * have an active transform (possibly iv_len != 0), so use the
4906 * fact that the record header len is 13 instead.
4907 */
4908 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
4909 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER &&
4910 rec_epoch == 0 &&
4911 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
4912 ssl->in_left > 13 &&
4913 ssl->in_buf[13] == MBEDTLS_SSL_HS_CLIENT_HELLO )
4914 {
4915 MBEDTLS_SSL_DEBUG_MSG( 1, ( "possible client reconnect "
4916 "from the same port" ) );
4917 return( ssl_handle_possible_reconnect( ssl ) );
4918 }
4919 else
4920#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
Hanno Becker5f066e72018-08-16 14:56:31 +01004921 {
4922 /* Consider buffering the record. */
4923 if( rec_epoch == (unsigned int) ssl->in_epoch + 1 )
4924 {
4925 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Consider record for buffering" ) );
4926 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
4927 }
4928
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004929 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
Hanno Becker5f066e72018-08-16 14:56:31 +01004930 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004931 }
4932
4933#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4934 /* Replay detection only works for the current epoch */
4935 if( rec_epoch == ssl->in_epoch &&
4936 mbedtls_ssl_dtls_replay_check( ssl ) != 0 )
4937 {
4938 MBEDTLS_SSL_DEBUG_MSG( 1, ( "replayed record" ) );
4939 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4940 }
4941#endif
Hanno Becker52c6dc62017-05-26 16:07:36 +01004942
Hanno Becker52c6dc62017-05-26 16:07:36 +01004943 /* Drop unexpected ApplicationData records,
4944 * except at the beginning of renegotiations */
4945 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA &&
4946 ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER
4947#if defined(MBEDTLS_SSL_RENEGOTIATION)
4948 && ! ( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS &&
4949 ssl->state == MBEDTLS_SSL_SERVER_HELLO )
4950#endif
4951 )
4952 {
4953 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping unexpected ApplicationData" ) );
4954 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
4955 }
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01004956 }
4957#endif /* MBEDTLS_SSL_PROTO_DTLS */
4958
Hanno Becker52c6dc62017-05-26 16:07:36 +01004959
4960 /* Check length against bounds of the current transform and version */
4961 if( ssl->transform_in == NULL )
4962 {
4963 if( ssl->in_msglen < 1 ||
Angus Grattond8213d02016-05-25 20:56:48 +10004964 ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004965 {
4966 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4967 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4968 }
4969 }
4970 else
4971 {
4972 if( ssl->in_msglen < ssl->transform_in->minlen )
4973 {
4974 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4975 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4976 }
4977
4978#if defined(MBEDTLS_SSL_PROTO_SSL3)
4979 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
Angus Grattond8213d02016-05-25 20:56:48 +10004980 ssl->in_msglen > ssl->transform_in->minlen + MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004981 {
4982 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4983 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4984 }
4985#endif
4986#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
4987 defined(MBEDTLS_SSL_PROTO_TLS1_2)
4988 /*
4989 * TLS encrypted messages can have up to 256 bytes of padding
4990 */
4991 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 &&
4992 ssl->in_msglen > ssl->transform_in->minlen +
Angus Grattond8213d02016-05-25 20:56:48 +10004993 MBEDTLS_SSL_IN_CONTENT_LEN + 256 )
Hanno Becker52c6dc62017-05-26 16:07:36 +01004994 {
4995 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
4996 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
4997 }
4998#endif
4999 }
5000
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005001 return( 0 );
5002}
Paul Bakker5121ce52009-01-03 21:22:43 +00005003
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005004/*
5005 * If applicable, decrypt (and decompress) record content
5006 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005007static int ssl_prepare_record_content( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005008{
5009 int ret, done = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02005010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005011 MBEDTLS_SSL_DEBUG_BUF( 4, "input record from network",
5012 ssl->in_hdr, mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen );
Paul Bakker5121ce52009-01-03 21:22:43 +00005013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005014#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
5015 if( mbedtls_ssl_hw_record_read != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00005016 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005017 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_read()" ) );
Paul Bakker05ef8352012-05-08 09:17:57 +00005018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005019 ret = mbedtls_ssl_hw_record_read( ssl );
5020 if( ret != 0 && ret != MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH )
Paul Bakker05ef8352012-05-08 09:17:57 +00005021 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_read", ret );
5023 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker05ef8352012-05-08 09:17:57 +00005024 }
Paul Bakkerc7878112012-12-19 14:41:14 +01005025
5026 if( ret == 0 )
5027 done = 1;
Paul Bakker05ef8352012-05-08 09:17:57 +00005028 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005029#endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
Paul Bakker48916f92012-09-16 19:57:18 +00005030 if( !done && ssl->transform_in != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00005031 {
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005032 mbedtls_record rec;
5033
5034 rec.buf = ssl->in_iv;
5035 rec.buf_len = MBEDTLS_SSL_IN_BUFFER_LEN
5036 - ( ssl->in_iv - ssl->in_buf );
5037 rec.data_len = ssl->in_msglen;
5038 rec.data_offset = 0;
5039
5040 memcpy( &rec.ctr[0], ssl->in_ctr, 8 );
5041 mbedtls_ssl_write_version( ssl->major_ver, ssl->minor_ver,
5042 ssl->conf->transport, rec.ver );
5043 rec.type = ssl->in_msgtype;
Hanno Beckera18d1322018-01-03 14:27:32 +00005044 if( ( ret = mbedtls_ssl_decrypt_buf( ssl, ssl->transform_in,
5045 &rec ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00005046 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005047 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decrypt_buf", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00005048 return( ret );
5049 }
5050
Hanno Becker29800d22018-08-07 14:30:18 +01005051 if( ssl->in_iv + rec.data_offset != ssl->in_msg )
5052 {
5053 /* Should never happen */
5054 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5055 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005056
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005057 ssl->in_msglen = rec.data_len;
5058 ssl->in_len[0] = (unsigned char)( rec.data_len >> 8 );
5059 ssl->in_len[1] = (unsigned char)( rec.data_len );
5060
Hanno Becker1c0c37f2018-08-07 14:29:29 +01005061 MBEDTLS_SSL_DEBUG_BUF( 4, "input payload after decrypt",
5062 ssl->in_msg, ssl->in_msglen );
5063
Angus Grattond8213d02016-05-25 20:56:48 +10005064 if( ssl->in_msglen > MBEDTLS_SSL_IN_CONTENT_LEN )
Paul Bakker5121ce52009-01-03 21:22:43 +00005065 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005066 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad message length" ) );
5067 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Paul Bakker5121ce52009-01-03 21:22:43 +00005068 }
Hanno Becker2e24c3b2017-12-27 21:28:58 +00005069 else if( ssl->in_msglen == 0 )
5070 {
5071#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5072 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_3
5073 && ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
5074 {
5075 /* TLS v1.2 explicitly disallows zero-length messages which are not application data */
5076 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid zero-length message type: %d", ssl->in_msgtype ) );
5077 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5078 }
5079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5080
5081 ssl->nb_zero++;
5082
5083 /*
5084 * Three or more empty messages may be a DoS attack
5085 * (excessive CPU consumption).
5086 */
5087 if( ssl->nb_zero > 3 )
5088 {
5089 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received four consecutive empty "
5090 "messages, possible DoS attack" ) );
5091 /* Q: Is that the right error code? */
5092 return( MBEDTLS_ERR_SSL_INVALID_MAC );
5093 }
5094 }
5095 else
5096 ssl->nb_zero = 0;
5097
5098#if defined(MBEDTLS_SSL_PROTO_DTLS)
5099 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5100 {
5101 ; /* in_ctr read from peer, not maintained internally */
5102 }
5103 else
5104#endif
5105 {
5106 unsigned i;
5107 for( i = 8; i > ssl_ep_len( ssl ); i-- )
5108 if( ++ssl->in_ctr[i - 1] != 0 )
5109 break;
5110
5111 /* The loop goes to its end iff the counter is wrapping */
5112 if( i == ssl_ep_len( ssl ) )
5113 {
5114 MBEDTLS_SSL_DEBUG_MSG( 1, ( "incoming message counter would wrap" ) );
5115 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
5116 }
5117 }
5118
Paul Bakker5121ce52009-01-03 21:22:43 +00005119 }
5120
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005121#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +00005122 if( ssl->transform_in != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005123 ssl->session_in->compression == MBEDTLS_SSL_COMPRESS_DEFLATE )
Paul Bakker2770fbd2012-07-03 13:30:23 +00005124 {
5125 if( ( ret = ssl_decompress_buf( ssl ) ) != 0 )
5126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005127 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_decompress_buf", ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00005128 return( ret );
5129 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00005130 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005131#endif /* MBEDTLS_ZLIB_SUPPORT */
Paul Bakker2770fbd2012-07-03 13:30:23 +00005132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005133#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005134 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005135 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005136 mbedtls_ssl_dtls_replay_update( ssl );
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02005137 }
5138#endif
5139
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005140 return( 0 );
5141}
5142
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005143static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005144
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005145/*
5146 * Read a record.
5147 *
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02005148 * Silently ignore non-fatal alert (and for DTLS, invalid records as well,
5149 * RFC 6347 4.1.2.7) and continue reading until a valid record is found.
5150 *
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005151 */
Hanno Becker1097b342018-08-15 14:09:41 +01005152
5153/* Helper functions for mbedtls_ssl_read_record(). */
5154static int ssl_consume_current_message( mbedtls_ssl_context *ssl );
Hanno Beckere74d5562018-08-15 14:26:08 +01005155static int ssl_get_next_record( mbedtls_ssl_context *ssl );
5156static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl );
Hanno Becker4162b112018-08-15 14:05:04 +01005157
Hanno Becker327c93b2018-08-15 13:56:18 +01005158int mbedtls_ssl_read_record( mbedtls_ssl_context *ssl,
Hanno Becker3a0aad12018-08-20 09:44:02 +01005159 unsigned update_hs_digest )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005160{
5161 int ret;
5162
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005163 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read record" ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005164
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005165 if( ssl->keep_current_message == 0 )
5166 {
5167 do {
Simon Butcher99000142016-10-13 17:21:01 +01005168
Hanno Becker26994592018-08-15 14:14:59 +01005169 ret = ssl_consume_current_message( ssl );
Hanno Becker90333da2017-10-10 11:27:13 +01005170 if( ret != 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005171 return( ret );
Hanno Becker26994592018-08-15 14:14:59 +01005172
Hanno Beckere74d5562018-08-15 14:26:08 +01005173 if( ssl_record_is_in_progress( ssl ) == 0 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005174 {
Hanno Becker40f50842018-08-15 14:48:01 +01005175#if defined(MBEDTLS_SSL_PROTO_DTLS)
5176 int have_buffered = 0;
Hanno Beckere74d5562018-08-15 14:26:08 +01005177
Hanno Becker40f50842018-08-15 14:48:01 +01005178 /* We only check for buffered messages if the
5179 * current datagram is fully consumed. */
5180 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005181 ssl_next_record_is_in_datagram( ssl ) == 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005182 {
Hanno Becker40f50842018-08-15 14:48:01 +01005183 if( ssl_load_buffered_message( ssl ) == 0 )
5184 have_buffered = 1;
5185 }
5186
5187 if( have_buffered == 0 )
5188#endif /* MBEDTLS_SSL_PROTO_DTLS */
5189 {
5190 ret = ssl_get_next_record( ssl );
5191 if( ret == MBEDTLS_ERR_SSL_CONTINUE_PROCESSING )
5192 continue;
5193
5194 if( ret != 0 )
5195 {
Hanno Beckerc573ac32018-08-28 17:15:25 +01005196 MBEDTLS_SSL_DEBUG_RET( 1, ( "ssl_get_next_record" ), ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005197 return( ret );
5198 }
Hanno Beckere74d5562018-08-15 14:26:08 +01005199 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005200 }
5201
5202 ret = mbedtls_ssl_handle_message_type( ssl );
5203
Hanno Becker40f50842018-08-15 14:48:01 +01005204#if defined(MBEDTLS_SSL_PROTO_DTLS)
5205 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5206 {
5207 /* Buffer future message */
5208 ret = ssl_buffer_message( ssl );
5209 if( ret != 0 )
5210 return( ret );
5211
5212 ret = MBEDTLS_ERR_SSL_CONTINUE_PROCESSING;
5213 }
5214#endif /* MBEDTLS_SSL_PROTO_DTLS */
5215
Hanno Becker90333da2017-10-10 11:27:13 +01005216 } while( MBEDTLS_ERR_SSL_NON_FATAL == ret ||
5217 MBEDTLS_ERR_SSL_CONTINUE_PROCESSING == ret );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005218
5219 if( 0 != ret )
Simon Butcher99000142016-10-13 17:21:01 +01005220 {
Hanno Becker05c4fc82017-11-09 14:34:06 +00005221 MBEDTLS_SSL_DEBUG_RET( 1, ( "mbedtls_ssl_handle_message_type" ), ret );
Simon Butcher99000142016-10-13 17:21:01 +01005222 return( ret );
5223 }
5224
Hanno Becker327c93b2018-08-15 13:56:18 +01005225 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
Hanno Becker3a0aad12018-08-20 09:44:02 +01005226 update_hs_digest == 1 )
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005227 {
5228 mbedtls_ssl_update_handshake_status( ssl );
5229 }
Simon Butcher99000142016-10-13 17:21:01 +01005230 }
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005231 else
Simon Butcher99000142016-10-13 17:21:01 +01005232 {
Hanno Becker02f59072018-08-15 14:00:24 +01005233 MBEDTLS_SSL_DEBUG_MSG( 2, ( "reuse previously read message" ) );
Hanno Beckeraf0665d2017-05-24 09:16:26 +01005234 ssl->keep_current_message = 0;
Simon Butcher99000142016-10-13 17:21:01 +01005235 }
5236
5237 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read record" ) );
5238
5239 return( 0 );
5240}
5241
Hanno Becker40f50842018-08-15 14:48:01 +01005242#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005243static int ssl_next_record_is_in_datagram( mbedtls_ssl_context *ssl )
Simon Butcher99000142016-10-13 17:21:01 +01005244{
Hanno Becker40f50842018-08-15 14:48:01 +01005245 if( ssl->in_left > ssl->next_record_offset )
5246 return( 1 );
Simon Butcher99000142016-10-13 17:21:01 +01005247
Hanno Becker40f50842018-08-15 14:48:01 +01005248 return( 0 );
5249}
5250
5251static int ssl_load_buffered_message( mbedtls_ssl_context *ssl )
5252{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005253 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker37f95322018-08-16 13:55:32 +01005254 mbedtls_ssl_hs_buffer * hs_buf;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005255 int ret = 0;
5256
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005257 if( hs == NULL )
5258 return( -1 );
5259
Hanno Beckere00ae372018-08-20 09:39:42 +01005260 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_messsage" ) );
5261
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005262 if( ssl->state == MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC ||
5263 ssl->state == MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5264 {
5265 /* Check if we have seen a ChangeCipherSpec before.
5266 * If yes, synthesize a CCS record. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005267 if( !hs->buffering.seen_ccs )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005268 {
5269 MBEDTLS_SSL_DEBUG_MSG( 2, ( "CCS not seen in the current flight" ) );
5270 ret = -1;
Hanno Becker0d4b3762018-08-20 09:36:59 +01005271 goto exit;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005272 }
5273
Hanno Becker39b8bc92018-08-28 17:17:13 +01005274 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Injecting buffered CCS message" ) );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005275 ssl->in_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
5276 ssl->in_msglen = 1;
5277 ssl->in_msg[0] = 1;
5278
5279 /* As long as they are equal, the exact value doesn't matter. */
5280 ssl->in_left = 0;
5281 ssl->next_record_offset = 0;
5282
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005283 hs->buffering.seen_ccs = 0;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005284 goto exit;
5285 }
Hanno Becker37f95322018-08-16 13:55:32 +01005286
Hanno Beckerb8f50142018-08-28 10:01:34 +01005287#if defined(MBEDTLS_DEBUG_C)
Hanno Becker37f95322018-08-16 13:55:32 +01005288 /* Debug only */
5289 {
5290 unsigned offset;
5291 for( offset = 1; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
5292 {
5293 hs_buf = &hs->buffering.hs[offset];
5294 if( hs_buf->is_valid == 1 )
5295 {
5296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Future message with sequence number %u %s buffered.",
5297 hs->in_msg_seq + offset,
Hanno Beckera591c482018-08-28 17:20:00 +01005298 hs_buf->is_complete ? "fully" : "partially" ) );
Hanno Becker37f95322018-08-16 13:55:32 +01005299 }
5300 }
5301 }
Hanno Beckerb8f50142018-08-28 10:01:34 +01005302#endif /* MBEDTLS_DEBUG_C */
Hanno Becker37f95322018-08-16 13:55:32 +01005303
5304 /* Check if we have buffered and/or fully reassembled the
5305 * next handshake message. */
5306 hs_buf = &hs->buffering.hs[0];
5307 if( ( hs_buf->is_valid == 1 ) && ( hs_buf->is_complete == 1 ) )
5308 {
5309 /* Synthesize a record containing the buffered HS message. */
5310 size_t msg_len = ( hs_buf->data[1] << 16 ) |
5311 ( hs_buf->data[2] << 8 ) |
5312 hs_buf->data[3];
5313
5314 /* Double-check that we haven't accidentally buffered
5315 * a message that doesn't fit into the input buffer. */
5316 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
5317 {
5318 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5319 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5320 }
5321
5322 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message has been buffered - load" ) );
5323 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered handshake message (incl. header)",
5324 hs_buf->data, msg_len + 12 );
5325
5326 ssl->in_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
5327 ssl->in_hslen = msg_len + 12;
5328 ssl->in_msglen = msg_len + 12;
5329 memcpy( ssl->in_msg, hs_buf->data, ssl->in_hslen );
5330
5331 ret = 0;
5332 goto exit;
5333 }
5334 else
5335 {
5336 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Next handshake message %u not or only partially bufffered",
5337 hs->in_msg_seq ) );
5338 }
5339
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005340 ret = -1;
5341
5342exit:
5343
5344 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_message" ) );
5345 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005346}
5347
Hanno Beckera02b0b42018-08-21 17:20:27 +01005348static int ssl_buffer_make_space( mbedtls_ssl_context *ssl,
5349 size_t desired )
5350{
5351 int offset;
5352 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005353 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Attempt to free buffered messages to have %u bytes available",
5354 (unsigned) desired ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005355
Hanno Becker01315ea2018-08-21 17:22:17 +01005356 /* Get rid of future records epoch first, if such exist. */
5357 ssl_free_buffered_record( ssl );
5358
5359 /* Check if we have enough space available now. */
5360 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5361 hs->buffering.total_bytes_buffered ) )
5362 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005363 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing future epoch record" ) );
Hanno Becker01315ea2018-08-21 17:22:17 +01005364 return( 0 );
5365 }
Hanno Beckera02b0b42018-08-21 17:20:27 +01005366
Hanno Becker4f432ad2018-08-28 10:02:32 +01005367 /* We don't have enough space to buffer the next expected handshake
5368 * message. Remove buffers used for future messages to gain space,
5369 * starting with the most distant one. */
Hanno Beckera02b0b42018-08-21 17:20:27 +01005370 for( offset = MBEDTLS_SSL_MAX_BUFFERED_HS - 1;
5371 offset >= 0; offset-- )
5372 {
5373 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Free buffering slot %d to make space for reassembly of next handshake message",
5374 offset ) );
5375
Hanno Beckerb309b922018-08-23 13:18:05 +01005376 ssl_buffering_free_slot( ssl, (uint8_t) offset );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005377
5378 /* Check if we have enough space available now. */
5379 if( desired <= ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5380 hs->buffering.total_bytes_buffered ) )
5381 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005382 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Enough space available after freeing buffered HS messages" ) );
Hanno Beckera02b0b42018-08-21 17:20:27 +01005383 return( 0 );
5384 }
5385 }
5386
5387 return( -1 );
5388}
5389
Hanno Becker40f50842018-08-15 14:48:01 +01005390static int ssl_buffer_message( mbedtls_ssl_context *ssl )
5391{
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005392 int ret = 0;
5393 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5394
5395 if( hs == NULL )
5396 return( 0 );
5397
5398 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_buffer_message" ) );
5399
5400 switch( ssl->in_msgtype )
5401 {
5402 case MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC:
5403 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Remember CCS message" ) );
Hanno Beckere678eaa2018-08-21 14:57:46 +01005404
Hanno Beckerd7f8ae22018-08-16 09:45:56 +01005405 hs->buffering.seen_ccs = 1;
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005406 break;
5407
5408 case MBEDTLS_SSL_MSG_HANDSHAKE:
Hanno Becker37f95322018-08-16 13:55:32 +01005409 {
5410 unsigned recv_msg_seq_offset;
5411 unsigned recv_msg_seq = ( ssl->in_msg[4] << 8 ) | ssl->in_msg[5];
5412 mbedtls_ssl_hs_buffer *hs_buf;
5413 size_t msg_len = ssl->in_hslen - 12;
5414
5415 /* We should never receive an old handshake
5416 * message - double-check nonetheless. */
5417 if( recv_msg_seq < ssl->handshake->in_msg_seq )
5418 {
5419 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5420 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5421 }
5422
5423 recv_msg_seq_offset = recv_msg_seq - ssl->handshake->in_msg_seq;
5424 if( recv_msg_seq_offset >= MBEDTLS_SSL_MAX_BUFFERED_HS )
5425 {
5426 /* Silently ignore -- message too far in the future */
5427 MBEDTLS_SSL_DEBUG_MSG( 2,
5428 ( "Ignore future HS message with sequence number %u, "
5429 "buffering window %u - %u",
5430 recv_msg_seq, ssl->handshake->in_msg_seq,
5431 ssl->handshake->in_msg_seq + MBEDTLS_SSL_MAX_BUFFERED_HS - 1 ) );
5432
5433 goto exit;
5434 }
5435
5436 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffering HS message with sequence number %u, offset %u ",
5437 recv_msg_seq, recv_msg_seq_offset ) );
5438
5439 hs_buf = &hs->buffering.hs[ recv_msg_seq_offset ];
5440
5441 /* Check if the buffering for this seq nr has already commenced. */
Hanno Becker4422bbb2018-08-20 09:40:19 +01005442 if( !hs_buf->is_valid )
Hanno Becker37f95322018-08-16 13:55:32 +01005443 {
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005444 size_t reassembly_buf_sz;
5445
Hanno Becker37f95322018-08-16 13:55:32 +01005446 hs_buf->is_fragmented =
5447 ( ssl_hs_is_proper_fragment( ssl ) == 1 );
5448
5449 /* We copy the message back into the input buffer
5450 * after reassembly, so check that it's not too large.
5451 * This is an implementation-specific limitation
5452 * and not one from the standard, hence it is not
5453 * checked in ssl_check_hs_header(). */
Hanno Becker96a6c692018-08-21 15:56:03 +01005454 if( msg_len + 12 > MBEDTLS_SSL_IN_CONTENT_LEN )
Hanno Becker37f95322018-08-16 13:55:32 +01005455 {
5456 /* Ignore message */
5457 goto exit;
5458 }
5459
Hanno Beckere0b150f2018-08-21 15:51:03 +01005460 /* Check if we have enough space to buffer the message. */
5461 if( hs->buffering.total_bytes_buffered >
5462 MBEDTLS_SSL_DTLS_MAX_BUFFERING )
5463 {
5464 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5465 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5466 }
5467
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005468 reassembly_buf_sz = ssl_get_reassembly_buffer_size( msg_len,
5469 hs_buf->is_fragmented );
Hanno Beckere0b150f2018-08-21 15:51:03 +01005470
5471 if( reassembly_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5472 hs->buffering.total_bytes_buffered ) )
5473 {
5474 if( recv_msg_seq_offset > 0 )
5475 {
5476 /* If we can't buffer a future message because
5477 * of space limitations -- ignore. */
5478 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",
5479 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5480 (unsigned) hs->buffering.total_bytes_buffered ) );
5481 goto exit;
5482 }
Hanno Beckere1801392018-08-21 16:51:05 +01005483 else
5484 {
5485 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",
5486 (unsigned) msg_len, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5487 (unsigned) hs->buffering.total_bytes_buffered ) );
5488 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005489
Hanno Beckera02b0b42018-08-21 17:20:27 +01005490 if( ssl_buffer_make_space( ssl, reassembly_buf_sz ) != 0 )
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005491 {
Hanno Becker6e12c1e2018-08-24 14:39:15 +01005492 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",
5493 (unsigned) msg_len,
5494 (unsigned) reassembly_buf_sz,
5495 MBEDTLS_SSL_DTLS_MAX_BUFFERING,
Hanno Beckere0b150f2018-08-21 15:51:03 +01005496 (unsigned) hs->buffering.total_bytes_buffered ) );
Hanno Becker55e9e2a2018-08-21 16:07:55 +01005497 ret = MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
5498 goto exit;
5499 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005500 }
5501
5502 MBEDTLS_SSL_DEBUG_MSG( 2, ( "initialize reassembly, total length = %d",
5503 msg_len ) );
5504
Hanno Becker2a97b0e2018-08-21 15:47:49 +01005505 hs_buf->data = mbedtls_calloc( 1, reassembly_buf_sz );
5506 if( hs_buf->data == NULL )
Hanno Becker37f95322018-08-16 13:55:32 +01005507 {
Hanno Beckere0b150f2018-08-21 15:51:03 +01005508 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
Hanno Becker37f95322018-08-16 13:55:32 +01005509 goto exit;
5510 }
Hanno Beckere0b150f2018-08-21 15:51:03 +01005511 hs_buf->data_len = reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005512
5513 /* Prepare final header: copy msg_type, length and message_seq,
5514 * then add standardised fragment_offset and fragment_length */
5515 memcpy( hs_buf->data, ssl->in_msg, 6 );
5516 memset( hs_buf->data + 6, 0, 3 );
5517 memcpy( hs_buf->data + 9, hs_buf->data + 1, 3 );
5518
5519 hs_buf->is_valid = 1;
Hanno Beckere0b150f2018-08-21 15:51:03 +01005520
5521 hs->buffering.total_bytes_buffered += reassembly_buf_sz;
Hanno Becker37f95322018-08-16 13:55:32 +01005522 }
5523 else
5524 {
5525 /* Make sure msg_type and length are consistent */
5526 if( memcmp( hs_buf->data, ssl->in_msg, 4 ) != 0 )
5527 {
5528 MBEDTLS_SSL_DEBUG_MSG( 1, ( "Fragment header mismatch - ignore" ) );
5529 /* Ignore */
5530 goto exit;
5531 }
5532 }
5533
Hanno Becker4422bbb2018-08-20 09:40:19 +01005534 if( !hs_buf->is_complete )
Hanno Becker37f95322018-08-16 13:55:32 +01005535 {
5536 size_t frag_len, frag_off;
5537 unsigned char * const msg = hs_buf->data + 12;
5538
5539 /*
5540 * Check and copy current fragment
5541 */
5542
5543 /* Validation of header fields already done in
5544 * mbedtls_ssl_prepare_handshake_record(). */
5545 frag_off = ssl_get_hs_frag_off( ssl );
5546 frag_len = ssl_get_hs_frag_len( ssl );
5547
5548 MBEDTLS_SSL_DEBUG_MSG( 2, ( "adding fragment, offset = %d, length = %d",
5549 frag_off, frag_len ) );
5550 memcpy( msg + frag_off, ssl->in_msg + 12, frag_len );
5551
5552 if( hs_buf->is_fragmented )
5553 {
5554 unsigned char * const bitmask = msg + msg_len;
5555 ssl_bitmask_set( bitmask, frag_off, frag_len );
5556 hs_buf->is_complete = ( ssl_bitmask_check( bitmask,
5557 msg_len ) == 0 );
5558 }
5559 else
5560 {
5561 hs_buf->is_complete = 1;
5562 }
5563
5564 MBEDTLS_SSL_DEBUG_MSG( 2, ( "message %scomplete",
5565 hs_buf->is_complete ? "" : "not yet " ) );
5566 }
5567
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005568 break;
Hanno Becker37f95322018-08-16 13:55:32 +01005569 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005570
5571 default:
Hanno Becker360bef32018-08-28 10:04:33 +01005572 /* We don't buffer other types of messages. */
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005573 break;
5574 }
5575
5576exit:
5577
5578 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_buffer_message" ) );
5579 return( ret );
Hanno Becker40f50842018-08-15 14:48:01 +01005580}
5581#endif /* MBEDTLS_SSL_PROTO_DTLS */
5582
Hanno Becker1097b342018-08-15 14:09:41 +01005583static int ssl_consume_current_message( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005584{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005585 /*
Hanno Becker4a810fb2017-05-24 16:27:30 +01005586 * Consume last content-layer message and potentially
5587 * update in_msglen which keeps track of the contents'
5588 * consumption state.
5589 *
5590 * (1) Handshake messages:
5591 * Remove last handshake message, move content
5592 * and adapt in_msglen.
5593 *
5594 * (2) Alert messages:
5595 * Consume whole record content, in_msglen = 0.
5596 *
Hanno Becker4a810fb2017-05-24 16:27:30 +01005597 * (3) Change cipher spec:
5598 * Consume whole record content, in_msglen = 0.
5599 *
5600 * (4) Application data:
5601 * Don't do anything - the record layer provides
5602 * the application data as a stream transport
5603 * and consumes through mbedtls_ssl_read only.
5604 *
5605 */
5606
5607 /* Case (1): Handshake messages */
5608 if( ssl->in_hslen != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005609 {
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005610 /* Hard assertion to be sure that no application data
5611 * is in flight, as corrupting ssl->in_msglen during
5612 * ssl->in_offt != NULL is fatal. */
5613 if( ssl->in_offt != NULL )
5614 {
5615 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5616 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5617 }
5618
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005619 /*
5620 * Get next Handshake message in the current record
5621 */
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005622
Hanno Becker4a810fb2017-05-24 16:27:30 +01005623 /* Notes:
Hanno Beckere72489d2017-10-23 13:23:50 +01005624 * (1) in_hslen is not necessarily the size of the
Hanno Becker4a810fb2017-05-24 16:27:30 +01005625 * current handshake content: If DTLS handshake
5626 * fragmentation is used, that's the fragment
5627 * size instead. Using the total handshake message
Hanno Beckere72489d2017-10-23 13:23:50 +01005628 * size here is faulty and should be changed at
5629 * some point.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005630 * (2) While it doesn't seem to cause problems, one
5631 * has to be very careful not to assume that in_hslen
5632 * is always <= in_msglen in a sensible communication.
5633 * Again, it's wrong for DTLS handshake fragmentation.
5634 * The following check is therefore mandatory, and
5635 * should not be treated as a silently corrected assertion.
Hanno Beckerbb9dd0c2017-06-08 11:55:34 +01005636 * Additionally, ssl->in_hslen might be arbitrarily out of
5637 * bounds after handling a DTLS message with an unexpected
5638 * sequence number, see mbedtls_ssl_prepare_handshake_record.
Hanno Becker4a810fb2017-05-24 16:27:30 +01005639 */
5640 if( ssl->in_hslen < ssl->in_msglen )
5641 {
5642 ssl->in_msglen -= ssl->in_hslen;
5643 memmove( ssl->in_msg, ssl->in_msg + ssl->in_hslen,
5644 ssl->in_msglen );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005645
Hanno Becker4a810fb2017-05-24 16:27:30 +01005646 MBEDTLS_SSL_DEBUG_BUF( 4, "remaining content in record",
5647 ssl->in_msg, ssl->in_msglen );
5648 }
5649 else
5650 {
5651 ssl->in_msglen = 0;
5652 }
Manuel Pégourié-Gonnard4a175362014-09-09 17:45:31 +02005653
Hanno Becker4a810fb2017-05-24 16:27:30 +01005654 ssl->in_hslen = 0;
5655 }
5656 /* Case (4): Application data */
5657 else if( ssl->in_offt != NULL )
5658 {
5659 return( 0 );
5660 }
5661 /* Everything else (CCS & Alerts) */
5662 else
5663 {
5664 ssl->in_msglen = 0;
5665 }
5666
Hanno Becker1097b342018-08-15 14:09:41 +01005667 return( 0 );
5668}
Hanno Becker4a810fb2017-05-24 16:27:30 +01005669
Hanno Beckere74d5562018-08-15 14:26:08 +01005670static int ssl_record_is_in_progress( mbedtls_ssl_context *ssl )
5671{
Hanno Becker4a810fb2017-05-24 16:27:30 +01005672 if( ssl->in_msglen > 0 )
Hanno Beckere74d5562018-08-15 14:26:08 +01005673 return( 1 );
5674
5675 return( 0 );
5676}
5677
Hanno Becker5f066e72018-08-16 14:56:31 +01005678#if defined(MBEDTLS_SSL_PROTO_DTLS)
5679
5680static void ssl_free_buffered_record( mbedtls_ssl_context *ssl )
5681{
5682 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5683 if( hs == NULL )
5684 return;
5685
Hanno Becker01315ea2018-08-21 17:22:17 +01005686 if( hs->buffering.future_record.data != NULL )
Hanno Becker4a810fb2017-05-24 16:27:30 +01005687 {
Hanno Becker01315ea2018-08-21 17:22:17 +01005688 hs->buffering.total_bytes_buffered -=
5689 hs->buffering.future_record.len;
5690
5691 mbedtls_free( hs->buffering.future_record.data );
5692 hs->buffering.future_record.data = NULL;
5693 }
Hanno Becker5f066e72018-08-16 14:56:31 +01005694}
5695
5696static int ssl_load_buffered_record( mbedtls_ssl_context *ssl )
5697{
5698 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5699 unsigned char * rec;
5700 size_t rec_len;
5701 unsigned rec_epoch;
5702
5703 if( ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
5704 return( 0 );
5705
5706 if( hs == NULL )
5707 return( 0 );
5708
Hanno Becker5f066e72018-08-16 14:56:31 +01005709 rec = hs->buffering.future_record.data;
5710 rec_len = hs->buffering.future_record.len;
5711 rec_epoch = hs->buffering.future_record.epoch;
5712
5713 if( rec == NULL )
5714 return( 0 );
5715
Hanno Becker4cb782d2018-08-20 11:19:05 +01005716 /* Only consider loading future records if the
5717 * input buffer is empty. */
Hanno Beckeref7afdf2018-08-28 17:16:31 +01005718 if( ssl_next_record_is_in_datagram( ssl ) == 1 )
Hanno Becker4cb782d2018-08-20 11:19:05 +01005719 return( 0 );
5720
Hanno Becker5f066e72018-08-16 14:56:31 +01005721 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> ssl_load_buffered_record" ) );
5722
5723 if( rec_epoch != ssl->in_epoch )
5724 {
5725 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffered record not from current epoch." ) );
5726 goto exit;
5727 }
5728
5729 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Found buffered record from current epoch - load" ) );
5730
5731 /* Double-check that the record is not too large */
5732 if( rec_len > MBEDTLS_SSL_IN_BUFFER_LEN -
5733 (size_t)( ssl->in_hdr - ssl->in_buf ) )
5734 {
5735 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
5736 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
5737 }
5738
5739 memcpy( ssl->in_hdr, rec, rec_len );
5740 ssl->in_left = rec_len;
5741 ssl->next_record_offset = 0;
5742
5743 ssl_free_buffered_record( ssl );
5744
5745exit:
5746 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= ssl_load_buffered_record" ) );
5747 return( 0 );
5748}
5749
5750static int ssl_buffer_future_record( mbedtls_ssl_context *ssl )
5751{
5752 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
5753 size_t const rec_hdr_len = 13;
Hanno Becker01315ea2018-08-21 17:22:17 +01005754 size_t const total_buf_sz = rec_hdr_len + ssl->in_msglen;
Hanno Becker5f066e72018-08-16 14:56:31 +01005755
5756 /* Don't buffer future records outside handshakes. */
5757 if( hs == NULL )
5758 return( 0 );
5759
5760 /* Only buffer handshake records (we are only interested
5761 * in Finished messages). */
5762 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
5763 return( 0 );
5764
5765 /* Don't buffer more than one future epoch record. */
5766 if( hs->buffering.future_record.data != NULL )
5767 return( 0 );
5768
Hanno Becker01315ea2018-08-21 17:22:17 +01005769 /* Don't buffer record if there's not enough buffering space remaining. */
5770 if( total_buf_sz > ( MBEDTLS_SSL_DTLS_MAX_BUFFERING -
5771 hs->buffering.total_bytes_buffered ) )
5772 {
5773 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",
5774 (unsigned) total_buf_sz, MBEDTLS_SSL_DTLS_MAX_BUFFERING,
5775 (unsigned) hs->buffering.total_bytes_buffered ) );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005776 return( 0 );
5777 }
5778
Hanno Becker5f066e72018-08-16 14:56:31 +01005779 /* Buffer record */
5780 MBEDTLS_SSL_DEBUG_MSG( 2, ( "Buffer record from epoch %u",
5781 ssl->in_epoch + 1 ) );
5782 MBEDTLS_SSL_DEBUG_BUF( 3, "Buffered record", ssl->in_hdr,
5783 rec_hdr_len + ssl->in_msglen );
5784
5785 /* ssl_parse_record_header() only considers records
5786 * of the next epoch as candidates for buffering. */
5787 hs->buffering.future_record.epoch = ssl->in_epoch + 1;
Hanno Becker01315ea2018-08-21 17:22:17 +01005788 hs->buffering.future_record.len = total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005789
5790 hs->buffering.future_record.data =
5791 mbedtls_calloc( 1, hs->buffering.future_record.len );
5792 if( hs->buffering.future_record.data == NULL )
5793 {
5794 /* If we run out of RAM trying to buffer a
5795 * record from the next epoch, just ignore. */
5796 return( 0 );
5797 }
5798
Hanno Becker01315ea2018-08-21 17:22:17 +01005799 memcpy( hs->buffering.future_record.data, ssl->in_hdr, total_buf_sz );
Hanno Becker5f066e72018-08-16 14:56:31 +01005800
Hanno Becker01315ea2018-08-21 17:22:17 +01005801 hs->buffering.total_bytes_buffered += total_buf_sz;
Hanno Becker5f066e72018-08-16 14:56:31 +01005802 return( 0 );
5803}
5804
5805#endif /* MBEDTLS_SSL_PROTO_DTLS */
5806
Hanno Beckere74d5562018-08-15 14:26:08 +01005807static int ssl_get_next_record( mbedtls_ssl_context *ssl )
Hanno Becker1097b342018-08-15 14:09:41 +01005808{
5809 int ret;
5810
Hanno Becker5f066e72018-08-16 14:56:31 +01005811#if defined(MBEDTLS_SSL_PROTO_DTLS)
5812 /* We might have buffered a future record; if so,
5813 * and if the epoch matches now, load it.
5814 * On success, this call will set ssl->in_left to
5815 * the length of the buffered record, so that
5816 * the calls to ssl_fetch_input() below will
5817 * essentially be no-ops. */
5818 ret = ssl_load_buffered_record( ssl );
5819 if( ret != 0 )
5820 return( ret );
5821#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker4a810fb2017-05-24 16:27:30 +01005822
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005823 if( ( ret = mbedtls_ssl_fetch_input( ssl, mbedtls_ssl_hdr_len( ssl ) ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005825 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005826 return( ret );
5827 }
5828
5829 if( ( ret = ssl_parse_record_header( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardbe619c12015-09-08 11:21:21 +02005832 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5833 ret != MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005834 {
Hanno Becker5f066e72018-08-16 14:56:31 +01005835 if( ret == MBEDTLS_ERR_SSL_EARLY_MESSAGE )
5836 {
5837 ret = ssl_buffer_future_record( ssl );
5838 if( ret != 0 )
5839 return( ret );
5840
5841 /* Fall through to handling of unexpected records */
5842 ret = MBEDTLS_ERR_SSL_UNEXPECTED_RECORD;
5843 }
5844
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005845 if( ret == MBEDTLS_ERR_SSL_UNEXPECTED_RECORD )
5846 {
5847 /* Skip unexpected record (but not whole datagram) */
5848 ssl->next_record_offset = ssl->in_msglen
5849 + mbedtls_ssl_hdr_len( ssl );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005850
Manuel Pégourié-Gonnarde2e25e72015-12-03 16:13:17 +01005851 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding unexpected record "
5852 "(header)" ) );
5853 }
5854 else
5855 {
5856 /* Skip invalid record and the rest of the datagram */
5857 ssl->next_record_offset = 0;
5858 ssl->in_left = 0;
5859
5860 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record "
5861 "(header)" ) );
5862 }
5863
5864 /* Get next record */
Hanno Becker90333da2017-10-10 11:27:13 +01005865 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005866 }
5867#endif
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005868 return( ret );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005869 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005870
5871 /*
5872 * Read and optionally decrypt the message contents
5873 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005874 if( ( ret = mbedtls_ssl_fetch_input( ssl,
5875 mbedtls_ssl_hdr_len( ssl ) + ssl->in_msglen ) ) != 0 )
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005876 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005877 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_fetch_input", ret );
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005878 return( ret );
5879 }
5880
5881 /* Done reading this record, get ready for the next one */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005883 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Beckere65ce782017-05-22 14:47:48 +01005884 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005885 ssl->next_record_offset = ssl->in_msglen + mbedtls_ssl_hdr_len( ssl );
Hanno Beckere65ce782017-05-22 14:47:48 +01005886 if( ssl->next_record_offset < ssl->in_left )
5887 {
5888 MBEDTLS_SSL_DEBUG_MSG( 3, ( "more than one record within datagram" ) );
5889 }
5890 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005891 else
5892#endif
5893 ssl->in_left = 0;
5894
5895 if( ( ret = ssl_prepare_record_content( ssl ) ) != 0 )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005896 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005898 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005899 {
5900 /* Silently discard invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005901 if( ret == MBEDTLS_ERR_SSL_INVALID_RECORD ||
5902 ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005903 {
Manuel Pégourié-Gonnard0a885742015-08-04 12:08:35 +02005904 /* Except when waiting for Finished as a bad mac here
5905 * probably means something went wrong in the handshake
5906 * (eg wrong psk used, mitm downgrade attempt, etc.) */
5907 if( ssl->state == MBEDTLS_SSL_CLIENT_FINISHED ||
5908 ssl->state == MBEDTLS_SSL_SERVER_FINISHED )
5909 {
5910#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5911 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
5912 {
5913 mbedtls_ssl_send_alert_message( ssl,
5914 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5915 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
5916 }
5917#endif
5918 return( ret );
5919 }
5920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005921#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02005922 if( ssl->conf->badmac_limit != 0 &&
5923 ++ssl->badmac_seen >= ssl->conf->badmac_limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005924 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005925 MBEDTLS_SSL_DEBUG_MSG( 1, ( "too many records with bad MAC" ) );
5926 return( MBEDTLS_ERR_SSL_INVALID_MAC );
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02005927 }
5928#endif
5929
Hanno Becker4a810fb2017-05-24 16:27:30 +01005930 /* As above, invalid records cause
5931 * dismissal of the whole datagram. */
5932
5933 ssl->next_record_offset = 0;
5934 ssl->in_left = 0;
5935
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005936 MBEDTLS_SSL_DEBUG_MSG( 1, ( "discarding invalid record (mac)" ) );
Hanno Becker90333da2017-10-10 11:27:13 +01005937 return( MBEDTLS_ERR_SSL_CONTINUE_PROCESSING );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005938 }
5939
5940 return( ret );
5941 }
5942 else
5943#endif
5944 {
5945 /* Error out (and send alert) on invalid records */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005946#if defined(MBEDTLS_SSL_ALL_ALERT_MESSAGES)
5947 if( ret == MBEDTLS_ERR_SSL_INVALID_MAC )
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005949 mbedtls_ssl_send_alert_message( ssl,
5950 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
5951 MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC );
Manuel Pégourié-Gonnard63eca932014-09-08 16:39:08 +02005952 }
5953#endif
5954 return( ret );
5955 }
5956 }
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005957
Simon Butcher99000142016-10-13 17:21:01 +01005958 return( 0 );
5959}
5960
5961int mbedtls_ssl_handle_message_type( mbedtls_ssl_context *ssl )
5962{
5963 int ret;
5964
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02005965 /*
Manuel Pégourié-Gonnard167a3762014-09-08 16:14:10 +02005966 * Handle particular types of records
5967 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005968 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00005969 {
Simon Butcher99000142016-10-13 17:21:01 +01005970 if( ( ret = mbedtls_ssl_prepare_handshake_record( ssl ) ) != 0 )
5971 {
Manuel Pégourié-Gonnarda59543a2014-02-18 11:33:49 +01005972 return( ret );
Simon Butcher99000142016-10-13 17:21:01 +01005973 }
Paul Bakker5121ce52009-01-03 21:22:43 +00005974 }
5975
Hanno Beckere678eaa2018-08-21 14:57:46 +01005976 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005977 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005978 if( ssl->in_msglen != 1 )
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005979 {
Hanno Beckere678eaa2018-08-21 14:57:46 +01005980 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, len: %d",
5981 ssl->in_msglen ) );
5982 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01005983 }
5984
Hanno Beckere678eaa2018-08-21 14:57:46 +01005985 if( ssl->in_msg[0] != 1 )
5986 {
5987 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid CCS message, content: %02x",
5988 ssl->in_msg[0] ) );
5989 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
5990 }
5991
5992#if defined(MBEDTLS_SSL_PROTO_DTLS)
5993 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
5994 ssl->state != MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC &&
5995 ssl->state != MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC )
5996 {
5997 if( ssl->handshake == NULL )
5998 {
5999 MBEDTLS_SSL_DEBUG_MSG( 1, ( "dropping ChangeCipherSpec outside handshake" ) );
6000 return( MBEDTLS_ERR_SSL_UNEXPECTED_RECORD );
6001 }
6002
6003 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received out-of-order ChangeCipherSpec - remember" ) );
6004 return( MBEDTLS_ERR_SSL_EARLY_MESSAGE );
6005 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006006#endif
Hanno Beckere678eaa2018-08-21 14:57:46 +01006007 }
Hanno Becker2ed6bcc2018-08-15 15:11:57 +01006008
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006009 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006010 {
Angus Gratton1a7a17e2018-06-20 15:43:50 +10006011 if( ssl->in_msglen != 2 )
6012 {
6013 /* Note: Standard allows for more than one 2 byte alert
6014 to be packed in a single message, but Mbed TLS doesn't
6015 currently support this. */
6016 MBEDTLS_SSL_DEBUG_MSG( 1, ( "invalid alert message, len: %d",
6017 ssl->in_msglen ) );
6018 return( MBEDTLS_ERR_SSL_INVALID_RECORD );
6019 }
6020
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006021 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got an alert message, type: [%d:%d]",
Paul Bakker5121ce52009-01-03 21:22:43 +00006022 ssl->in_msg[0], ssl->in_msg[1] ) );
6023
6024 /*
Simon Butcher459a9502015-10-27 16:09:03 +00006025 * Ignore non-fatal alerts, except close_notify and no_renegotiation
Paul Bakker5121ce52009-01-03 21:22:43 +00006026 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006027 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_FATAL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006028 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006029 MBEDTLS_SSL_DEBUG_MSG( 1, ( "is a fatal alert message (msg %d)",
Paul Bakker2770fbd2012-07-03 13:30:23 +00006030 ssl->in_msg[1] ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006031 return( MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006032 }
6033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006034 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6035 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY )
Paul Bakker5121ce52009-01-03 21:22:43 +00006036 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006037 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a close notify message" ) );
6038 return( MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY );
Paul Bakker5121ce52009-01-03 21:22:43 +00006039 }
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006040
6041#if defined(MBEDTLS_SSL_RENEGOTIATION_ENABLED)
6042 if( ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6043 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION )
6044 {
Hanno Becker90333da2017-10-10 11:27:13 +01006045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no renegotiation alert" ) );
Manuel Pégourié-Gonnardfbdf06c2015-10-23 11:13:28 +02006046 /* Will be handled when trying to parse ServerHello */
6047 return( 0 );
6048 }
6049#endif
6050
6051#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_SRV_C)
6052 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 &&
6053 ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
6054 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6055 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6056 {
6057 MBEDTLS_SSL_DEBUG_MSG( 2, ( "is a SSLv3 no_cert" ) );
6058 /* Will be handled in mbedtls_ssl_parse_certificate() */
6059 return( 0 );
6060 }
6061#endif /* MBEDTLS_SSL_PROTO_SSL3 && MBEDTLS_SSL_SRV_C */
6062
6063 /* Silently ignore: fetch new message */
Simon Butcher99000142016-10-13 17:21:01 +01006064 return MBEDTLS_ERR_SSL_NON_FATAL;
Paul Bakker5121ce52009-01-03 21:22:43 +00006065 }
6066
Hanno Beckerc76c6192017-06-06 10:03:17 +01006067#if defined(MBEDTLS_SSL_PROTO_DTLS)
6068 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
6069 ssl->handshake != NULL &&
6070 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
6071 {
6072 ssl_handshake_wrapup_free_hs_transform( ssl );
6073 }
6074#endif
6075
Paul Bakker5121ce52009-01-03 21:22:43 +00006076 return( 0 );
6077}
6078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006079int mbedtls_ssl_send_fatal_handshake_failure( mbedtls_ssl_context *ssl )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006080{
6081 int ret;
6082
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006083 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
6084 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6085 MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00006086 {
6087 return( ret );
6088 }
6089
6090 return( 0 );
6091}
6092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006093int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
Paul Bakker0a925182012-04-16 06:46:41 +00006094 unsigned char level,
6095 unsigned char message )
6096{
6097 int ret;
6098
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02006099 if( ssl == NULL || ssl->conf == NULL )
6100 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
6101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006102 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> send alert message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006103 MBEDTLS_SSL_DEBUG_MSG( 3, ( "send alert level=%u message=%u", level, message ));
Paul Bakker0a925182012-04-16 06:46:41 +00006104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006105 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
Paul Bakker0a925182012-04-16 06:46:41 +00006106 ssl->out_msglen = 2;
6107 ssl->out_msg[0] = level;
6108 ssl->out_msg[1] = message;
6109
Hanno Becker67bc7c32018-08-06 11:33:50 +01006110 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker0a925182012-04-16 06:46:41 +00006111 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006112 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker0a925182012-04-16 06:46:41 +00006113 return( ret );
6114 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006115 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= send alert message" ) );
Paul Bakker0a925182012-04-16 06:46:41 +00006116
6117 return( 0 );
6118}
6119
Hanno Beckerb9d44792019-02-08 07:19:04 +00006120#if defined(MBEDTLS_X509_CRT_PARSE_C)
6121static void ssl_clear_peer_cert( mbedtls_ssl_session *session )
6122{
6123#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6124 if( session->peer_cert != NULL )
6125 {
6126 mbedtls_x509_crt_free( session->peer_cert );
6127 mbedtls_free( session->peer_cert );
6128 session->peer_cert = NULL;
6129 }
6130#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6131 if( session->peer_cert_digest != NULL )
6132 {
6133 /* Zeroization is not necessary. */
6134 mbedtls_free( session->peer_cert_digest );
6135 session->peer_cert_digest = NULL;
6136 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
6137 session->peer_cert_digest_len = 0;
6138 }
6139#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6140}
6141#endif /* MBEDTLS_X509_CRT_PARSE_C */
6142
Paul Bakker5121ce52009-01-03 21:22:43 +00006143/*
6144 * Handshake functions
6145 */
Hanno Becker21489932019-02-05 13:20:55 +00006146#if !defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Gilles Peskinef9828522017-05-03 12:28:43 +02006147/* No certificate support -> dummy functions */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006148int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006149{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006150 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6151 ssl->handshake->ciphersuite_info;
Paul Bakker5121ce52009-01-03 21:22:43 +00006152
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006153 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006154
Hanno Becker7177a882019-02-05 13:36:46 +00006155 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006156 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006157 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02006158 ssl->state++;
6159 return( 0 );
6160 }
6161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006162 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6163 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006164}
6165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006166int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006167{
Hanno Beckere694c3e2017-12-27 21:34:08 +00006168 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6169 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006171 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006172
Hanno Becker7177a882019-02-05 13:36:46 +00006173 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006175 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006176 ssl->state++;
6177 return( 0 );
6178 }
6179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006180 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
6181 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006182}
Gilles Peskinef9828522017-05-03 12:28:43 +02006183
Hanno Becker21489932019-02-05 13:20:55 +00006184#else /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Gilles Peskinef9828522017-05-03 12:28:43 +02006185/* Some certificate support -> implement write and parse */
6186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006187int mbedtls_ssl_write_certificate( mbedtls_ssl_context *ssl )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006188{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006189 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006190 size_t i, n;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006191 const mbedtls_x509_crt *crt;
Hanno Beckere694c3e2017-12-27 21:34:08 +00006192 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6193 ssl->handshake->ciphersuite_info;
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006194
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006195 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006196
Hanno Becker7177a882019-02-05 13:36:46 +00006197 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker48f7a5d2013-04-19 14:30:58 +02006200 ssl->state++;
6201 return( 0 );
6202 }
6203
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006204#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006205 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker5121ce52009-01-03 21:22:43 +00006206 {
6207 if( ssl->client_auth == 0 )
6208 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006209 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006210 ssl->state++;
6211 return( 0 );
6212 }
6213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006214#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker5121ce52009-01-03 21:22:43 +00006215 /*
6216 * If using SSLv3 and got no cert, send an Alert message
6217 * (otherwise an empty Certificate message will be sent).
6218 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006219 if( mbedtls_ssl_own_cert( ssl ) == NULL &&
6220 ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006221 {
6222 ssl->out_msglen = 2;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006223 ssl->out_msgtype = MBEDTLS_SSL_MSG_ALERT;
6224 ssl->out_msg[0] = MBEDTLS_SSL_ALERT_LEVEL_WARNING;
6225 ssl->out_msg[1] = MBEDTLS_SSL_ALERT_MSG_NO_CERT;
Paul Bakker5121ce52009-01-03 21:22:43 +00006226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006227 MBEDTLS_SSL_DEBUG_MSG( 2, ( "got no certificate to send" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006228 goto write_msg;
6229 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006230#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00006231 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006232#endif /* MBEDTLS_SSL_CLI_C */
6233#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02006234 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00006235 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006236 if( mbedtls_ssl_own_cert( ssl ) == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006237 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006238 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no certificate to send" ) );
6239 return( MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED );
Paul Bakker5121ce52009-01-03 21:22:43 +00006240 }
6241 }
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01006242#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006243
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006244 MBEDTLS_SSL_DEBUG_CRT( 3, "own certificate", mbedtls_ssl_own_cert( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006245
6246 /*
6247 * 0 . 0 handshake type
6248 * 1 . 3 handshake length
6249 * 4 . 6 length of all certs
6250 * 7 . 9 length of cert. 1
6251 * 10 . n-1 peer certificate
6252 * n . n+2 length of cert. 2
6253 * n+3 . ... upper level cert, etc.
6254 */
6255 i = 7;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006256 crt = mbedtls_ssl_own_cert( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00006257
Paul Bakker29087132010-03-21 21:03:34 +00006258 while( crt != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00006259 {
6260 n = crt->raw.len;
Angus Grattond8213d02016-05-25 20:56:48 +10006261 if( n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i )
Paul Bakker5121ce52009-01-03 21:22:43 +00006262 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006263 MBEDTLS_SSL_DEBUG_MSG( 1, ( "certificate too large, %d > %d",
Angus Grattond8213d02016-05-25 20:56:48 +10006264 i + 3 + n, MBEDTLS_SSL_OUT_CONTENT_LEN ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006265 return( MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006266 }
6267
6268 ssl->out_msg[i ] = (unsigned char)( n >> 16 );
6269 ssl->out_msg[i + 1] = (unsigned char)( n >> 8 );
6270 ssl->out_msg[i + 2] = (unsigned char)( n );
6271
6272 i += 3; memcpy( ssl->out_msg + i, crt->raw.p, n );
6273 i += n; crt = crt->next;
6274 }
6275
6276 ssl->out_msg[4] = (unsigned char)( ( i - 7 ) >> 16 );
6277 ssl->out_msg[5] = (unsigned char)( ( i - 7 ) >> 8 );
6278 ssl->out_msg[6] = (unsigned char)( ( i - 7 ) );
6279
6280 ssl->out_msglen = i;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006281 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6282 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
Paul Bakker5121ce52009-01-03 21:22:43 +00006283
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02006284#if defined(MBEDTLS_SSL_PROTO_SSL3) && defined(MBEDTLS_SSL_CLI_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00006285write_msg:
Paul Bakkerd2f068e2013-08-27 21:19:20 +02006286#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00006287
6288 ssl->state++;
6289
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006290 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006291 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006292 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006293 return( ret );
6294 }
6295
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006296 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006297
Paul Bakkered27a042013-04-18 22:46:23 +02006298 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006299}
6300
Hanno Becker84879e32019-01-31 07:44:03 +00006301#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Hanno Becker177475a2019-02-05 17:02:46 +00006302
6303#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006304static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6305 unsigned char *crt_buf,
6306 size_t crt_buf_len )
6307{
6308 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6309
6310 if( peer_crt == NULL )
6311 return( -1 );
6312
6313 if( peer_crt->raw.len != crt_buf_len )
6314 return( -1 );
6315
Hanno Becker46f34d02019-02-08 14:00:04 +00006316 return( memcmp( peer_crt->raw.p, crt_buf, crt_buf_len ) );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006317}
Hanno Becker177475a2019-02-05 17:02:46 +00006318#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6319static int ssl_check_peer_crt_unchanged( mbedtls_ssl_context *ssl,
6320 unsigned char *crt_buf,
6321 size_t crt_buf_len )
6322{
6323 int ret;
6324 unsigned char const * const peer_cert_digest =
6325 ssl->session->peer_cert_digest;
6326 mbedtls_md_type_t const peer_cert_digest_type =
6327 ssl->session->peer_cert_digest_type;
6328 mbedtls_md_info_t const * const digest_info =
6329 mbedtls_md_info_from_type( peer_cert_digest_type );
6330 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6331 size_t digest_len;
6332
6333 if( peer_cert_digest == NULL || digest_info == NULL )
6334 return( -1 );
6335
6336 digest_len = mbedtls_md_get_size( digest_info );
6337 if( digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN )
6338 return( -1 );
6339
6340 ret = mbedtls_md( digest_info, crt_buf, crt_buf_len, tmp_digest );
6341 if( ret != 0 )
6342 return( -1 );
6343
6344 return( memcmp( tmp_digest, peer_cert_digest, digest_len ) );
6345}
6346#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker84879e32019-01-31 07:44:03 +00006347#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006348
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006349/*
6350 * Once the certificate message is read, parse it into a cert chain and
6351 * perform basic checks, but leave actual verification to the caller
6352 */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006353static int ssl_parse_certificate_chain( mbedtls_ssl_context *ssl,
6354 mbedtls_x509_crt *chain )
Paul Bakker5121ce52009-01-03 21:22:43 +00006355{
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006356 int ret;
6357#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6358 int crt_cnt=0;
6359#endif
Paul Bakker23986e52011-04-24 08:57:21 +00006360 size_t i, n;
Gilles Peskine064a85c2017-05-10 10:46:40 +02006361 uint8_t alert;
Paul Bakker5121ce52009-01-03 21:22:43 +00006362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006363 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00006364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006365 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006366 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6367 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006368 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006369 }
6370
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006371 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE ||
6372 ssl->in_hslen < mbedtls_ssl_hs_hdr_len( ssl ) + 3 + 3 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006373 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006374 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006375 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6376 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006377 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006378 }
6379
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006380 i = mbedtls_ssl_hs_hdr_len( ssl );
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006381
Paul Bakker5121ce52009-01-03 21:22:43 +00006382 /*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006383 * Same message structure as in mbedtls_ssl_write_certificate()
Paul Bakker5121ce52009-01-03 21:22:43 +00006384 */
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006385 n = ( ssl->in_msg[i+1] << 8 ) | ssl->in_msg[i+2];
Paul Bakker5121ce52009-01-03 21:22:43 +00006386
Manuel Pégourié-Gonnardf49a7da2014-09-10 13:30:43 +00006387 if( ssl->in_msg[i] != 0 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006388 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len( ssl ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00006389 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006390 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006391 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6392 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006393 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006394 }
6395
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006396 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6397 i += 3;
6398
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006399 /* Iterate through and parse the CRTs in the provided chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006400 while( i < ssl->in_hslen )
6401 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006402 /* Check that there's room for the next CRT's length fields. */
Philippe Antoine747fd532018-05-30 09:13:21 +02006403 if ( i + 3 > ssl->in_hslen ) {
6404 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006405 mbedtls_ssl_send_alert_message( ssl,
6406 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6407 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Philippe Antoine747fd532018-05-30 09:13:21 +02006408 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
6409 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006410 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6411 * anything beyond 2**16 ~ 64K. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006412 if( ssl->in_msg[i] != 0 )
6413 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006414 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006415 mbedtls_ssl_send_alert_message( ssl,
6416 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6417 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006418 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006419 }
6420
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006421 /* Read length of the next CRT in the chain. */
Paul Bakker5121ce52009-01-03 21:22:43 +00006422 n = ( (unsigned int) ssl->in_msg[i + 1] << 8 )
6423 | (unsigned int) ssl->in_msg[i + 2];
6424 i += 3;
6425
6426 if( n < 128 || i + n > ssl->in_hslen )
6427 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006428 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate message" ) );
Hanno Beckere2734e22019-01-31 07:44:17 +00006429 mbedtls_ssl_send_alert_message( ssl,
6430 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6431 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006432 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Paul Bakker5121ce52009-01-03 21:22:43 +00006433 }
6434
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006435 /* Check if we're handling the first CRT in the chain. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006436#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6437 if( crt_cnt++ == 0 &&
6438 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
6439 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006440 {
Hanno Becker46f34d02019-02-08 14:00:04 +00006441 /* During client-side renegotiation, check that the server's
6442 * end-CRTs hasn't changed compared to the initial handshake,
6443 * mitigating the triple handshake attack. On success, reuse
6444 * the original end-CRT instead of parsing it again. */
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006445 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Check that peer CRT hasn't changed during renegotiation" ) );
6446 if( ssl_check_peer_crt_unchanged( ssl,
6447 &ssl->in_msg[i],
6448 n ) != 0 )
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006449 {
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006450 MBEDTLS_SSL_DEBUG_MSG( 1, ( "new server cert during renegotiation" ) );
6451 mbedtls_ssl_send_alert_message( ssl,
6452 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6453 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED );
6454 return( MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE );
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006455 }
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006456
6457 /* Now we can safely free the original chain. */
6458 ssl_clear_peer_cert( ssl->session );
6459 }
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006460#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6461
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006462 /* Parse the next certificate in the chain. */
Hanno Becker0056eab2019-02-08 14:39:16 +00006463#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006464 ret = mbedtls_x509_crt_parse_der( chain, ssl->in_msg + i, n );
Hanno Becker0056eab2019-02-08 14:39:16 +00006465#else
Hanno Becker353a6f02019-02-26 11:51:34 +00006466 /* If we don't need to store the CRT chain permanently, parse
Hanno Becker0056eab2019-02-08 14:39:16 +00006467 * it in-place from the input buffer instead of making a copy. */
6468 ret = mbedtls_x509_crt_parse_der_nocopy( chain, ssl->in_msg + i, n );
6469#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006470 switch( ret )
Paul Bakker5121ce52009-01-03 21:22:43 +00006471 {
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006472 case 0: /*ok*/
6473 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
6474 /* Ignore certificate with an unknown algorithm: maybe a
6475 prior certificate was already trusted. */
6476 break;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006477
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006478 case MBEDTLS_ERR_X509_ALLOC_FAILED:
6479 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
6480 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006481
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006482 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
6483 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6484 goto crt_parse_der_failed;
Gilles Peskine1cc8e342017-05-03 16:28:34 +02006485
Hanno Beckerdef9bdc2019-01-30 14:46:46 +00006486 default:
6487 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6488 crt_parse_der_failed:
6489 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert );
6490 MBEDTLS_SSL_DEBUG_RET( 1, " mbedtls_x509_crt_parse_der", ret );
6491 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006492 }
6493
6494 i += n;
6495 }
6496
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006497 MBEDTLS_SSL_DEBUG_CRT( 3, "peer certificate", chain );
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006498 return( 0 );
6499}
6500
Hanno Becker4a55f632019-02-05 12:49:06 +00006501#if defined(MBEDTLS_SSL_SRV_C)
6502static int ssl_srv_check_client_no_crt_notification( mbedtls_ssl_context *ssl )
6503{
6504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
6505 return( -1 );
6506
6507#if defined(MBEDTLS_SSL_PROTO_SSL3)
6508 /*
6509 * Check if the client sent an empty certificate
6510 */
6511 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
6512 {
6513 if( ssl->in_msglen == 2 &&
6514 ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT &&
6515 ssl->in_msg[0] == MBEDTLS_SSL_ALERT_LEVEL_WARNING &&
6516 ssl->in_msg[1] == MBEDTLS_SSL_ALERT_MSG_NO_CERT )
6517 {
6518 MBEDTLS_SSL_DEBUG_MSG( 1, ( "SSLv3 client has no certificate" ) );
6519 return( 0 );
6520 }
6521
6522 return( -1 );
6523 }
6524#endif /* MBEDTLS_SSL_PROTO_SSL3 */
6525
6526#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
6527 defined(MBEDTLS_SSL_PROTO_TLS1_2)
6528 if( ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len( ssl ) &&
6529 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
6530 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
6531 memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ), "\0\0\0", 3 ) == 0 )
6532 {
6533 MBEDTLS_SSL_DEBUG_MSG( 1, ( "TLSv1 client has no certificate" ) );
6534 return( 0 );
6535 }
6536
6537 return( -1 );
6538#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
6539 MBEDTLS_SSL_PROTO_TLS1_2 */
6540}
6541#endif /* MBEDTLS_SSL_SRV_C */
6542
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006543/* Check if a certificate message is expected.
6544 * Return either
6545 * - SSL_CERTIFICATE_EXPECTED, or
6546 * - SSL_CERTIFICATE_SKIP
6547 * indicating whether a Certificate message is expected or not.
6548 */
6549#define SSL_CERTIFICATE_EXPECTED 0
6550#define SSL_CERTIFICATE_SKIP 1
6551static int ssl_parse_certificate_coordinate( mbedtls_ssl_context *ssl,
6552 int authmode )
6553{
6554 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006555 ssl->handshake->ciphersuite_info;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006556
6557 if( !mbedtls_ssl_ciphersuite_uses_srv_cert( ciphersuite_info ) )
6558 return( SSL_CERTIFICATE_SKIP );
6559
6560#if defined(MBEDTLS_SSL_SRV_C)
6561 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
6562 {
6563 if( ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK )
6564 return( SSL_CERTIFICATE_SKIP );
6565
6566 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6567 {
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006568 ssl->session_negotiate->verify_result =
6569 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
6570 return( SSL_CERTIFICATE_SKIP );
6571 }
6572 }
Hanno Becker84d9d272019-03-01 08:10:46 +00006573#else
6574 ((void) authmode);
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006575#endif /* MBEDTLS_SSL_SRV_C */
6576
6577 return( SSL_CERTIFICATE_EXPECTED );
6578}
6579
Hanno Becker68636192019-02-05 14:36:34 +00006580static int ssl_parse_certificate_verify( mbedtls_ssl_context *ssl,
6581 int authmode,
6582 mbedtls_x509_crt *chain,
6583 void *rs_ctx )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006584{
Hanno Becker6bdfab22019-02-05 13:11:17 +00006585 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006586 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Hanno Beckere694c3e2017-12-27 21:34:08 +00006587 ssl->handshake->ciphersuite_info;
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006588 int have_ca_chain = 0;
Hanno Becker68636192019-02-05 14:36:34 +00006589
Hanno Becker8927c832019-04-03 12:52:50 +01006590 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
6591 void *p_vrfy;
6592
Hanno Becker68636192019-02-05 14:36:34 +00006593 if( authmode == MBEDTLS_SSL_VERIFY_NONE )
6594 return( 0 );
6595
Hanno Becker8927c832019-04-03 12:52:50 +01006596 if( ssl->f_vrfy != NULL )
6597 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006598 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use context-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006599 f_vrfy = ssl->f_vrfy;
6600 p_vrfy = ssl->p_vrfy;
6601 }
6602 else
6603 {
Hanno Beckerefb440a2019-04-03 13:04:33 +01006604 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Use configuration-specific verification callback" ) );
Hanno Becker8927c832019-04-03 12:52:50 +01006605 f_vrfy = ssl->conf->f_vrfy;
6606 p_vrfy = ssl->conf->p_vrfy;
6607 }
6608
Hanno Becker68636192019-02-05 14:36:34 +00006609 /*
6610 * Main check: verify certificate
6611 */
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006612#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
6613 if( ssl->conf->f_ca_cb != NULL )
6614 {
6615 ((void) rs_ctx);
6616 have_ca_chain = 1;
6617
6618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "use CA callback for X.509 CRT verification" ) );
Jarno Lamsa9822c0d2019-04-01 16:59:48 +03006619 ret = mbedtls_x509_crt_verify_with_ca_cb(
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006620 chain,
6621 ssl->conf->f_ca_cb,
6622 ssl->conf->p_ca_cb,
6623 ssl->conf->cert_profile,
6624 ssl->hostname,
6625 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006626 f_vrfy, p_vrfy );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006627 }
6628 else
6629#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
6630 {
6631 mbedtls_x509_crt *ca_chain;
6632 mbedtls_x509_crl *ca_crl;
6633
6634#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6635 if( ssl->handshake->sni_ca_chain != NULL )
6636 {
6637 ca_chain = ssl->handshake->sni_ca_chain;
6638 ca_crl = ssl->handshake->sni_ca_crl;
6639 }
6640 else
6641#endif
6642 {
6643 ca_chain = ssl->conf->ca_chain;
6644 ca_crl = ssl->conf->ca_crl;
6645 }
6646
6647 if( ca_chain != NULL )
6648 have_ca_chain = 1;
6649
6650 ret = mbedtls_x509_crt_verify_restartable(
6651 chain,
6652 ca_chain, ca_crl,
6653 ssl->conf->cert_profile,
6654 ssl->hostname,
6655 &ssl->session_negotiate->verify_result,
Jaeden Amerofe710672019-04-16 15:03:12 +01006656 f_vrfy, p_vrfy, rs_ctx );
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006657 }
Hanno Becker68636192019-02-05 14:36:34 +00006658
6659 if( ret != 0 )
6660 {
6661 MBEDTLS_SSL_DEBUG_RET( 1, "x509_verify_cert", ret );
6662 }
6663
6664#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6665 if( ret == MBEDTLS_ERR_ECP_IN_PROGRESS )
6666 return( MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS );
6667#endif
6668
6669 /*
6670 * Secondary checks: always done, but change 'ret' only if it was 0
6671 */
6672
6673#if defined(MBEDTLS_ECP_C)
6674 {
6675 const mbedtls_pk_context *pk = &chain->pk;
6676
6677 /* If certificate uses an EC key, make sure the curve is OK */
6678 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECKEY ) &&
6679 mbedtls_ssl_check_curve( ssl, mbedtls_pk_ec( *pk )->grp.id ) != 0 )
6680 {
6681 ssl->session_negotiate->verify_result |= MBEDTLS_X509_BADCERT_BAD_KEY;
6682
6683 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (EC key curve)" ) );
6684 if( ret == 0 )
6685 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6686 }
6687 }
6688#endif /* MBEDTLS_ECP_C */
6689
6690 if( mbedtls_ssl_check_cert_usage( chain,
6691 ciphersuite_info,
6692 ! ssl->conf->endpoint,
6693 &ssl->session_negotiate->verify_result ) != 0 )
6694 {
6695 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad certificate (usage extensions)" ) );
6696 if( ret == 0 )
6697 ret = MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE;
6698 }
6699
6700 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
6701 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
6702 * with details encoded in the verification flags. All other kinds
6703 * of error codes, including those from the user provided f_vrfy
6704 * functions, are treated as fatal and lead to a failure of
6705 * ssl_parse_certificate even if verification was optional. */
6706 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
6707 ( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
6708 ret == MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE ) )
6709 {
6710 ret = 0;
6711 }
6712
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00006713 if( have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED )
Hanno Becker68636192019-02-05 14:36:34 +00006714 {
6715 MBEDTLS_SSL_DEBUG_MSG( 1, ( "got no CA chain" ) );
6716 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
6717 }
6718
6719 if( ret != 0 )
6720 {
6721 uint8_t alert;
6722
6723 /* The certificate may have been rejected for several reasons.
6724 Pick one and send the corresponding alert. Which alert to send
6725 may be a subject of debate in some cases. */
6726 if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER )
6727 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
6728 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH )
6729 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
6730 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE )
6731 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6732 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE )
6733 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6734 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE )
6735 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6736 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK )
6737 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6738 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY )
6739 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
6740 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED )
6741 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
6742 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED )
6743 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
6744 else if( ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED )
6745 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
6746 else
6747 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
6748 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6749 alert );
6750 }
6751
6752#if defined(MBEDTLS_DEBUG_C)
6753 if( ssl->session_negotiate->verify_result != 0 )
6754 {
6755 MBEDTLS_SSL_DEBUG_MSG( 3, ( "! Certificate verification flags %x",
6756 ssl->session_negotiate->verify_result ) );
6757 }
6758 else
6759 {
6760 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Certificate verification flags clear" ) );
6761 }
6762#endif /* MBEDTLS_DEBUG_C */
6763
6764 return( ret );
6765}
6766
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006767#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
6768static int ssl_remember_peer_crt_digest( mbedtls_ssl_context *ssl,
6769 unsigned char *start, size_t len )
6770{
6771 int ret;
6772 /* Remember digest of the peer's end-CRT. */
6773 ssl->session_negotiate->peer_cert_digest =
6774 mbedtls_calloc( 1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN );
6775 if( ssl->session_negotiate->peer_cert_digest == NULL )
6776 {
6777 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6778 sizeof( MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN ) ) );
6779 mbedtls_ssl_send_alert_message( ssl,
6780 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6781 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
6782
6783 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
6784 }
6785
6786 ret = mbedtls_md( mbedtls_md_info_from_type(
6787 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE ),
6788 start, len,
6789 ssl->session_negotiate->peer_cert_digest );
6790
6791 ssl->session_negotiate->peer_cert_digest_type =
6792 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
6793 ssl->session_negotiate->peer_cert_digest_len =
6794 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
6795
6796 return( ret );
6797}
6798
6799static int ssl_remember_peer_pubkey( mbedtls_ssl_context *ssl,
6800 unsigned char *start, size_t len )
6801{
6802 unsigned char *end = start + len;
6803 int ret;
6804
6805 /* Make a copy of the peer's raw public key. */
6806 mbedtls_pk_init( &ssl->handshake->peer_pubkey );
6807 ret = mbedtls_pk_parse_subpubkey( &start, end,
6808 &ssl->handshake->peer_pubkey );
6809 if( ret != 0 )
6810 {
6811 /* We should have parsed the public key before. */
6812 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
6813 }
6814
6815 return( 0 );
6816}
6817#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6818
Hanno Becker68636192019-02-05 14:36:34 +00006819int mbedtls_ssl_parse_certificate( mbedtls_ssl_context *ssl )
6820{
6821 int ret = 0;
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006822 int crt_expected;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006823#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
6824 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
6825 ? ssl->handshake->sni_authmode
6826 : ssl->conf->authmode;
6827#else
6828 const int authmode = ssl->conf->authmode;
6829#endif
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006830 void *rs_ctx = NULL;
Hanno Becker3dad3112019-02-05 17:19:52 +00006831 mbedtls_x509_crt *chain = NULL;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006832
6833 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse certificate" ) );
6834
Hanno Becker28f2fcd2019-02-07 10:11:07 +00006835 crt_expected = ssl_parse_certificate_coordinate( ssl, authmode );
6836 if( crt_expected == SSL_CERTIFICATE_SKIP )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006837 {
6838 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= skip parse certificate" ) );
Hanno Becker6bdfab22019-02-05 13:11:17 +00006839 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006840 }
6841
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006842#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6843 if( ssl->handshake->ecrs_enabled &&
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006844 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify )
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006845 {
Hanno Becker3dad3112019-02-05 17:19:52 +00006846 chain = ssl->handshake->ecrs_peer_cert;
6847 ssl->handshake->ecrs_peer_cert = NULL;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006848 goto crt_verify;
6849 }
6850#endif
6851
Manuel Pégourié-Gonnard125af942018-09-11 11:08:12 +02006852 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006853 {
6854 /* mbedtls_ssl_read_record may have sent an alert already. We
6855 let it decide whether to alert. */
6856 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Hanno Becker3dad3112019-02-05 17:19:52 +00006857 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006858 }
6859
Hanno Becker4a55f632019-02-05 12:49:06 +00006860#if defined(MBEDTLS_SSL_SRV_C)
6861 if( ssl_srv_check_client_no_crt_notification( ssl ) == 0 )
6862 {
6863 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
Hanno Becker4a55f632019-02-05 12:49:06 +00006864
6865 if( authmode == MBEDTLS_SSL_VERIFY_OPTIONAL )
Hanno Becker6bdfab22019-02-05 13:11:17 +00006866 ret = 0;
6867 else
6868 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Hanno Becker4a55f632019-02-05 12:49:06 +00006869
Hanno Becker6bdfab22019-02-05 13:11:17 +00006870 goto exit;
Hanno Becker4a55f632019-02-05 12:49:06 +00006871 }
6872#endif /* MBEDTLS_SSL_SRV_C */
6873
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006874 /* Clear existing peer CRT structure in case we tried to
6875 * reuse a session but it failed, and allocate a new one. */
Hanno Becker7a955a02019-02-05 13:08:01 +00006876 ssl_clear_peer_cert( ssl->session_negotiate );
Hanno Becker3dad3112019-02-05 17:19:52 +00006877
6878 chain = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) );
6879 if( chain == NULL )
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006880 {
6881 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed",
6882 sizeof( mbedtls_x509_crt ) ) );
6883 mbedtls_ssl_send_alert_message( ssl,
6884 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6885 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Hanno Becker7a955a02019-02-05 13:08:01 +00006886
Hanno Becker3dad3112019-02-05 17:19:52 +00006887 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6888 goto exit;
6889 }
6890 mbedtls_x509_crt_init( chain );
6891
6892 ret = ssl_parse_certificate_chain( ssl, chain );
Hanno Beckerc7bd7802019-02-05 15:37:23 +00006893 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006894 goto exit;
Manuel Pégourié-Gonnardfed37ed2017-08-15 13:27:41 +02006895
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006896#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6897 if( ssl->handshake->ecrs_enabled)
Manuel Pégourié-Gonnard0b23f162017-08-24 12:08:33 +02006898 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Manuel Pégourié-Gonnard3bf49c42017-08-15 13:47:06 +02006899
6900crt_verify:
6901 if( ssl->handshake->ecrs_enabled)
6902 rs_ctx = &ssl->handshake->ecrs_ctx;
6903#endif
6904
Hanno Becker68636192019-02-05 14:36:34 +00006905 ret = ssl_parse_certificate_verify( ssl, authmode,
Hanno Becker3dad3112019-02-05 17:19:52 +00006906 chain, rs_ctx );
Hanno Becker68636192019-02-05 14:36:34 +00006907 if( ret != 0 )
Hanno Becker3dad3112019-02-05 17:19:52 +00006908 goto exit;
Paul Bakker5121ce52009-01-03 21:22:43 +00006909
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006910#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006911 {
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006912 unsigned char *crt_start, *pk_start;
6913 size_t crt_len, pk_len;
Hanno Becker3dad3112019-02-05 17:19:52 +00006914
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006915 /* We parse the CRT chain without copying, so
6916 * these pointers point into the input buffer,
6917 * and are hence still valid after freeing the
6918 * CRT chain. */
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006919
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006920 crt_start = chain->raw.p;
6921 crt_len = chain->raw.len;
Hanno Becker6bbd94c2019-02-05 17:02:28 +00006922
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006923 pk_start = chain->pk_raw.p;
6924 pk_len = chain->pk_raw.len;
6925
6926 /* Free the CRT structures before computing
6927 * digest and copying the peer's public key. */
6928 mbedtls_x509_crt_free( chain );
6929 mbedtls_free( chain );
6930 chain = NULL;
6931
6932 ret = ssl_remember_peer_crt_digest( ssl, crt_start, crt_len );
Hanno Beckera2747532019-02-06 16:19:04 +00006933 if( ret != 0 )
Hanno Beckera2747532019-02-06 16:19:04 +00006934 goto exit;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006935
6936 ret = ssl_remember_peer_pubkey( ssl, pk_start, pk_len );
6937 if( ret != 0 )
6938 goto exit;
Hanno Beckera2747532019-02-06 16:19:04 +00006939 }
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006940#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6941 /* Pass ownership to session structure. */
Hanno Becker3dad3112019-02-05 17:19:52 +00006942 ssl->session_negotiate->peer_cert = chain;
6943 chain = NULL;
Hanno Becker6b8fbab2019-02-08 14:59:05 +00006944#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Hanno Becker3dad3112019-02-05 17:19:52 +00006945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006946 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse certificate" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006947
Hanno Becker6bdfab22019-02-05 13:11:17 +00006948exit:
6949
Hanno Becker3dad3112019-02-05 17:19:52 +00006950 if( ret == 0 )
6951 ssl->state++;
6952
6953#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
6954 if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
6955 {
6956 ssl->handshake->ecrs_peer_cert = chain;
6957 chain = NULL;
6958 }
6959#endif
6960
6961 if( chain != NULL )
6962 {
6963 mbedtls_x509_crt_free( chain );
6964 mbedtls_free( chain );
6965 }
6966
Paul Bakker5121ce52009-01-03 21:22:43 +00006967 return( ret );
6968}
Hanno Becker21489932019-02-05 13:20:55 +00006969#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Paul Bakker5121ce52009-01-03 21:22:43 +00006970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006971int mbedtls_ssl_write_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006972{
6973 int ret;
6974
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006975 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006976
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006977 ssl->out_msgtype = MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC;
Paul Bakker5121ce52009-01-03 21:22:43 +00006978 ssl->out_msglen = 1;
6979 ssl->out_msg[0] = 1;
6980
Paul Bakker5121ce52009-01-03 21:22:43 +00006981 ssl->state++;
6982
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006983 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00006984 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02006985 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00006986 return( ret );
6987 }
6988
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006989 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006990
6991 return( 0 );
6992}
6993
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006994int mbedtls_ssl_parse_change_cipher_spec( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00006995{
6996 int ret;
6997
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006998 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00006999
Hanno Becker327c93b2018-08-15 13:56:18 +01007000 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007001 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007002 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007003 return( ret );
7004 }
7005
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007006 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC )
Paul Bakker5121ce52009-01-03 21:22:43 +00007007 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007008 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad change cipher spec message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007009 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7010 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007011 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007012 }
7013
Hanno Beckere678eaa2018-08-21 14:57:46 +01007014 /* CCS records are only accepted if they have length 1 and content '1',
7015 * so we don't need to check this here. */
Paul Bakker5121ce52009-01-03 21:22:43 +00007016
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007017 /*
7018 * Switch to our negotiated transform and session parameters for inbound
7019 * data.
7020 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007021 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for inbound data" ) );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007022 ssl->transform_in = ssl->transform_negotiate;
7023 ssl->session_in = ssl->session_negotiate;
7024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007025#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007026 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007027 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007028#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007029 ssl_dtls_replay_reset( ssl );
7030#endif
7031
7032 /* Increment epoch */
7033 if( ++ssl->in_epoch == 0 )
7034 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007035 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007036 /* This is highly unlikely to happen for legitimate reasons, so
7037 treat it as an attack and don't send an alert. */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007038 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007039 }
7040 }
7041 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007042#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007043 memset( ssl->in_ctr, 0, 8 );
7044
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007045 ssl_update_in_pointers( ssl, ssl->transform_negotiate );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007047#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7048 if( mbedtls_ssl_hw_record_activate != NULL )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007049 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007050 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_INBOUND ) ) != 0 )
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007052 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007053 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7054 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007055 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02007056 }
7057 }
7058#endif
7059
Paul Bakker5121ce52009-01-03 21:22:43 +00007060 ssl->state++;
7061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007062 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse change cipher spec" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007063
7064 return( 0 );
7065}
7066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007067void mbedtls_ssl_optimize_checksum( mbedtls_ssl_context *ssl,
7068 const mbedtls_ssl_ciphersuite_t *ciphersuite_info )
Paul Bakker380da532012-04-18 16:10:25 +00007069{
Paul Bakkerfb08fd22013-08-27 15:06:26 +02007070 ((void) ciphersuite_info);
Paul Bakker769075d2012-11-24 11:26:46 +01007071
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007072#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7073 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7074 if( ssl->minor_ver < MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker48916f92012-09-16 19:57:18 +00007075 ssl->handshake->update_checksum = ssl_update_checksum_md5sha1;
Paul Bakker380da532012-04-18 16:10:25 +00007076 else
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007077#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007078#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7079#if defined(MBEDTLS_SHA512_C)
7080 if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007081 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
7082 else
7083#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007084#if defined(MBEDTLS_SHA256_C)
7085 if( ciphersuite_info->mac != MBEDTLS_MD_SHA384 )
Paul Bakker48916f92012-09-16 19:57:18 +00007086 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007087 else
7088#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007089#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007090 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007091 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007092 return;
Manuel Pégourié-Gonnard61edffe2014-04-11 17:07:31 +02007093 }
Paul Bakker380da532012-04-18 16:10:25 +00007094}
Paul Bakkerf7abd422013-04-16 13:15:56 +02007095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007096void mbedtls_ssl_reset_checksum( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007097{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007098#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7099 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007100 mbedtls_md5_starts_ret( &ssl->handshake->fin_md5 );
7101 mbedtls_sha1_starts_ret( &ssl->handshake->fin_sha1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007102#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007103#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7104#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007106 psa_hash_abort( &ssl->handshake->fin_sha256_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007107 psa_hash_setup( &ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7108#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007109 mbedtls_sha256_starts_ret( &ssl->handshake->fin_sha256, 0 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007110#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007111#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007112#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007113#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek2ad22972019-01-30 03:32:12 -05007114 psa_hash_abort( &ssl->handshake->fin_sha384_psa );
Andrzej Kurek972fba52019-01-30 03:29:12 -05007115 psa_hash_setup( &ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007116#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007117 mbedtls_sha512_starts_ret( &ssl->handshake->fin_sha512, 1 );
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007118#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007120#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Manuel Pégourié-Gonnard67427c02014-07-11 13:45:34 +02007121}
7122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007123static void ssl_update_checksum_start( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007124 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007125{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007126#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7127 defined(MBEDTLS_SSL_PROTO_TLS1_1)
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007128 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7129 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007130#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007131#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7132#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007133#if defined(MBEDTLS_USE_PSA_CRYPTO)
7134 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7135#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007136 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007137#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007138#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007139#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007141 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007142#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007143 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Paul Bakker769075d2012-11-24 11:26:46 +01007144#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007145#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007146#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007147}
7148
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007149#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7150 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7151static void ssl_update_checksum_md5sha1( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007152 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007153{
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007154 mbedtls_md5_update_ret( &ssl->handshake->fin_md5 , buf, len );
7155 mbedtls_sha1_update_ret( &ssl->handshake->fin_sha1, buf, len );
Paul Bakker380da532012-04-18 16:10:25 +00007156}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007157#endif
Paul Bakker380da532012-04-18 16:10:25 +00007158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007159#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7160#if defined(MBEDTLS_SHA256_C)
7161static void ssl_update_checksum_sha256( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007162 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007163{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007164#if defined(MBEDTLS_USE_PSA_CRYPTO)
7165 psa_hash_update( &ssl->handshake->fin_sha256_psa, buf, len );
7166#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007167 mbedtls_sha256_update_ret( &ssl->handshake->fin_sha256, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007168#endif
Paul Bakker380da532012-04-18 16:10:25 +00007169}
Paul Bakkerd2f068e2013-08-27 21:19:20 +02007170#endif
Paul Bakker380da532012-04-18 16:10:25 +00007171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007172#if defined(MBEDTLS_SHA512_C)
7173static void ssl_update_checksum_sha384( mbedtls_ssl_context *ssl,
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007174 const unsigned char *buf, size_t len )
Paul Bakker380da532012-04-18 16:10:25 +00007175{
Andrzej Kurekeb342242019-01-29 09:14:33 -05007176#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007177 psa_hash_update( &ssl->handshake->fin_sha384_psa, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007178#else
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007179 mbedtls_sha512_update_ret( &ssl->handshake->fin_sha512, buf, len );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007180#endif
Paul Bakker380da532012-04-18 16:10:25 +00007181}
Paul Bakker769075d2012-11-24 11:26:46 +01007182#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007183#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker380da532012-04-18 16:10:25 +00007184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007185#if defined(MBEDTLS_SSL_PROTO_SSL3)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007186static void ssl_calc_finished_ssl(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007187 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007188{
Paul Bakker3c2122f2013-06-24 19:03:14 +02007189 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007190 mbedtls_md5_context md5;
7191 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007192
Paul Bakker5121ce52009-01-03 21:22:43 +00007193 unsigned char padbuf[48];
7194 unsigned char md5sum[16];
7195 unsigned char sha1sum[20];
7196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007197 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007198 if( !session )
7199 session = ssl->session;
7200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007201 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished ssl" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007202
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007203 mbedtls_md5_init( &md5 );
7204 mbedtls_sha1_init( &sha1 );
7205
7206 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7207 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007208
7209 /*
7210 * SSLv3:
7211 * hash =
7212 * MD5( master + pad2 +
7213 * MD5( handshake + sender + master + pad1 ) )
7214 * + SHA1( master + pad2 +
7215 * SHA1( handshake + sender + master + pad1 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +00007216 */
7217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007218#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007219 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7220 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007221#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007223#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007224 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7225 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007226#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007228 sender = ( from == MBEDTLS_SSL_IS_CLIENT ) ? "CLNT"
Paul Bakker3c2122f2013-06-24 19:03:14 +02007229 : "SRVR";
Paul Bakker5121ce52009-01-03 21:22:43 +00007230
Paul Bakker1ef83d62012-04-11 12:09:53 +00007231 memset( padbuf, 0x36, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007232
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007233 mbedtls_md5_update_ret( &md5, (const unsigned char *) sender, 4 );
7234 mbedtls_md5_update_ret( &md5, session->master, 48 );
7235 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7236 mbedtls_md5_finish_ret( &md5, md5sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007237
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007238 mbedtls_sha1_update_ret( &sha1, (const unsigned char *) sender, 4 );
7239 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7240 mbedtls_sha1_update_ret( &sha1, padbuf, 40 );
7241 mbedtls_sha1_finish_ret( &sha1, sha1sum );
Paul Bakker5121ce52009-01-03 21:22:43 +00007242
Paul Bakker1ef83d62012-04-11 12:09:53 +00007243 memset( padbuf, 0x5C, 48 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007244
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007245 mbedtls_md5_starts_ret( &md5 );
7246 mbedtls_md5_update_ret( &md5, session->master, 48 );
7247 mbedtls_md5_update_ret( &md5, padbuf, 48 );
7248 mbedtls_md5_update_ret( &md5, md5sum, 16 );
7249 mbedtls_md5_finish_ret( &md5, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +00007250
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007251 mbedtls_sha1_starts_ret( &sha1 );
7252 mbedtls_sha1_update_ret( &sha1, session->master, 48 );
7253 mbedtls_sha1_update_ret( &sha1, padbuf , 40 );
7254 mbedtls_sha1_update_ret( &sha1, sha1sum, 20 );
7255 mbedtls_sha1_finish_ret( &sha1, buf + 16 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007256
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007257 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, 36 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007258
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007259 mbedtls_md5_free( &md5 );
7260 mbedtls_sha1_free( &sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007261
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007262 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
7263 mbedtls_platform_zeroize( md5sum, sizeof( md5sum ) );
7264 mbedtls_platform_zeroize( sha1sum, sizeof( sha1sum ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007265
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007266 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007267}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007268#endif /* MBEDTLS_SSL_PROTO_SSL3 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007269
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007270#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
Paul Bakker1ef83d62012-04-11 12:09:53 +00007271static void ssl_calc_finished_tls(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007272 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker5121ce52009-01-03 21:22:43 +00007273{
Paul Bakker1ef83d62012-04-11 12:09:53 +00007274 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007275 const char *sender;
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007276 mbedtls_md5_context md5;
7277 mbedtls_sha1_context sha1;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007278 unsigned char padbuf[36];
Paul Bakker5121ce52009-01-03 21:22:43 +00007279
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007280 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007281 if( !session )
7282 session = ssl->session;
7283
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007284 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007285
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007286 mbedtls_md5_init( &md5 );
7287 mbedtls_sha1_init( &sha1 );
7288
7289 mbedtls_md5_clone( &md5, &ssl->handshake->fin_md5 );
7290 mbedtls_sha1_clone( &sha1, &ssl->handshake->fin_sha1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007291
Paul Bakker1ef83d62012-04-11 12:09:53 +00007292 /*
7293 * TLSv1:
7294 * hash = PRF( master, finished_label,
7295 * MD5( handshake ) + SHA1( handshake ) )[0..11]
7296 */
Paul Bakker5121ce52009-01-03 21:22:43 +00007297
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007298#if !defined(MBEDTLS_MD5_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007299 MBEDTLS_SSL_DEBUG_BUF( 4, "finished md5 state", (unsigned char *)
7300 md5.state, sizeof( md5.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007301#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007303#if !defined(MBEDTLS_SHA1_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007304 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha1 state", (unsigned char *)
7305 sha1.state, sizeof( sha1.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007306#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007307
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007308 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
Paul Bakker3c2122f2013-06-24 19:03:14 +02007309 ? "client finished"
7310 : "server finished";
Paul Bakker1ef83d62012-04-11 12:09:53 +00007311
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007312 mbedtls_md5_finish_ret( &md5, padbuf );
7313 mbedtls_sha1_finish_ret( &sha1, padbuf + 16 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007314
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007315 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007316 padbuf, 36, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007317
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007318 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007319
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007320 mbedtls_md5_free( &md5 );
7321 mbedtls_sha1_free( &sha1 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007322
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007323 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007324
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007325 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007326}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007327#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007329#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7330#if defined(MBEDTLS_SHA256_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007331static void ssl_calc_finished_tls_sha256(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007332 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007333{
7334 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007335 const char *sender;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007336 unsigned char padbuf[32];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007337#if defined(MBEDTLS_USE_PSA_CRYPTO)
7338 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007339 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007340 psa_status_t status;
7341#else
7342 mbedtls_sha256_context sha256;
7343#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007344
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007345 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007346 if( !session )
7347 session = ssl->session;
7348
Andrzej Kurekeb342242019-01-29 09:14:33 -05007349 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7350 ? "client finished"
7351 : "server finished";
7352
7353#if defined(MBEDTLS_USE_PSA_CRYPTO)
7354 sha256_psa = psa_hash_operation_init();
7355
7356 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha256" ) );
7357
7358 status = psa_hash_clone( &ssl->handshake->fin_sha256_psa, &sha256_psa );
7359 if( status != PSA_SUCCESS )
7360 {
7361 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7362 return;
7363 }
7364
7365 status = psa_hash_finish( &sha256_psa, padbuf, sizeof( padbuf ), &hash_size );
7366 if( status != PSA_SUCCESS )
7367 {
7368 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7369 return;
7370 }
7371 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 32 );
7372#else
7373
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007374 mbedtls_sha256_init( &sha256 );
7375
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007376 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha256" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007377
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007378 mbedtls_sha256_clone( &sha256, &ssl->handshake->fin_sha256 );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007379
7380 /*
7381 * TLSv1.2:
7382 * hash = PRF( master, finished_label,
7383 * Hash( handshake ) )[0.11]
7384 */
7385
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007386#if !defined(MBEDTLS_SHA256_ALT)
7387 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha2 state", (unsigned char *)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007388 sha256.state, sizeof( sha256.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007389#endif
Paul Bakker1ef83d62012-04-11 12:09:53 +00007390
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007391 mbedtls_sha256_finish_ret( &sha256, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007392 mbedtls_sha256_free( &sha256 );
7393#endif /* MBEDTLS_USE_PSA_CRYPTO */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007394
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007395 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007396 padbuf, 32, buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007397
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007398 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007399
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007400 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007401
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007402 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007403}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007404#endif /* MBEDTLS_SHA256_C */
Paul Bakker1ef83d62012-04-11 12:09:53 +00007405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007406#if defined(MBEDTLS_SHA512_C)
Paul Bakkerca4ab492012-04-18 14:23:57 +00007407static void ssl_calc_finished_tls_sha384(
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007408 mbedtls_ssl_context *ssl, unsigned char *buf, int from )
Paul Bakkerca4ab492012-04-18 14:23:57 +00007409{
7410 int len = 12;
Paul Bakker3c2122f2013-06-24 19:03:14 +02007411 const char *sender;
Paul Bakkerca4ab492012-04-18 14:23:57 +00007412 unsigned char padbuf[48];
Andrzej Kurekeb342242019-01-29 09:14:33 -05007413#if defined(MBEDTLS_USE_PSA_CRYPTO)
7414 size_t hash_size;
Jaeden Amero34973232019-02-20 10:32:28 +00007415 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
Andrzej Kurekeb342242019-01-29 09:14:33 -05007416 psa_status_t status;
7417#else
7418 mbedtls_sha512_context sha512;
7419#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007420
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007421 mbedtls_ssl_session *session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007422 if( !session )
7423 session = ssl->session;
7424
Andrzej Kurekeb342242019-01-29 09:14:33 -05007425 sender = ( from == MBEDTLS_SSL_IS_CLIENT )
7426 ? "client finished"
7427 : "server finished";
7428
7429#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007430 sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -05007431
7432 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc PSA finished tls sha384" ) );
7433
Andrzej Kurek972fba52019-01-30 03:29:12 -05007434 status = psa_hash_clone( &ssl->handshake->fin_sha384_psa, &sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007435 if( status != PSA_SUCCESS )
7436 {
7437 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash clone failed" ) );
7438 return;
7439 }
7440
Andrzej Kurek972fba52019-01-30 03:29:12 -05007441 status = psa_hash_finish( &sha384_psa, padbuf, sizeof( padbuf ), &hash_size );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007442 if( status != PSA_SUCCESS )
7443 {
7444 MBEDTLS_SSL_DEBUG_MSG( 2, ( "PSA hash finish failed" ) );
7445 return;
7446 }
7447 MBEDTLS_SSL_DEBUG_BUF( 3, "PSA calculated padbuf", padbuf, 48 );
7448#else
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007449 mbedtls_sha512_init( &sha512 );
7450
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007451 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> calc finished tls sha384" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007452
Manuel Pégourié-Gonnard001f2b62015-07-06 16:21:13 +02007453 mbedtls_sha512_clone( &sha512, &ssl->handshake->fin_sha512 );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007454
7455 /*
7456 * TLSv1.2:
7457 * hash = PRF( master, finished_label,
7458 * Hash( handshake ) )[0.11]
7459 */
7460
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007461#if !defined(MBEDTLS_SHA512_ALT)
Manuel Pégourié-Gonnardc0bf01e2015-07-06 16:11:18 +02007462 MBEDTLS_SSL_DEBUG_BUF( 4, "finished sha512 state", (unsigned char *)
7463 sha512.state, sizeof( sha512.state ) );
Paul Bakker90995b52013-06-24 19:20:35 +02007464#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007465
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007466 mbedtls_sha512_finish_ret( &sha512, padbuf );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007467 mbedtls_sha512_free( &sha512 );
7468#endif
Paul Bakkerca4ab492012-04-18 14:23:57 +00007469
Paul Bakkerb6c5d2e2013-06-25 16:25:17 +02007470 ssl->handshake->tls_prf( session->master, 48, sender,
Paul Bakker48916f92012-09-16 19:57:18 +00007471 padbuf, 48, buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007473 MBEDTLS_SSL_DEBUG_BUF( 3, "calc finished result", buf, len );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007474
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05007475 mbedtls_platform_zeroize( padbuf, sizeof( padbuf ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007477 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= calc finished" ) );
Paul Bakkerca4ab492012-04-18 14:23:57 +00007478}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007479#endif /* MBEDTLS_SHA512_C */
7480#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkerca4ab492012-04-18 14:23:57 +00007481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007482static void ssl_handshake_wrapup_free_hs_transform( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007483{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007484 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup: final free" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007485
7486 /*
7487 * Free our handshake params
7488 */
Gilles Peskine9b562d52018-04-25 20:32:43 +02007489 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007490 mbedtls_free( ssl->handshake );
Paul Bakker48916f92012-09-16 19:57:18 +00007491 ssl->handshake = NULL;
7492
7493 /*
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007494 * Free the previous transform and swith in the current one
Paul Bakker48916f92012-09-16 19:57:18 +00007495 */
7496 if( ssl->transform )
7497 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007498 mbedtls_ssl_transform_free( ssl->transform );
7499 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00007500 }
7501 ssl->transform = ssl->transform_negotiate;
7502 ssl->transform_negotiate = NULL;
7503
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007504 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup: final free" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007505}
7506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007507void mbedtls_ssl_handshake_wrapup( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007508{
7509 int resume = ssl->handshake->resume;
7510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007511 MBEDTLS_SSL_DEBUG_MSG( 3, ( "=> handshake wrapup" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007513#if defined(MBEDTLS_SSL_RENEGOTIATION)
7514 if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007515 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007516 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007517 ssl->renego_records_seen = 0;
7518 }
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007519#endif
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007520
7521 /*
7522 * Free the previous session and switch in the current one
7523 */
Paul Bakker0a597072012-09-25 21:55:46 +00007524 if( ssl->session )
7525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007526#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard1a034732014-11-04 17:36:18 +01007527 /* RFC 7366 3.1: keep the EtM state */
7528 ssl->session_negotiate->encrypt_then_mac =
7529 ssl->session->encrypt_then_mac;
7530#endif
7531
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007532 mbedtls_ssl_session_free( ssl->session );
7533 mbedtls_free( ssl->session );
Paul Bakker0a597072012-09-25 21:55:46 +00007534 }
7535 ssl->session = ssl->session_negotiate;
Paul Bakker48916f92012-09-16 19:57:18 +00007536 ssl->session_negotiate = NULL;
7537
Paul Bakker0a597072012-09-25 21:55:46 +00007538 /*
7539 * Add cache entry
7540 */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007541 if( ssl->conf->f_set_cache != NULL &&
Manuel Pégourié-Gonnard12ad7982015-06-18 15:50:37 +02007542 ssl->session->id_len != 0 &&
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007543 resume == 0 )
7544 {
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01007545 if( ssl->conf->f_set_cache( ssl->conf->p_cache, ssl->session ) != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007546 MBEDTLS_SSL_DEBUG_MSG( 1, ( "cache did not store session" ) );
Manuel Pégourié-Gonnardc086cce2013-08-02 14:13:02 +02007547 }
Paul Bakker0a597072012-09-25 21:55:46 +00007548
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007549#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007550 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007551 ssl->handshake->flight != NULL )
7552 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02007553 /* Cancel handshake timer */
7554 ssl_set_timer( ssl, 0 );
7555
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007556 /* Keep last flight around in case we need to resend it:
7557 * we need the handshake and transform structures for that */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007558 MBEDTLS_SSL_DEBUG_MSG( 3, ( "skip freeing handshake and transform" ) );
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02007559 }
7560 else
7561#endif
7562 ssl_handshake_wrapup_free_hs_transform( ssl );
7563
Paul Bakker48916f92012-09-16 19:57:18 +00007564 ssl->state++;
7565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007566 MBEDTLS_SSL_DEBUG_MSG( 3, ( "<= handshake wrapup" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00007567}
7568
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007569int mbedtls_ssl_write_finished( mbedtls_ssl_context *ssl )
Paul Bakker1ef83d62012-04-11 12:09:53 +00007570{
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007571 int ret, hash_len;
Paul Bakker1ef83d62012-04-11 12:09:53 +00007572
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007573 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write finished" ) );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007574
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007575 ssl_update_out_pointers( ssl, ssl->transform_negotiate );
Paul Bakker92be97b2013-01-02 17:30:03 +01007576
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007577 ssl->handshake->calc_finished( ssl, ssl->out_msg + 4, ssl->conf->endpoint );
Paul Bakker1ef83d62012-04-11 12:09:53 +00007578
Manuel Pégourié-Gonnard214a8482016-02-22 11:27:26 +01007579 /*
7580 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7581 * may define some other value. Currently (early 2016), no defined
7582 * ciphersuite does this (and this is unlikely to change as activity has
7583 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7584 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007585 hash_len = ( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 ) ? 36 : 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007586
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007587#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007588 ssl->verify_data_len = hash_len;
7589 memcpy( ssl->own_verify_data, ssl->out_msg + 4, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007590#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007591
Paul Bakker5121ce52009-01-03 21:22:43 +00007592 ssl->out_msglen = 4 + hash_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007593 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7594 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
Paul Bakker5121ce52009-01-03 21:22:43 +00007595
7596 /*
7597 * In case of session resuming, invert the client and server
7598 * ChangeCipherSpec messages order.
7599 */
Paul Bakker0a597072012-09-25 21:55:46 +00007600 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007601 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007602#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007603 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007604 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007605#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007606#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007607 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007608 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007609#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007610 }
7611 else
7612 ssl->state++;
7613
Paul Bakker48916f92012-09-16 19:57:18 +00007614 /*
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02007615 * Switch to our negotiated transform and session parameters for outbound
7616 * data.
Paul Bakker48916f92012-09-16 19:57:18 +00007617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007618 MBEDTLS_SSL_DEBUG_MSG( 3, ( "switching to new transform spec for outbound data" ) );
Manuel Pégourié-Gonnard5afb1672014-02-16 18:33:22 +01007619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007620#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007621 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007622 {
7623 unsigned char i;
7624
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007625 /* Remember current epoch settings for resending */
7626 ssl->handshake->alt_transform_out = ssl->transform_out;
Hanno Becker19859472018-08-06 09:40:20 +01007627 memcpy( ssl->handshake->alt_out_ctr, ssl->cur_out_ctr, 8 );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007628
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007629 /* Set sequence_number to zero */
Hanno Becker19859472018-08-06 09:40:20 +01007630 memset( ssl->cur_out_ctr + 2, 0, 6 );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007631
7632 /* Increment epoch */
7633 for( i = 2; i > 0; i-- )
Hanno Becker19859472018-08-06 09:40:20 +01007634 if( ++ssl->cur_out_ctr[i - 1] != 0 )
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007635 break;
7636
7637 /* The loop goes to its end iff the counter is wrapping */
7638 if( i == 0 )
7639 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007640 MBEDTLS_SSL_DEBUG_MSG( 1, ( "DTLS epoch would wrap" ) );
7641 return( MBEDTLS_ERR_SSL_COUNTER_WRAPPING );
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007642 }
7643 }
7644 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007645#endif /* MBEDTLS_SSL_PROTO_DTLS */
Hanno Becker19859472018-08-06 09:40:20 +01007646 memset( ssl->cur_out_ctr, 0, 8 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007647
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007648 ssl->transform_out = ssl->transform_negotiate;
7649 ssl->session_out = ssl->session_negotiate;
7650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007651#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
7652 if( mbedtls_ssl_hw_record_activate != NULL )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007653 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007654 if( ( ret = mbedtls_ssl_hw_record_activate( ssl, MBEDTLS_SSL_CHANNEL_OUTBOUND ) ) != 0 )
Paul Bakker07eb38b2012-12-19 14:42:06 +01007655 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007656 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_activate", ret );
7657 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker07eb38b2012-12-19 14:42:06 +01007658 }
7659 }
7660#endif
7661
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +02007664 mbedtls_ssl_send_flight_completed( ssl );
Manuel Pégourié-Gonnard7de3c9e2014-09-29 15:29:48 +02007665#endif
7666
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007667 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007668 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02007669 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007670 return( ret );
7671 }
7672
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02007673#if defined(MBEDTLS_SSL_PROTO_DTLS)
7674 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7675 ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
7676 {
7677 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flight_transmit", ret );
7678 return( ret );
7679 }
7680#endif
7681
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007682 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007683
7684 return( 0 );
7685}
7686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007687#if defined(MBEDTLS_SSL_PROTO_SSL3)
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007688#define SSL_MAX_HASH_LEN 36
7689#else
7690#define SSL_MAX_HASH_LEN 12
7691#endif
7692
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007693int mbedtls_ssl_parse_finished( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00007694{
Paul Bakker23986e52011-04-24 08:57:21 +00007695 int ret;
Manuel Pégourié-Gonnard879a4f92014-07-11 22:31:12 +02007696 unsigned int hash_len;
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007697 unsigned char buf[SSL_MAX_HASH_LEN];
Paul Bakker5121ce52009-01-03 21:22:43 +00007698
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007699 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007700
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007701 ssl->handshake->calc_finished( ssl, buf, ssl->conf->endpoint ^ 1 );
Paul Bakker5121ce52009-01-03 21:22:43 +00007702
Hanno Becker327c93b2018-08-15 13:56:18 +01007703 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007704 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007705 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00007706 return( ret );
7707 }
7708
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007709 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker5121ce52009-01-03 21:22:43 +00007710 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007711 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007712 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7713 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007714 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00007715 }
7716
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007717 /* There is currently no ciphersuite using another length with TLS 1.2 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007718#if defined(MBEDTLS_SSL_PROTO_SSL3)
7719 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Manuel Pégourié-Gonnardca6440b2014-09-10 12:39:54 +00007720 hash_len = 36;
7721 else
7722#endif
7723 hash_len = 12;
Paul Bakker5121ce52009-01-03 21:22:43 +00007724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007725 if( ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED ||
7726 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) + hash_len )
Paul Bakker5121ce52009-01-03 21:22:43 +00007727 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007728 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007729 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7730 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007731 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007732 }
7733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007734 if( mbedtls_ssl_safer_memcmp( ssl->in_msg + mbedtls_ssl_hs_hdr_len( ssl ),
Manuel Pégourié-Gonnard4abc3272014-09-10 12:02:46 +00007735 buf, hash_len ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007736 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007737 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad finished message" ) );
Gilles Peskine1cc8e342017-05-03 16:28:34 +02007738 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7739 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007740 return( MBEDTLS_ERR_SSL_BAD_HS_FINISHED );
Paul Bakker5121ce52009-01-03 21:22:43 +00007741 }
7742
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007743#if defined(MBEDTLS_SSL_RENEGOTIATION)
Paul Bakker48916f92012-09-16 19:57:18 +00007744 ssl->verify_data_len = hash_len;
7745 memcpy( ssl->peer_verify_data, buf, hash_len );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01007746#endif
Paul Bakker48916f92012-09-16 19:57:18 +00007747
Paul Bakker0a597072012-09-25 21:55:46 +00007748 if( ssl->handshake->resume != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00007749 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007750#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007751 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007752 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007753#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007754#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007755 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007756 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Manuel Pégourié-Gonnardd16d1cb2014-11-20 18:15:05 +01007757#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00007758 }
7759 else
7760 ssl->state++;
7761
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007762#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02007763 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007764 mbedtls_ssl_recv_flight_completed( ssl );
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007765#endif
7766
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007767 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= parse finished" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00007768
7769 return( 0 );
7770}
7771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007772static void ssl_handshake_params_init( mbedtls_ssl_handshake_params *handshake )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007773{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007774 memset( handshake, 0, sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007775
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007776#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
7777 defined(MBEDTLS_SSL_PROTO_TLS1_1)
7778 mbedtls_md5_init( &handshake->fin_md5 );
7779 mbedtls_sha1_init( &handshake->fin_sha1 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007780 mbedtls_md5_starts_ret( &handshake->fin_md5 );
7781 mbedtls_sha1_starts_ret( &handshake->fin_sha1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007782#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007783#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
7784#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007785#if defined(MBEDTLS_USE_PSA_CRYPTO)
7786 handshake->fin_sha256_psa = psa_hash_operation_init();
7787 psa_hash_setup( &handshake->fin_sha256_psa, PSA_ALG_SHA_256 );
7788#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007789 mbedtls_sha256_init( &handshake->fin_sha256 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007790 mbedtls_sha256_starts_ret( &handshake->fin_sha256, 0 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007791#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007792#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007793#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -05007794#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -05007795 handshake->fin_sha384_psa = psa_hash_operation_init();
7796 psa_hash_setup( &handshake->fin_sha384_psa, PSA_ALG_SHA_384 );
Andrzej Kurekeb342242019-01-29 09:14:33 -05007797#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007798 mbedtls_sha512_init( &handshake->fin_sha512 );
Gilles Peskine9e4f77c2018-01-22 11:48:08 +01007799 mbedtls_sha512_starts_ret( &handshake->fin_sha512, 1 );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007800#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05007801#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007802#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007803
7804 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +01007805
7806#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
7807 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
7808 mbedtls_ssl_sig_hash_set_init( &handshake->hash_algs );
7809#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007811#if defined(MBEDTLS_DHM_C)
7812 mbedtls_dhm_init( &handshake->dhm_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007813#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007814#if defined(MBEDTLS_ECDH_C)
7815 mbedtls_ecdh_init( &handshake->ecdh_ctx );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007816#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02007817#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007818 mbedtls_ecjpake_init( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02007819#if defined(MBEDTLS_SSL_CLI_C)
7820 handshake->ecjpake_cache = NULL;
7821 handshake->ecjpake_cache_len = 0;
7822#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02007823#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007824
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007825#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +02007826 mbedtls_x509_crt_restart_init( &handshake->ecrs_ctx );
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02007827#endif
7828
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02007829#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7830 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
7831#endif
Hanno Becker75173122019-02-06 16:18:31 +00007832
7833#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
7834 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7835 mbedtls_pk_init( &handshake->peer_pubkey );
7836#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007837}
7838
Hanno Beckera18d1322018-01-03 14:27:32 +00007839void mbedtls_ssl_transform_init( mbedtls_ssl_transform *transform )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007840{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007841 memset( transform, 0, sizeof(mbedtls_ssl_transform) );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007842
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007843 mbedtls_cipher_init( &transform->cipher_ctx_enc );
7844 mbedtls_cipher_init( &transform->cipher_ctx_dec );
Paul Bakker84bbeb52014-07-01 14:53:22 +02007845
Hanno Beckerd56ed242018-01-03 15:32:51 +00007846#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007847 mbedtls_md_init( &transform->md_ctx_enc );
7848 mbedtls_md_init( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +00007849#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007850}
7851
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007852void mbedtls_ssl_session_init( mbedtls_ssl_session *session )
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007853{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007854 memset( session, 0, sizeof(mbedtls_ssl_session) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007855}
7856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007857static int ssl_handshake_init( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00007858{
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007859 /* Clear old handshake information if present */
Paul Bakker48916f92012-09-16 19:57:18 +00007860 if( ssl->transform_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007861 mbedtls_ssl_transform_free( ssl->transform_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007862 if( ssl->session_negotiate )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007863 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007864 if( ssl->handshake )
Gilles Peskine9b562d52018-04-25 20:32:43 +02007865 mbedtls_ssl_handshake_free( ssl );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007866
7867 /*
7868 * Either the pointers are now NULL or cleared properly and can be freed.
7869 * Now allocate missing structures.
7870 */
7871 if( ssl->transform_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007872 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007873 ssl->transform_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_transform) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007874 }
Paul Bakker48916f92012-09-16 19:57:18 +00007875
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007876 if( ssl->session_negotiate == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007877 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007878 ssl->session_negotiate = mbedtls_calloc( 1, sizeof(mbedtls_ssl_session) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007879 }
Paul Bakker48916f92012-09-16 19:57:18 +00007880
Paul Bakker82788fb2014-10-20 13:59:19 +02007881 if( ssl->handshake == NULL )
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007882 {
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02007883 ssl->handshake = mbedtls_calloc( 1, sizeof(mbedtls_ssl_handshake_params) );
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02007884 }
Paul Bakker48916f92012-09-16 19:57:18 +00007885
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007886 /* All pointers should exist and can be directly freed without issue */
Paul Bakker48916f92012-09-16 19:57:18 +00007887 if( ssl->handshake == NULL ||
7888 ssl->transform_negotiate == NULL ||
7889 ssl->session_negotiate == NULL )
7890 {
Manuel Pégourié-Gonnardb2a18a22015-05-27 16:29:56 +02007891 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc() of ssl sub-contexts failed" ) );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007892
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007893 mbedtls_free( ssl->handshake );
7894 mbedtls_free( ssl->transform_negotiate );
7895 mbedtls_free( ssl->session_negotiate );
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007896
7897 ssl->handshake = NULL;
7898 ssl->transform_negotiate = NULL;
7899 ssl->session_negotiate = NULL;
7900
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02007901 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker48916f92012-09-16 19:57:18 +00007902 }
7903
Paul Bakkeraccaffe2014-06-26 13:37:14 +02007904 /* Initialize structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007905 mbedtls_ssl_session_init( ssl->session_negotiate );
Hanno Beckera18d1322018-01-03 14:27:32 +00007906 mbedtls_ssl_transform_init( ssl->transform_negotiate );
Paul Bakker968afaa2014-07-09 11:09:24 +02007907 ssl_handshake_params_init( ssl->handshake );
7908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7911 {
7912 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007913
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007914 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
7915 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
7916 else
7917 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02007918
7919 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02007920 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02007921#endif
7922
Paul Bakker48916f92012-09-16 19:57:18 +00007923 return( 0 );
7924}
7925
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007926#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007927/* Dummy cookie callbacks for defaults */
7928static int ssl_cookie_write_dummy( void *ctx,
7929 unsigned char **p, unsigned char *end,
7930 const unsigned char *cli_id, size_t cli_id_len )
7931{
7932 ((void) ctx);
7933 ((void) p);
7934 ((void) end);
7935 ((void) cli_id);
7936 ((void) cli_id_len);
7937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007938 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007939}
7940
7941static int ssl_cookie_check_dummy( void *ctx,
7942 const unsigned char *cookie, size_t cookie_len,
7943 const unsigned char *cli_id, size_t cli_id_len )
7944{
7945 ((void) ctx);
7946 ((void) cookie);
7947 ((void) cookie_len);
7948 ((void) cli_id);
7949 ((void) cli_id_len);
7950
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02007951 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007952}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02007953#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02007954
Hanno Becker5aa4e2c2018-08-06 09:26:08 +01007955/* Once ssl->out_hdr as the address of the beginning of the
7956 * next outgoing record is set, deduce the other pointers.
7957 *
7958 * Note: For TLS, we save the implicit record sequence number
7959 * (entering MAC computation) in the 8 bytes before ssl->out_hdr,
7960 * and the caller has to make sure there's space for this.
7961 */
7962
7963static void ssl_update_out_pointers( mbedtls_ssl_context *ssl,
7964 mbedtls_ssl_transform *transform )
7965{
7966#if defined(MBEDTLS_SSL_PROTO_DTLS)
7967 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
7968 {
7969 ssl->out_ctr = ssl->out_hdr + 3;
7970 ssl->out_len = ssl->out_hdr + 11;
7971 ssl->out_iv = ssl->out_hdr + 13;
7972 }
7973 else
7974#endif
7975 {
7976 ssl->out_ctr = ssl->out_hdr - 8;
7977 ssl->out_len = ssl->out_hdr + 3;
7978 ssl->out_iv = ssl->out_hdr + 5;
7979 }
7980
7981 /* Adjust out_msg to make space for explicit IV, if used. */
7982 if( transform != NULL &&
7983 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
7984 {
7985 ssl->out_msg = ssl->out_iv + transform->ivlen - transform->fixed_ivlen;
7986 }
7987 else
7988 ssl->out_msg = ssl->out_iv;
7989}
7990
7991/* Once ssl->in_hdr as the address of the beginning of the
7992 * next incoming record is set, deduce the other pointers.
7993 *
7994 * Note: For TLS, we save the implicit record sequence number
7995 * (entering MAC computation) in the 8 bytes before ssl->in_hdr,
7996 * and the caller has to make sure there's space for this.
7997 */
7998
7999static void ssl_update_in_pointers( mbedtls_ssl_context *ssl,
8000 mbedtls_ssl_transform *transform )
8001{
8002#if defined(MBEDTLS_SSL_PROTO_DTLS)
8003 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8004 {
8005 ssl->in_ctr = ssl->in_hdr + 3;
8006 ssl->in_len = ssl->in_hdr + 11;
8007 ssl->in_iv = ssl->in_hdr + 13;
8008 }
8009 else
8010#endif
8011 {
8012 ssl->in_ctr = ssl->in_hdr - 8;
8013 ssl->in_len = ssl->in_hdr + 3;
8014 ssl->in_iv = ssl->in_hdr + 5;
8015 }
8016
8017 /* Offset in_msg from in_iv to allow space for explicit IV, if used. */
8018 if( transform != NULL &&
8019 ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
8020 {
8021 ssl->in_msg = ssl->in_iv + transform->ivlen - transform->fixed_ivlen;
8022 }
8023 else
8024 ssl->in_msg = ssl->in_iv;
8025}
8026
Paul Bakker5121ce52009-01-03 21:22:43 +00008027/*
8028 * Initialize an SSL context
8029 */
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02008030void mbedtls_ssl_init( mbedtls_ssl_context *ssl )
8031{
8032 memset( ssl, 0, sizeof( mbedtls_ssl_context ) );
8033}
8034
8035/*
8036 * Setup an SSL context
8037 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008038
8039static void ssl_reset_in_out_pointers( mbedtls_ssl_context *ssl )
8040{
8041 /* Set the incoming and outgoing record pointers. */
8042#if defined(MBEDTLS_SSL_PROTO_DTLS)
8043 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
8044 {
8045 ssl->out_hdr = ssl->out_buf;
8046 ssl->in_hdr = ssl->in_buf;
8047 }
8048 else
8049#endif /* MBEDTLS_SSL_PROTO_DTLS */
8050 {
8051 ssl->out_hdr = ssl->out_buf + 8;
8052 ssl->in_hdr = ssl->in_buf + 8;
8053 }
8054
8055 /* Derive other internal pointers. */
8056 ssl_update_out_pointers( ssl, NULL /* no transform enabled */ );
8057 ssl_update_in_pointers ( ssl, NULL /* no transform enabled */ );
8058}
8059
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008060int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard1897af92015-05-10 23:27:38 +02008061 const mbedtls_ssl_config *conf )
Paul Bakker5121ce52009-01-03 21:22:43 +00008062{
Paul Bakker48916f92012-09-16 19:57:18 +00008063 int ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00008064
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02008065 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00008066
8067 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01008068 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00008069 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02008070
8071 /* Set to NULL in case of an error condition */
8072 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02008073
Angus Grattond8213d02016-05-25 20:56:48 +10008074 ssl->in_buf = mbedtls_calloc( 1, MBEDTLS_SSL_IN_BUFFER_LEN );
8075 if( ssl->in_buf == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00008076 {
Angus Grattond8213d02016-05-25 20:56:48 +10008077 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_IN_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008078 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008079 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10008080 }
8081
8082 ssl->out_buf = mbedtls_calloc( 1, MBEDTLS_SSL_OUT_BUFFER_LEN );
8083 if( ssl->out_buf == NULL )
8084 {
8085 MBEDTLS_SSL_DEBUG_MSG( 1, ( "alloc(%d bytes) failed", MBEDTLS_SSL_OUT_BUFFER_LEN) );
k-stachowiak9f7798e2018-07-31 16:52:32 +02008086 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02008087 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008088 }
8089
Hanno Becker2a43f6f2018-08-10 11:12:52 +01008090 ssl_reset_in_out_pointers( ssl );
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02008091
Paul Bakker48916f92012-09-16 19:57:18 +00008092 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
k-stachowiaka47911c2018-07-04 17:41:58 +02008093 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00008094
8095 return( 0 );
k-stachowiaka47911c2018-07-04 17:41:58 +02008096
8097error:
8098 mbedtls_free( ssl->in_buf );
8099 mbedtls_free( ssl->out_buf );
8100
8101 ssl->conf = NULL;
8102
8103 ssl->in_buf = NULL;
8104 ssl->out_buf = NULL;
8105
8106 ssl->in_hdr = NULL;
8107 ssl->in_ctr = NULL;
8108 ssl->in_len = NULL;
8109 ssl->in_iv = NULL;
8110 ssl->in_msg = NULL;
8111
8112 ssl->out_hdr = NULL;
8113 ssl->out_ctr = NULL;
8114 ssl->out_len = NULL;
8115 ssl->out_iv = NULL;
8116 ssl->out_msg = NULL;
8117
k-stachowiak9f7798e2018-07-31 16:52:32 +02008118 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00008119}
8120
8121/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00008122 * Reset an initialized and used SSL context for re-use while retaining
8123 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008124 *
8125 * If partial is non-zero, keep data in the input buffer and client ID.
8126 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00008127 */
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008128static int ssl_session_reset_int( mbedtls_ssl_context *ssl, int partial )
Paul Bakker7eb013f2011-10-06 12:37:39 +00008129{
Paul Bakker48916f92012-09-16 19:57:18 +00008130 int ret;
8131
Hanno Becker7e772132018-08-10 12:38:21 +01008132#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || \
8133 !defined(MBEDTLS_SSL_SRV_C)
8134 ((void) partial);
8135#endif
8136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008137 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008138
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008139 /* Cancel any possibly running timer */
8140 ssl_set_timer( ssl, 0 );
8141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008142#if defined(MBEDTLS_SSL_RENEGOTIATION)
8143 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008144 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00008145
8146 ssl->verify_data_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008147 memset( ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
8148 memset( ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN );
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008149#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008150 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00008151
Paul Bakker7eb013f2011-10-06 12:37:39 +00008152 ssl->in_offt = NULL;
Hanno Beckerf29d4702018-08-10 11:31:15 +01008153 ssl_reset_in_out_pointers( ssl );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008154
8155 ssl->in_msgtype = 0;
8156 ssl->in_msglen = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008157#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008158 ssl->next_record_offset = 0;
Manuel Pégourié-Gonnard246c13a2014-09-24 13:56:09 +02008159 ssl->in_epoch = 0;
Manuel Pégourié-Gonnardb2f3be82014-07-10 17:54:52 +02008160#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008161#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnardb47368a2014-09-24 13:29:58 +02008162 ssl_dtls_replay_reset( ssl );
8163#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008164
8165 ssl->in_hslen = 0;
8166 ssl->nb_zero = 0;
Hanno Beckeraf0665d2017-05-24 09:16:26 +01008167
8168 ssl->keep_current_message = 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008169
8170 ssl->out_msgtype = 0;
8171 ssl->out_msglen = 0;
8172 ssl->out_left = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008173#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
8174 if( ssl->split_done != MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008175 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01008176#endif
Paul Bakker7eb013f2011-10-06 12:37:39 +00008177
Hanno Becker19859472018-08-06 09:40:20 +01008178 memset( ssl->cur_out_ctr, 0, sizeof( ssl->cur_out_ctr ) );
8179
Paul Bakker48916f92012-09-16 19:57:18 +00008180 ssl->transform_in = NULL;
8181 ssl->transform_out = NULL;
Paul Bakker7eb013f2011-10-06 12:37:39 +00008182
Hanno Becker78640902018-08-13 16:35:15 +01008183 ssl->session_in = NULL;
8184 ssl->session_out = NULL;
8185
Angus Grattond8213d02016-05-25 20:56:48 +10008186 memset( ssl->out_buf, 0, MBEDTLS_SSL_OUT_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008187
8188#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008189 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008190#endif /* MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE && MBEDTLS_SSL_SRV_C */
8191 {
8192 ssl->in_left = 0;
Angus Grattond8213d02016-05-25 20:56:48 +10008193 memset( ssl->in_buf, 0, MBEDTLS_SSL_IN_BUFFER_LEN );
Hanno Becker4ccbf062018-08-10 11:20:38 +01008194 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008196#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
8197 if( mbedtls_ssl_hw_record_reset != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +00008198 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008199 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_reset()" ) );
8200 if( ( ret = mbedtls_ssl_hw_record_reset( ssl ) ) != 0 )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008201 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008202 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_hw_record_reset", ret );
8203 return( MBEDTLS_ERR_SSL_HW_ACCEL_FAILED );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008204 }
Paul Bakker05ef8352012-05-08 09:17:57 +00008205 }
8206#endif
Paul Bakker2770fbd2012-07-03 13:30:23 +00008207
Paul Bakker48916f92012-09-16 19:57:18 +00008208 if( ssl->transform )
Paul Bakker2770fbd2012-07-03 13:30:23 +00008209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008210 mbedtls_ssl_transform_free( ssl->transform );
8211 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +00008212 ssl->transform = NULL;
Paul Bakker2770fbd2012-07-03 13:30:23 +00008213 }
Paul Bakker48916f92012-09-16 19:57:18 +00008214
Paul Bakkerc0463502013-02-14 11:19:38 +01008215 if( ssl->session )
8216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008217 mbedtls_ssl_session_free( ssl->session );
8218 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +01008219 ssl->session = NULL;
8220 }
8221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008222#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008223 ssl->alpn_chosen = NULL;
8224#endif
8225
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02008226#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker4ccbf062018-08-10 11:20:38 +01008227#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008228 if( partial == 0 )
Hanno Becker4ccbf062018-08-10 11:20:38 +01008229#endif
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008230 {
8231 mbedtls_free( ssl->cli_id );
8232 ssl->cli_id = NULL;
8233 ssl->cli_id_len = 0;
8234 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02008235#endif
8236
Paul Bakker48916f92012-09-16 19:57:18 +00008237 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
8238 return( ret );
Paul Bakker2770fbd2012-07-03 13:30:23 +00008239
8240 return( 0 );
Paul Bakker7eb013f2011-10-06 12:37:39 +00008241}
8242
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02008243/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02008244 * Reset an initialized and used SSL context for re-use while retaining
8245 * all application-set variables, function pointers and data.
8246 */
8247int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl )
8248{
8249 return( ssl_session_reset_int( ssl, 0 ) );
8250}
8251
8252/*
Paul Bakker5121ce52009-01-03 21:22:43 +00008253 * SSL set accessors
8254 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008255void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint )
Paul Bakker5121ce52009-01-03 21:22:43 +00008256{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008257 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00008258}
8259
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008260void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport )
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008261{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008262 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01008263}
8264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008265#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008266void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode )
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008267{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008268 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02008269}
8270#endif
8271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008272#if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008273void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit )
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008274{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008275 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02008276}
8277#endif
8278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008279#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01008280
Hanno Becker1841b0a2018-08-24 11:13:57 +01008281void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
8282 unsigned allow_packing )
Hanno Becker04da1892018-08-14 13:22:10 +01008283{
8284 ssl->disable_datagram_packing = !allow_packing;
8285}
8286
8287void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf,
8288 uint32_t min, uint32_t max )
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008289{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008290 conf->hs_timeout_min = min;
8291 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02008292}
8293#endif
8294
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008295void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode )
Paul Bakker5121ce52009-01-03 21:22:43 +00008296{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008297 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00008298}
8299
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008300#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008301void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02008302 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008303 void *p_vrfy )
8304{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008305 conf->f_vrfy = f_vrfy;
8306 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008307}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008308#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00008309
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008310void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
Paul Bakkera3d195c2011-11-27 21:07:34 +00008311 int (*f_rng)(void *, unsigned char *, size_t),
Paul Bakker5121ce52009-01-03 21:22:43 +00008312 void *p_rng )
8313{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01008314 conf->f_rng = f_rng;
8315 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00008316}
8317
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008318void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardfd474232015-06-23 16:34:24 +02008319 void (*f_dbg)(void *, int, const char *, int, const char *),
Paul Bakker5121ce52009-01-03 21:22:43 +00008320 void *p_dbg )
8321{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008322 conf->f_dbg = f_dbg;
8323 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00008324}
8325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008326void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008327 void *p_bio,
Simon Butchere846b512016-03-01 17:31:49 +00008328 mbedtls_ssl_send_t *f_send,
8329 mbedtls_ssl_recv_t *f_recv,
8330 mbedtls_ssl_recv_timeout_t *f_recv_timeout )
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008331{
8332 ssl->p_bio = p_bio;
8333 ssl->f_send = f_send;
8334 ssl->f_recv = f_recv;
8335 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008336}
8337
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02008338#if defined(MBEDTLS_SSL_PROTO_DTLS)
8339void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu )
8340{
8341 ssl->mtu = mtu;
8342}
8343#endif
8344
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008345void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout )
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01008346{
8347 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02008348}
8349
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008350void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
8351 void *p_timer,
Simon Butchere846b512016-03-01 17:31:49 +00008352 mbedtls_ssl_set_timer_t *f_set_timer,
8353 mbedtls_ssl_get_timer_t *f_get_timer )
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008354{
8355 ssl->p_timer = p_timer;
8356 ssl->f_set_timer = f_set_timer;
8357 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02008358
8359 /* Make sure we start with no timer running */
8360 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02008361}
8362
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008363#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008364void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008365 void *p_cache,
8366 int (*f_get_cache)(void *, mbedtls_ssl_session *),
8367 int (*f_set_cache)(void *, const mbedtls_ssl_session *) )
Paul Bakker5121ce52009-01-03 21:22:43 +00008368{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01008369 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008370 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008371 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00008372}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008373#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008375#if defined(MBEDTLS_SSL_CLI_C)
8376int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session )
Paul Bakker5121ce52009-01-03 21:22:43 +00008377{
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008378 int ret;
8379
8380 if( ssl == NULL ||
8381 session == NULL ||
8382 ssl->session_negotiate == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02008383 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008384 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008385 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008386 }
8387
Hanno Becker52055ae2019-02-06 14:30:46 +00008388 if( ( ret = mbedtls_ssl_session_copy( ssl->session_negotiate,
8389 session ) ) != 0 )
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008390 return( ret );
8391
Paul Bakker0a597072012-09-25 21:55:46 +00008392 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02008393
8394 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +00008395}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008396#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008397
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008398void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008399 const int *ciphersuites )
Paul Bakker5121ce52009-01-03 21:22:43 +00008400{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008401 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] = ciphersuites;
8402 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] = ciphersuites;
8403 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] = ciphersuites;
8404 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] = ciphersuites;
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008405}
8406
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008407void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02008408 const int *ciphersuites,
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008409 int major, int minor )
8410{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008411 if( major != MBEDTLS_SSL_MAJOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008412 return;
8413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008414 if( minor < MBEDTLS_SSL_MINOR_VERSION_0 || minor > MBEDTLS_SSL_MINOR_VERSION_3 )
Paul Bakker8f4ddae2013-04-15 15:09:54 +02008415 return;
8416
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008417 conf->ciphersuite_list[minor] = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00008418}
8419
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008420#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008421void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
Nicholas Wilson2088e2e2015-09-08 16:53:18 +01008422 const mbedtls_x509_crt_profile *profile )
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02008423{
8424 conf->cert_profile = profile;
8425}
8426
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008427/* Append a new keycert entry to a (possibly empty) list */
8428static int ssl_append_key_cert( mbedtls_ssl_key_cert **head,
8429 mbedtls_x509_crt *cert,
8430 mbedtls_pk_context *key )
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008431{
niisato8ee24222018-06-25 19:05:48 +09008432 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008433
niisato8ee24222018-06-25 19:05:48 +09008434 new_cert = mbedtls_calloc( 1, sizeof( mbedtls_ssl_key_cert ) );
8435 if( new_cert == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008436 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008437
niisato8ee24222018-06-25 19:05:48 +09008438 new_cert->cert = cert;
8439 new_cert->key = key;
8440 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008441
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008442 /* Update head is the list was null, else add to the end */
8443 if( *head == NULL )
Paul Bakker0333b972013-11-04 17:08:28 +01008444 {
niisato8ee24222018-06-25 19:05:48 +09008445 *head = new_cert;
Paul Bakker0333b972013-11-04 17:08:28 +01008446 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008447 else
8448 {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008449 mbedtls_ssl_key_cert *cur = *head;
8450 while( cur->next != NULL )
8451 cur = cur->next;
niisato8ee24222018-06-25 19:05:48 +09008452 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008453 }
8454
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008455 return( 0 );
8456}
8457
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008458int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02008459 mbedtls_x509_crt *own_cert,
8460 mbedtls_pk_context *pk_key )
8461{
Manuel Pégourié-Gonnard17a40cd2015-05-10 23:17:17 +02008462 return( ssl_append_key_cert( &conf->key_cert, own_cert, pk_key ) );
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02008463}
8464
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008465void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008466 mbedtls_x509_crt *ca_chain,
8467 mbedtls_x509_crl *ca_crl )
Paul Bakker5121ce52009-01-03 21:22:43 +00008468{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008469 conf->ca_chain = ca_chain;
8470 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00008471
8472#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8473 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8474 * cannot be used together. */
8475 conf->f_ca_cb = NULL;
8476 conf->p_ca_cb = NULL;
8477#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00008478}
Hanno Becker5adaad92019-03-27 16:54:37 +00008479
8480#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
8481void mbedtls_ssl_conf_ca_cb( mbedtls_ssl_config *conf,
Hanno Beckerafd0b0a2019-03-27 16:55:01 +00008482 mbedtls_x509_crt_ca_cb_t f_ca_cb,
Hanno Becker5adaad92019-03-27 16:54:37 +00008483 void *p_ca_cb )
8484{
8485 conf->f_ca_cb = f_ca_cb;
8486 conf->p_ca_cb = p_ca_cb;
8487
8488 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
8489 * cannot be used together. */
8490 conf->ca_chain = NULL;
8491 conf->ca_crl = NULL;
8492}
8493#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008494#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00008495
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008496#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
8497int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
8498 mbedtls_x509_crt *own_cert,
8499 mbedtls_pk_context *pk_key )
8500{
8501 return( ssl_append_key_cert( &ssl->handshake->sni_key_cert,
8502 own_cert, pk_key ) );
8503}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02008504
8505void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
8506 mbedtls_x509_crt *ca_chain,
8507 mbedtls_x509_crl *ca_crl )
8508{
8509 ssl->handshake->sni_ca_chain = ca_chain;
8510 ssl->handshake->sni_ca_crl = ca_crl;
8511}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02008512
8513void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
8514 int authmode )
8515{
8516 ssl->handshake->sni_authmode = authmode;
8517}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02008518#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
8519
Hanno Becker8927c832019-04-03 12:52:50 +01008520#if defined(MBEDTLS_X509_CRT_PARSE_C)
8521void mbedtls_ssl_set_verify( mbedtls_ssl_context *ssl,
8522 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
8523 void *p_vrfy )
8524{
8525 ssl->f_vrfy = f_vrfy;
8526 ssl->p_vrfy = p_vrfy;
8527}
8528#endif
8529
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008530#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008531/*
8532 * Set EC J-PAKE password for current handshake
8533 */
8534int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
8535 const unsigned char *pw,
8536 size_t pw_len )
8537{
8538 mbedtls_ecjpake_role role;
8539
Janos Follath8eb64132016-06-03 15:40:57 +01008540 if( ssl->handshake == NULL || ssl->conf == NULL )
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008541 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8542
8543 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
8544 role = MBEDTLS_ECJPAKE_SERVER;
8545 else
8546 role = MBEDTLS_ECJPAKE_CLIENT;
8547
8548 return( mbedtls_ecjpake_setup( &ssl->handshake->ecjpake_ctx,
8549 role,
8550 MBEDTLS_MD_SHA256,
8551 MBEDTLS_ECP_DP_SECP256R1,
8552 pw, pw_len ) );
8553}
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02008554#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02008555
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008556#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008557
8558static void ssl_conf_remove_psk( mbedtls_ssl_config *conf )
8559{
8560 /* Remove reference to existing PSK, if any. */
8561#if defined(MBEDTLS_USE_PSA_CRYPTO)
8562 if( conf->psk_opaque != 0 )
8563 {
8564 /* The maintenance of the PSK key slot is the
8565 * user's responsibility. */
8566 conf->psk_opaque = 0;
8567 }
Hanno Beckera63ac3f2018-11-05 12:47:16 +00008568 /* This and the following branch should never
8569 * be taken simultaenously as we maintain the
8570 * invariant that raw and opaque PSKs are never
8571 * configured simultaneously. As a safeguard,
8572 * though, `else` is omitted here. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008573#endif /* MBEDTLS_USE_PSA_CRYPTO */
8574 if( conf->psk != NULL )
8575 {
8576 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
8577
8578 mbedtls_free( conf->psk );
8579 conf->psk = NULL;
8580 conf->psk_len = 0;
8581 }
8582
8583 /* Remove reference to PSK identity, if any. */
8584 if( conf->psk_identity != NULL )
8585 {
8586 mbedtls_free( conf->psk_identity );
8587 conf->psk_identity = NULL;
8588 conf->psk_identity_len = 0;
8589 }
8590}
8591
Hanno Becker7390c712018-11-15 13:33:04 +00008592/* This function assumes that PSK identity in the SSL config is unset.
8593 * It checks that the provided identity is well-formed and attempts
8594 * to make a copy of it in the SSL config.
8595 * On failure, the PSK identity in the config remains unset. */
8596static int ssl_conf_set_psk_identity( mbedtls_ssl_config *conf,
8597 unsigned char const *psk_identity,
8598 size_t psk_identity_len )
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008599{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008600 /* Identity len will be encoded on two bytes */
Hanno Becker7390c712018-11-15 13:33:04 +00008601 if( psk_identity == NULL ||
8602 ( psk_identity_len >> 16 ) != 0 ||
Angus Grattond8213d02016-05-25 20:56:48 +10008603 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN )
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02008604 {
8605 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8606 }
8607
Hanno Becker7390c712018-11-15 13:33:04 +00008608 conf->psk_identity = mbedtls_calloc( 1, psk_identity_len );
8609 if( conf->psk_identity == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008610 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker6db455e2013-09-18 17:29:31 +02008611
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008612 conf->psk_identity_len = psk_identity_len;
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01008613 memcpy( conf->psk_identity, psk_identity, conf->psk_identity_len );
Paul Bakker5ad403f2013-09-18 21:21:30 +02008614
8615 return( 0 );
Paul Bakker6db455e2013-09-18 17:29:31 +02008616}
8617
Hanno Becker7390c712018-11-15 13:33:04 +00008618int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
8619 const unsigned char *psk, size_t psk_len,
8620 const unsigned char *psk_identity, size_t psk_identity_len )
8621{
8622 int ret;
8623 /* Remove opaque/raw PSK + PSK Identity */
8624 ssl_conf_remove_psk( conf );
8625
8626 /* Check and set raw PSK */
8627 if( psk == NULL || psk_len > MBEDTLS_PSK_MAX_LEN )
8628 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8629 if( ( conf->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
8630 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
8631 conf->psk_len = psk_len;
8632 memcpy( conf->psk, psk, conf->psk_len );
8633
8634 /* Check and set PSK Identity */
8635 ret = ssl_conf_set_psk_identity( conf, psk_identity, psk_identity_len );
8636 if( ret != 0 )
8637 ssl_conf_remove_psk( conf );
8638
8639 return( ret );
8640}
8641
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008642static void ssl_remove_psk( mbedtls_ssl_context *ssl )
8643{
8644#if defined(MBEDTLS_USE_PSA_CRYPTO)
8645 if( ssl->handshake->psk_opaque != 0 )
8646 {
8647 ssl->handshake->psk_opaque = 0;
8648 }
8649 else
8650#endif /* MBEDTLS_USE_PSA_CRYPTO */
8651 if( ssl->handshake->psk != NULL )
8652 {
8653 mbedtls_platform_zeroize( ssl->handshake->psk,
8654 ssl->handshake->psk_len );
8655 mbedtls_free( ssl->handshake->psk );
8656 ssl->handshake->psk_len = 0;
8657 }
8658}
8659
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008660int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
8661 const unsigned char *psk, size_t psk_len )
8662{
8663 if( psk == NULL || ssl->handshake == NULL )
8664 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8665
8666 if( psk_len > MBEDTLS_PSK_MAX_LEN )
8667 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8668
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008669 ssl_remove_psk( ssl );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008670
Manuel Pégourié-Gonnard7551cb92015-05-26 16:04:06 +02008671 if( ( ssl->handshake->psk = mbedtls_calloc( 1, psk_len ) ) == NULL )
Manuel Pégourié-Gonnard6a8ca332015-05-28 09:33:39 +02008672 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01008673
8674 ssl->handshake->psk_len = psk_len;
8675 memcpy( ssl->handshake->psk, psk, ssl->handshake->psk_len );
8676
8677 return( 0 );
8678}
8679
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008680#if defined(MBEDTLS_USE_PSA_CRYPTO)
8681int mbedtls_ssl_conf_psk_opaque( mbedtls_ssl_config *conf,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008682 psa_key_handle_t psk_slot,
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008683 const unsigned char *psk_identity,
8684 size_t psk_identity_len )
8685{
Hanno Becker7390c712018-11-15 13:33:04 +00008686 int ret;
8687 /* Clear opaque/raw PSK + PSK Identity, if present. */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008688 ssl_conf_remove_psk( conf );
8689
Hanno Becker7390c712018-11-15 13:33:04 +00008690 /* Check and set opaque PSK */
8691 if( psk_slot == 0 )
8692 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008693 conf->psk_opaque = psk_slot;
Hanno Becker7390c712018-11-15 13:33:04 +00008694
8695 /* Check and set PSK Identity */
8696 ret = ssl_conf_set_psk_identity( conf, psk_identity,
8697 psk_identity_len );
8698 if( ret != 0 )
8699 ssl_conf_remove_psk( conf );
8700
8701 return( ret );
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008702}
8703
8704int mbedtls_ssl_set_hs_psk_opaque( mbedtls_ssl_context *ssl,
Andrzej Kurek2349c4d2019-01-08 09:36:01 -05008705 psa_key_handle_t psk_slot )
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01008706{
8707 if( psk_slot == 0 || ssl->handshake == NULL )
8708 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8709
8710 ssl_remove_psk( ssl );
8711 ssl->handshake->psk_opaque = psk_slot;
8712 return( 0 );
8713}
8714#endif /* MBEDTLS_USE_PSA_CRYPTO */
8715
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008716void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008717 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
Paul Bakker6db455e2013-09-18 17:29:31 +02008718 size_t),
8719 void *p_psk )
8720{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008721 conf->f_psk = f_psk;
8722 conf->p_psk = p_psk;
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02008723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008724#endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
Paul Bakker43b7e352011-01-18 15:27:19 +00008725
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008726#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Hanno Becker470a8c42017-10-04 15:28:46 +01008727
8728#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008729int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf, const char *dhm_P, const char *dhm_G )
Paul Bakker5121ce52009-01-03 21:22:43 +00008730{
8731 int ret;
8732
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008733 if( ( ret = mbedtls_mpi_read_string( &conf->dhm_P, 16, dhm_P ) ) != 0 ||
8734 ( ret = mbedtls_mpi_read_string( &conf->dhm_G, 16, dhm_G ) ) != 0 )
8735 {
8736 mbedtls_mpi_free( &conf->dhm_P );
8737 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker5121ce52009-01-03 21:22:43 +00008738 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008739 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008740
8741 return( 0 );
8742}
Hanno Becker470a8c42017-10-04 15:28:46 +01008743#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker5121ce52009-01-03 21:22:43 +00008744
Hanno Beckera90658f2017-10-04 15:29:08 +01008745int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
8746 const unsigned char *dhm_P, size_t P_len,
8747 const unsigned char *dhm_G, size_t G_len )
8748{
8749 int ret;
8750
8751 if( ( ret = mbedtls_mpi_read_binary( &conf->dhm_P, dhm_P, P_len ) ) != 0 ||
8752 ( ret = mbedtls_mpi_read_binary( &conf->dhm_G, dhm_G, G_len ) ) != 0 )
8753 {
8754 mbedtls_mpi_free( &conf->dhm_P );
8755 mbedtls_mpi_free( &conf->dhm_G );
8756 return( ret );
8757 }
8758
8759 return( 0 );
8760}
Paul Bakker5121ce52009-01-03 21:22:43 +00008761
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008762int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx )
Paul Bakker1b57b062011-01-06 15:48:19 +00008763{
8764 int ret;
8765
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008766 if( ( ret = mbedtls_mpi_copy( &conf->dhm_P, &dhm_ctx->P ) ) != 0 ||
8767 ( ret = mbedtls_mpi_copy( &conf->dhm_G, &dhm_ctx->G ) ) != 0 )
8768 {
8769 mbedtls_mpi_free( &conf->dhm_P );
8770 mbedtls_mpi_free( &conf->dhm_G );
Paul Bakker1b57b062011-01-06 15:48:19 +00008771 return( ret );
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01008772 }
Paul Bakker1b57b062011-01-06 15:48:19 +00008773
8774 return( 0 );
8775}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02008776#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00008777
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02008778#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
8779/*
8780 * Set the minimum length for Diffie-Hellman parameters
8781 */
8782void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
8783 unsigned int bitlen )
8784{
8785 conf->dhm_min_bitlen = bitlen;
8786}
8787#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
8788
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +02008789#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008790/*
8791 * Set allowed/preferred hashes for handshake signatures
8792 */
8793void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
8794 const int *hashes )
8795{
8796 conf->sig_hashes = hashes;
8797}
Hanno Becker947194e2017-04-07 13:25:49 +01008798#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02008799
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02008800#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008801/*
8802 * Set the allowed elliptic curves
8803 */
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008804void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008805 const mbedtls_ecp_group_id *curve_list )
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008806{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008807 conf->curve_list = curve_list;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008808}
Hanno Becker947194e2017-04-07 13:25:49 +01008809#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01008810
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008811#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008812int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname )
Paul Bakker5121ce52009-01-03 21:22:43 +00008813{
Hanno Becker947194e2017-04-07 13:25:49 +01008814 /* Initialize to suppress unnecessary compiler warning */
8815 size_t hostname_len = 0;
8816
8817 /* Check if new hostname is valid before
8818 * making any change to current one */
Hanno Becker947194e2017-04-07 13:25:49 +01008819 if( hostname != NULL )
8820 {
8821 hostname_len = strlen( hostname );
8822
8823 if( hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN )
8824 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
8825 }
8826
8827 /* Now it's clear that we will overwrite the old hostname,
8828 * so we can free it safely */
8829
8830 if( ssl->hostname != NULL )
8831 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -05008832 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Hanno Becker947194e2017-04-07 13:25:49 +01008833 mbedtls_free( ssl->hostname );
8834 }
8835
8836 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01008837
Paul Bakker5121ce52009-01-03 21:22:43 +00008838 if( hostname == NULL )
Hanno Becker947194e2017-04-07 13:25:49 +01008839 {
8840 ssl->hostname = NULL;
8841 }
8842 else
8843 {
8844 ssl->hostname = mbedtls_calloc( 1, hostname_len + 1 );
Hanno Becker947194e2017-04-07 13:25:49 +01008845 if( ssl->hostname == NULL )
8846 return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008847
Hanno Becker947194e2017-04-07 13:25:49 +01008848 memcpy( ssl->hostname, hostname, hostname_len );
Paul Bakker75c1a6f2013-08-19 14:25:29 +02008849
Hanno Becker947194e2017-04-07 13:25:49 +01008850 ssl->hostname[hostname_len] = '\0';
8851 }
Paul Bakker5121ce52009-01-03 21:22:43 +00008852
8853 return( 0 );
8854}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01008855#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00008856
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01008857#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008858void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008859 int (*f_sni)(void *, mbedtls_ssl_context *,
Paul Bakker5701cdc2012-09-27 21:49:42 +00008860 const unsigned char *, size_t),
8861 void *p_sni )
8862{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008863 conf->f_sni = f_sni;
8864 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00008865}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008866#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00008867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008868#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008869int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008870{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008871 size_t cur_len, tot_len;
8872 const char **p;
8873
8874 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08008875 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
8876 * MUST NOT be truncated."
8877 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008878 */
8879 tot_len = 0;
8880 for( p = protos; *p != NULL; p++ )
8881 {
8882 cur_len = strlen( *p );
8883 tot_len += cur_len;
8884
8885 if( cur_len == 0 || cur_len > 255 || tot_len > 65535 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008886 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008887 }
8888
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008889 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02008890
8891 return( 0 );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008892}
8893
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008894const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008895{
Paul Bakkerd8bb8262014-06-17 14:06:49 +02008896 return( ssl->alpn_chosen );
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008897}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008898#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02008899
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008900void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker490ecc82011-10-06 13:04:09 +00008901{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008902 conf->max_major_ver = major;
8903 conf->max_minor_ver = minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00008904}
8905
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008906void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor )
Paul Bakker1d29fb52012-09-28 13:28:45 +00008907{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008908 conf->min_major_ver = major;
8909 conf->min_minor_ver = minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00008910}
8911
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008912#if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008913void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback )
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008914{
Manuel Pégourié-Gonnard684b0592015-05-06 09:27:31 +01008915 conf->fallback = fallback;
Manuel Pégourié-Gonnard1cbd39d2014-10-20 13:34:59 +02008916}
8917#endif
8918
Janos Follath088ce432017-04-10 12:42:31 +01008919#if defined(MBEDTLS_SSL_SRV_C)
8920void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
8921 char cert_req_ca_list )
8922{
8923 conf->cert_req_ca_list = cert_req_ca_list;
8924}
8925#endif
8926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008927#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008928void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm )
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008929{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008930 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01008931}
8932#endif
8933
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008934#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008935void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems )
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008936{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008937 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02008938}
8939#endif
8940
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008941#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008942void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 )
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008943{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008944 conf->arc4_disabled = arc4;
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008945}
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +02008946#endif
Manuel Pégourié-Gonnardbd47a582015-01-12 13:43:29 +01008947
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008948#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008949int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008950{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008951 if( mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
Angus Grattond8213d02016-05-25 20:56:48 +10008952 ssl_mfl_code_to_length( mfl_code ) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN )
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008953 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008954 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008955 }
8956
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01008957 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008958
8959 return( 0 );
8960}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008961#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02008962
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008963#if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
Manuel Pégourié-Gonnard01e5e8c2015-05-11 10:11:56 +02008964void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate )
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008966 conf->trunc_hmac = truncate;
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008967}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008968#endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
Manuel Pégourié-Gonnarde980a992013-07-19 11:08:52 +02008969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008970#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008971void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split )
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008972{
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01008973 conf->cbc_record_splitting = split;
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01008974}
8975#endif
8976
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008977void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy )
Paul Bakker48916f92012-09-16 19:57:18 +00008978{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008979 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00008980}
8981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008982#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008983void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation )
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008984{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008985 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01008986}
8987
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008988void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008989{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008990 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02008991}
8992
Manuel Pégourié-Gonnard6729e792015-05-11 09:50:24 +02008993void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008994 const unsigned char period[8] )
8995{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02008996 memcpy( conf->renego_period, period, 8 );
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01008997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008998#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00008999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009000#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009001#if defined(MBEDTLS_SSL_CLI_C)
9002void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets )
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009003{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01009004 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009005}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009006#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02009007
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009008#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009009void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
9010 mbedtls_ssl_ticket_write_t *f_ticket_write,
9011 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
9012 void *p_ticket )
Paul Bakker606b4ba2013-08-14 16:52:14 +02009013{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02009014 conf->f_ticket_write = f_ticket_write;
9015 conf->f_ticket_parse = f_ticket_parse;
9016 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02009017}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02009018#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009019#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02009020
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009021#if defined(MBEDTLS_SSL_EXPORT_KEYS)
9022void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
9023 mbedtls_ssl_export_keys_t *f_export_keys,
9024 void *p_export_keys )
9025{
9026 conf->f_export_keys = f_export_keys;
9027 conf->p_export_keys = p_export_keys;
9028}
Ron Eldorf5cc10d2019-05-07 18:33:40 +03009029
9030void mbedtls_ssl_conf_export_keys_ext_cb( mbedtls_ssl_config *conf,
9031 mbedtls_ssl_export_keys_ext_t *f_export_keys_ext,
9032 void *p_export_keys )
9033{
9034 conf->f_export_keys_ext = f_export_keys_ext;
9035 conf->p_export_keys = p_export_keys;
9036}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01009037#endif
9038
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009039#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009040void mbedtls_ssl_conf_async_private_cb(
9041 mbedtls_ssl_config *conf,
9042 mbedtls_ssl_async_sign_t *f_async_sign,
9043 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
9044 mbedtls_ssl_async_resume_t *f_async_resume,
9045 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009046 void *async_config_data )
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009047{
9048 conf->f_async_sign_start = f_async_sign;
9049 conf->f_async_decrypt_start = f_async_decrypt;
9050 conf->f_async_resume = f_async_resume;
9051 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009052 conf->p_async_config_data = async_config_data;
9053}
9054
Gilles Peskine8f97af72018-04-26 11:46:10 +02009055void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf )
9056{
9057 return( conf->p_async_config_data );
9058}
9059
Gilles Peskine1febfef2018-04-30 11:54:39 +02009060void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl )
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009061{
9062 if( ssl->handshake == NULL )
9063 return( NULL );
9064 else
9065 return( ssl->handshake->user_async_ctx );
9066}
9067
Gilles Peskine1febfef2018-04-30 11:54:39 +02009068void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02009069 void *ctx )
9070{
9071 if( ssl->handshake != NULL )
9072 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009073}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02009074#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01009075
Paul Bakker5121ce52009-01-03 21:22:43 +00009076/*
9077 * SSL get accessors
9078 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009079size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009080{
9081 return( ssl->in_offt == NULL ? 0 : ssl->in_msglen );
9082}
9083
Hanno Becker8b170a02017-10-10 11:51:19 +01009084int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl )
9085{
9086 /*
9087 * Case A: We're currently holding back
9088 * a message for further processing.
9089 */
9090
9091 if( ssl->keep_current_message == 1 )
9092 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009093 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: record held back for processing" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009094 return( 1 );
9095 }
9096
9097 /*
9098 * Case B: Further records are pending in the current datagram.
9099 */
9100
9101#if defined(MBEDTLS_SSL_PROTO_DTLS)
9102 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9103 ssl->in_left > ssl->next_record_offset )
9104 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009105 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more records within current datagram" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009106 return( 1 );
9107 }
9108#endif /* MBEDTLS_SSL_PROTO_DTLS */
9109
9110 /*
9111 * Case C: A handshake message is being processed.
9112 */
9113
Hanno Becker8b170a02017-10-10 11:51:19 +01009114 if( ssl->in_hslen > 0 && ssl->in_hslen < ssl->in_msglen )
9115 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009116 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: more handshake messages within current record" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009117 return( 1 );
9118 }
9119
9120 /*
9121 * Case D: An application data message is being processed
9122 */
9123 if( ssl->in_offt != NULL )
9124 {
Hanno Beckera6fb0892017-10-23 13:17:48 +01009125 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: application data record is being processed" ) );
Hanno Becker8b170a02017-10-10 11:51:19 +01009126 return( 1 );
9127 }
9128
9129 /*
9130 * In all other cases, the rest of the message can be dropped.
Hanno Beckerc573ac32018-08-28 17:15:25 +01009131 * As in ssl_get_next_record, this needs to be adapted if
Hanno Becker8b170a02017-10-10 11:51:19 +01009132 * we implement support for multiple alerts in single records.
9133 */
9134
9135 MBEDTLS_SSL_DEBUG_MSG( 3, ( "ssl_check_pending: nothing pending" ) );
9136 return( 0 );
9137}
9138
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +02009139uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009140{
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00009141 if( ssl->session != NULL )
9142 return( ssl->session->verify_result );
9143
9144 if( ssl->session_negotiate != NULL )
9145 return( ssl->session_negotiate->verify_result );
9146
Manuel Pégourié-Gonnard6ab9b002015-05-14 11:25:04 +02009147 return( 0xFFFFFFFF );
Paul Bakker5121ce52009-01-03 21:22:43 +00009148}
9149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009150const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl )
Paul Bakker72f62662011-01-16 21:27:44 +00009151{
Paul Bakker926c8e42013-03-06 10:23:34 +01009152 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009153 return( NULL );
Paul Bakker926c8e42013-03-06 10:23:34 +01009154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009155 return mbedtls_ssl_get_ciphersuite_name( ssl->session->ciphersuite );
Paul Bakker72f62662011-01-16 21:27:44 +00009156}
9157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009158const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl )
Paul Bakker43ca69c2011-01-15 17:35:19 +00009159{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009160#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009161 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009162 {
9163 switch( ssl->minor_ver )
9164 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009165 case MBEDTLS_SSL_MINOR_VERSION_2:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009166 return( "DTLSv1.0" );
9167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009168 case MBEDTLS_SSL_MINOR_VERSION_3:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009169 return( "DTLSv1.2" );
9170
9171 default:
9172 return( "unknown (DTLS)" );
9173 }
9174 }
9175#endif
9176
Paul Bakker43ca69c2011-01-15 17:35:19 +00009177 switch( ssl->minor_ver )
9178 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009179 case MBEDTLS_SSL_MINOR_VERSION_0:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009180 return( "SSLv3.0" );
9181
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009182 case MBEDTLS_SSL_MINOR_VERSION_1:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009183 return( "TLSv1.0" );
9184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009185 case MBEDTLS_SSL_MINOR_VERSION_2:
Paul Bakker43ca69c2011-01-15 17:35:19 +00009186 return( "TLSv1.1" );
9187
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009188 case MBEDTLS_SSL_MINOR_VERSION_3:
Paul Bakker1ef83d62012-04-11 12:09:53 +00009189 return( "TLSv1.2" );
9190
Paul Bakker43ca69c2011-01-15 17:35:19 +00009191 default:
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01009192 return( "unknown" );
Paul Bakker43ca69c2011-01-15 17:35:19 +00009193 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00009194}
9195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009196int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009197{
Hanno Becker3136ede2018-08-17 15:28:19 +01009198 size_t transform_expansion = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009199 const mbedtls_ssl_transform *transform = ssl->transform_out;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009200 unsigned block_size;
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009201
Hanno Becker78640902018-08-13 16:35:15 +01009202 if( transform == NULL )
9203 return( (int) mbedtls_ssl_hdr_len( ssl ) );
9204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009205#if defined(MBEDTLS_ZLIB_SUPPORT)
9206 if( ssl->session_out->compression != MBEDTLS_SSL_COMPRESS_NULL )
9207 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009208#endif
9209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009210 switch( mbedtls_cipher_get_cipher_mode( &transform->cipher_ctx_enc ) )
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009211 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009212 case MBEDTLS_MODE_GCM:
9213 case MBEDTLS_MODE_CCM:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009214 case MBEDTLS_MODE_CHACHAPOLY:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009215 case MBEDTLS_MODE_STREAM:
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009216 transform_expansion = transform->minlen;
9217 break;
9218
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009219 case MBEDTLS_MODE_CBC:
Hanno Becker5b559ac2018-08-03 09:40:07 +01009220
9221 block_size = mbedtls_cipher_get_block_size(
9222 &transform->cipher_ctx_enc );
9223
Hanno Becker3136ede2018-08-17 15:28:19 +01009224 /* Expansion due to the addition of the MAC. */
9225 transform_expansion += transform->maclen;
9226
9227 /* Expansion due to the addition of CBC padding;
9228 * Theoretically up to 256 bytes, but we never use
9229 * more than the block size of the underlying cipher. */
9230 transform_expansion += block_size;
9231
9232 /* For TLS 1.1 or higher, an explicit IV is added
9233 * after the record header. */
Hanno Becker5b559ac2018-08-03 09:40:07 +01009234#if defined(MBEDTLS_SSL_PROTO_TLS1_1) || defined(MBEDTLS_SSL_PROTO_TLS1_2)
9235 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_2 )
Hanno Becker3136ede2018-08-17 15:28:19 +01009236 transform_expansion += block_size;
Hanno Becker5b559ac2018-08-03 09:40:07 +01009237#endif /* MBEDTLS_SSL_PROTO_TLS1_1 || MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker3136ede2018-08-17 15:28:19 +01009238
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009239 break;
9240
9241 default:
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009242 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009243 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009244 }
9245
Manuel Pégourié-Gonnard9de64f52015-07-01 15:51:43 +02009246 return( (int)( mbedtls_ssl_hdr_len( ssl ) + transform_expansion ) );
Manuel Pégourié-Gonnard9b35f182014-10-14 17:47:31 +02009247}
9248
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009249#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9250size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl )
9251{
9252 size_t max_len;
9253
9254 /*
9255 * Assume mfl_code is correct since it was checked when set
9256 */
Angus Grattond8213d02016-05-25 20:56:48 +10009257 max_len = ssl_mfl_code_to_length( ssl->conf->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009258
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009259 /* Check if a smaller max length was negotiated */
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009260 if( ssl->session_out != NULL &&
Angus Grattond8213d02016-05-25 20:56:48 +10009261 ssl_mfl_code_to_length( ssl->session_out->mfl_code ) < max_len )
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009262 {
Angus Grattond8213d02016-05-25 20:56:48 +10009263 max_len = ssl_mfl_code_to_length( ssl->session_out->mfl_code );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009264 }
9265
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02009266 /* During a handshake, use the value being negotiated */
9267 if( ssl->session_negotiate != NULL &&
9268 ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code ) < max_len )
9269 {
9270 max_len = ssl_mfl_code_to_length( ssl->session_negotiate->mfl_code );
9271 }
9272
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009273 return( max_len );
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02009274}
9275#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
9276
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009277#if defined(MBEDTLS_SSL_PROTO_DTLS)
9278static size_t ssl_get_current_mtu( const mbedtls_ssl_context *ssl )
9279{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04009280 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
9281 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
9282 ( ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
9283 ssl->state == MBEDTLS_SSL_SERVER_HELLO ) )
9284 return ( 0 );
9285
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009286 if( ssl->handshake == NULL || ssl->handshake->mtu == 0 )
9287 return( ssl->mtu );
9288
9289 if( ssl->mtu == 0 )
9290 return( ssl->handshake->mtu );
9291
9292 return( ssl->mtu < ssl->handshake->mtu ?
9293 ssl->mtu : ssl->handshake->mtu );
9294}
9295#endif /* MBEDTLS_SSL_PROTO_DTLS */
9296
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009297int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl )
9298{
9299 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
9300
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02009301#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9302 !defined(MBEDTLS_SSL_PROTO_DTLS)
9303 (void) ssl;
9304#endif
9305
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009306#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9307 const size_t mfl = mbedtls_ssl_get_max_frag_len( ssl );
9308
9309 if( max_len > mfl )
9310 max_len = mfl;
9311#endif
9312
9313#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009314 if( ssl_get_current_mtu( ssl ) != 0 )
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009315 {
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009316 const size_t mtu = ssl_get_current_mtu( ssl );
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009317 const int ret = mbedtls_ssl_get_record_expansion( ssl );
9318 const size_t overhead = (size_t) ret;
9319
9320 if( ret < 0 )
9321 return( ret );
9322
9323 if( mtu <= overhead )
9324 {
9325 MBEDTLS_SSL_DEBUG_MSG( 1, ( "MTU too low for record expansion" ) );
9326 return( MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE );
9327 }
9328
9329 if( max_len > mtu - overhead )
9330 max_len = mtu - overhead;
9331 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02009332#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009333
Hanno Becker0defedb2018-08-10 12:35:02 +01009334#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
9335 !defined(MBEDTLS_SSL_PROTO_DTLS)
9336 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009337#endif
9338
9339 return( (int) max_len );
9340}
9341
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009342#if defined(MBEDTLS_X509_CRT_PARSE_C)
9343const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl )
Paul Bakkerb0550d92012-10-30 07:51:03 +00009344{
9345 if( ssl == NULL || ssl->session == NULL )
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009346 return( NULL );
Paul Bakkerb0550d92012-10-30 07:51:03 +00009347
Hanno Beckere6824572019-02-07 13:18:46 +00009348#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Paul Bakkerd8bb8262014-06-17 14:06:49 +02009349 return( ssl->session->peer_cert );
Hanno Beckere6824572019-02-07 13:18:46 +00009350#else
9351 return( NULL );
9352#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009353}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009354#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00009355
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009356#if defined(MBEDTLS_SSL_CLI_C)
Hanno Beckerf852b1c2019-02-05 11:42:30 +00009357int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl,
9358 mbedtls_ssl_session *dst )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009359{
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009360 if( ssl == NULL ||
9361 dst == NULL ||
9362 ssl->session == NULL ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009363 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009364 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009365 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009366 }
9367
Hanno Becker52055ae2019-02-06 14:30:46 +00009368 return( mbedtls_ssl_session_copy( dst, ssl->session ) );
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009369}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009370#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02009371
Paul Bakker5121ce52009-01-03 21:22:43 +00009372/*
Paul Bakker1961b702013-01-25 14:49:24 +01009373 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00009374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009375int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +00009376{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009377 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Paul Bakker5121ce52009-01-03 21:22:43 +00009378
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009379 if( ssl == NULL || ssl->conf == NULL )
9380 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9381
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009382#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009383 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009384 ret = mbedtls_ssl_handshake_client_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009385#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009386#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009387 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009388 ret = mbedtls_ssl_handshake_server_step( ssl );
Paul Bakker5121ce52009-01-03 21:22:43 +00009389#endif
9390
Paul Bakker1961b702013-01-25 14:49:24 +01009391 return( ret );
9392}
9393
9394/*
9395 * Perform the SSL handshake
9396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009397int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl )
Paul Bakker1961b702013-01-25 14:49:24 +01009398{
9399 int ret = 0;
9400
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009401 if( ssl == NULL || ssl->conf == NULL )
9402 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9403
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009404 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> handshake" ) );
Paul Bakker1961b702013-01-25 14:49:24 +01009405
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009406 while( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker1961b702013-01-25 14:49:24 +01009407 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009408 ret = mbedtls_ssl_handshake_step( ssl );
Paul Bakker1961b702013-01-25 14:49:24 +01009409
9410 if( ret != 0 )
9411 break;
9412 }
9413
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009414 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= handshake" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009415
9416 return( ret );
9417}
9418
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009419#if defined(MBEDTLS_SSL_RENEGOTIATION)
9420#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00009421/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009422 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00009423 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009424static int ssl_write_hello_request( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009425{
9426 int ret;
9427
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009428 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009429
9430 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009431 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
9432 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009433
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009434 if( ( ret = mbedtls_ssl_write_handshake_msg( ssl ) ) != 0 )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009435 {
Manuel Pégourié-Gonnard31c15862017-09-13 09:38:11 +02009436 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_handshake_msg", ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009437 return( ret );
9438 }
9439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009440 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write hello request" ) );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009441
9442 return( 0 );
9443}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009444#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009445
9446/*
9447 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009448 * - any side: calling mbedtls_ssl_renegotiate(),
9449 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
9450 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02009451 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009452 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009453 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009454 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009455static int ssl_start_renegotiation( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +00009456{
9457 int ret;
9458
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009459 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009460
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009461 if( ( ret = ssl_handshake_init( ssl ) ) != 0 )
9462 return( ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009463
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009464 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
9465 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009466#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009467 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009468 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009469 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009470 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02009471 ssl->handshake->out_msg_seq = 1;
9472 else
9473 ssl->handshake->in_msg_seq = 1;
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02009474 }
9475#endif
9476
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009477 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
9478 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00009479
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009480 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009481 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker48916f92012-09-16 19:57:18 +00009483 return( ret );
9484 }
9485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009486 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= renegotiate" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009487
9488 return( 0 );
9489}
9490
9491/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009492 * Renegotiate current connection on client,
9493 * or request renegotiation on server
9494 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009495int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009496{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009497 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009498
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009499 if( ssl == NULL || ssl->conf == NULL )
9500 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9501
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009502#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009503 /* On server, just send the request */
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009504 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009505 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009506 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9507 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009508
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009509 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009510
9511 /* Did we already try/start sending HelloRequest? */
9512 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009513 return( mbedtls_ssl_flush_output( ssl ) );
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02009514
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009515 return( ssl_write_hello_request( ssl ) );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009516 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009518
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009519#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009520 /*
9521 * On client, either start the renegotiation process or,
9522 * if already in progress, continue the handshake
9523 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009524 if( ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009525 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009526 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
9527 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009528
9529 if( ( ret = ssl_start_renegotiation( ssl ) ) != 0 )
9530 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009531 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009532 return( ret );
9533 }
9534 }
9535 else
9536 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009537 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009538 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009539 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009540 return( ret );
9541 }
9542 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009543#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01009544
Paul Bakker37ce0ff2013-10-31 14:32:04 +01009545 return( ret );
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01009546}
9547
9548/*
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009549 * Check record counters and renegotiate if they're above the limit.
9550 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009551static int ssl_check_ctr_renegotiate( mbedtls_ssl_context *ssl )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009552{
Andres AG2196c7f2016-12-15 17:01:16 +00009553 size_t ep_len = ssl_ep_len( ssl );
9554 int in_ctr_cmp;
9555 int out_ctr_cmp;
9556
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009557 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER ||
9558 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING ||
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009559 ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009560 {
9561 return( 0 );
9562 }
9563
Andres AG2196c7f2016-12-15 17:01:16 +00009564 in_ctr_cmp = memcmp( ssl->in_ctr + ep_len,
9565 ssl->conf->renego_period + ep_len, 8 - ep_len );
Hanno Becker19859472018-08-06 09:40:20 +01009566 out_ctr_cmp = memcmp( ssl->cur_out_ctr + ep_len,
Andres AG2196c7f2016-12-15 17:01:16 +00009567 ssl->conf->renego_period + ep_len, 8 - ep_len );
9568
9569 if( in_ctr_cmp <= 0 && out_ctr_cmp <= 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009570 {
9571 return( 0 );
9572 }
9573
Manuel Pégourié-Gonnardcb0d2122015-07-22 11:52:11 +02009574 MBEDTLS_SSL_DEBUG_MSG( 1, ( "record counter limit reached: renegotiate" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009575 return( mbedtls_ssl_renegotiate( ssl ) );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009576}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009577#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00009578
9579/*
9580 * Receive application data decrypted from the SSL layer
9581 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009582int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009583{
Hanno Becker4a810fb2017-05-24 16:27:30 +01009584 int ret;
Paul Bakker23986e52011-04-24 08:57:21 +00009585 size_t n;
Paul Bakker5121ce52009-01-03 21:22:43 +00009586
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02009587 if( ssl == NULL || ssl->conf == NULL )
9588 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
9589
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009590 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009591
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009592#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009593 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009594 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009595 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009596 return( ret );
9597
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009598 if( ssl->handshake != NULL &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009599 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009600 {
Manuel Pégourié-Gonnard87a346f2017-09-13 12:45:21 +02009601 if( ( ret = mbedtls_ssl_flight_transmit( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009602 return( ret );
9603 }
Manuel Pégourié-Gonnardabf16242014-09-23 09:42:16 +02009604 }
9605#endif
9606
Hanno Becker4a810fb2017-05-24 16:27:30 +01009607 /*
9608 * Check if renegotiation is necessary and/or handshake is
9609 * in process. If yes, perform/continue, and fall through
9610 * if an unexpected packet is received while the client
9611 * is waiting for the ServerHello.
9612 *
9613 * (There is no equivalent to the last condition on
9614 * the server-side as it is not treated as within
9615 * a handshake while waiting for the ClientHello
9616 * after a renegotiation request.)
9617 */
9618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009619#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009620 ret = ssl_check_ctr_renegotiate( ssl );
9621 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9622 ret != 0 )
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009623 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009624 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnardb4458052014-11-04 21:04:22 +01009625 return( ret );
9626 }
9627#endif
9628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009629 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +00009630 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009631 ret = mbedtls_ssl_handshake( ssl );
Hanno Becker4a810fb2017-05-24 16:27:30 +01009632 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9633 ret != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009634 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009635 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009636 return( ret );
9637 }
9638 }
9639
Hanno Beckere41158b2017-10-23 13:30:32 +01009640 /* Loop as long as no application data record is available */
Hanno Becker90333da2017-10-10 11:27:13 +01009641 while( ssl->in_offt == NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +00009642 {
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009643 /* Start timer if not already running */
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009644 if( ssl->f_get_timer != NULL &&
9645 ssl->f_get_timer( ssl->p_timer ) == -1 )
9646 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009647 ssl_set_timer( ssl, ssl->conf->read_timeout );
Manuel Pégourié-Gonnard545102e2015-05-13 17:28:43 +02009648 }
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009649
Hanno Becker327c93b2018-08-15 13:56:18 +01009650 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009651 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009652 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
9653 return( 0 );
Paul Bakker831a7552011-05-18 13:32:51 +00009654
Hanno Becker4a810fb2017-05-24 16:27:30 +01009655 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
9656 return( ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009657 }
9658
9659 if( ssl->in_msglen == 0 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009660 ssl->in_msgtype == MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009661 {
9662 /*
9663 * OpenSSL sends empty messages to randomize the IV
9664 */
Hanno Becker327c93b2018-08-15 13:56:18 +01009665 if( ( ret = mbedtls_ssl_read_record( ssl, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009666 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009667 if( ret == MBEDTLS_ERR_SSL_CONN_EOF )
Paul Bakker831a7552011-05-18 13:32:51 +00009668 return( 0 );
9669
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009670 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_read_record", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009671 return( ret );
9672 }
9673 }
9674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE )
Paul Bakker48916f92012-09-16 19:57:18 +00009676 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009677 MBEDTLS_SSL_DEBUG_MSG( 1, ( "received handshake message" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009678
Hanno Becker4a810fb2017-05-24 16:27:30 +01009679 /*
9680 * - For client-side, expect SERVER_HELLO_REQUEST.
9681 * - For server-side, expect CLIENT_HELLO.
9682 * - Fail (TLS) or silently drop record (DTLS) in other cases.
9683 */
9684
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009685#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009686 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009687 ( ssl->in_msg[0] != MBEDTLS_SSL_HS_HELLO_REQUEST ||
Hanno Becker4a810fb2017-05-24 16:27:30 +01009688 ssl->in_hslen != mbedtls_ssl_hs_hdr_len( ssl ) ) )
Paul Bakker48916f92012-09-16 19:57:18 +00009689 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009690 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not HelloRequest)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009691
9692 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009693#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009694 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009695 {
9696 continue;
9697 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009698#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009699 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009700 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009701#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009702
Hanno Becker4a810fb2017-05-24 16:27:30 +01009703#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009704 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009705 ssl->in_msg[0] != MBEDTLS_SSL_HS_CLIENT_HELLO )
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009706 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009707 MBEDTLS_SSL_DEBUG_MSG( 1, ( "handshake received (not ClientHello)" ) );
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009708
9709 /* With DTLS, drop the packet (probably from last handshake) */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009710#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009711 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Hanno Becker90333da2017-10-10 11:27:13 +01009712 {
9713 continue;
9714 }
Manuel Pégourié-Gonnard990f9e42014-09-06 12:27:02 +02009715#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009716 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker48916f92012-09-16 19:57:18 +00009717 }
Hanno Becker4a810fb2017-05-24 16:27:30 +01009718#endif /* MBEDTLS_SSL_SRV_C */
9719
Hanno Becker21df7f92017-10-17 11:03:26 +01009720#if defined(MBEDTLS_SSL_RENEGOTIATION)
Hanno Becker4a810fb2017-05-24 16:27:30 +01009721 /* Determine whether renegotiation attempt should be accepted */
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009722 if( ! ( ssl->conf->disable_renegotiation == MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
9723 ( ssl->secure_renegotiation == MBEDTLS_SSL_LEGACY_RENEGOTIATION &&
9724 ssl->conf->allow_legacy_renegotiation ==
9725 MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION ) ) )
9726 {
9727 /*
9728 * Accept renegotiation request
9729 */
Paul Bakker48916f92012-09-16 19:57:18 +00009730
Hanno Beckerb4ff0aa2017-10-17 11:03:04 +01009731 /* DTLS clients need to know renego is server-initiated */
9732#if defined(MBEDTLS_SSL_PROTO_DTLS)
9733 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
9734 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT )
9735 {
9736 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
9737 }
9738#endif
9739 ret = ssl_start_renegotiation( ssl );
9740 if( ret != MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO &&
9741 ret != 0 )
9742 {
9743 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_start_renegotiation", ret );
9744 return( ret );
9745 }
9746 }
9747 else
Hanno Becker21df7f92017-10-17 11:03:26 +01009748#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker48916f92012-09-16 19:57:18 +00009749 {
Hanno Becker4a810fb2017-05-24 16:27:30 +01009750 /*
9751 * Refuse renegotiation
9752 */
9753
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009754 MBEDTLS_SSL_DEBUG_MSG( 3, ( "refusing renegotiation, sending alert" ) );
Paul Bakker48916f92012-09-16 19:57:18 +00009755
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009756#if defined(MBEDTLS_SSL_PROTO_SSL3)
9757 if( ssl->minor_ver == MBEDTLS_SSL_MINOR_VERSION_0 )
Paul Bakker48916f92012-09-16 19:57:18 +00009758 {
Gilles Peskine92e44262017-05-10 17:27:49 +02009759 /* SSLv3 does not have a "no_renegotiation" warning, so
9760 we send a fatal alert and abort the connection. */
9761 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
9762 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE );
9763 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009764 }
9765 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009766#endif /* MBEDTLS_SSL_PROTO_SSL3 */
9767#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
9768 defined(MBEDTLS_SSL_PROTO_TLS1_2)
9769 if( ssl->minor_ver >= MBEDTLS_SSL_MINOR_VERSION_1 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009770 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009771 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
9772 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
9773 MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION ) ) != 0 )
Paul Bakkerd0f6fa72012-09-17 09:18:12 +00009774 {
9775 return( ret );
9776 }
Paul Bakker48916f92012-09-16 19:57:18 +00009777 }
Paul Bakkerd2f068e2013-08-27 21:19:20 +02009778 else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009779#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 ||
9780 MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker577e0062013-08-28 11:57:20 +02009781 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009782 MBEDTLS_SSL_DEBUG_MSG( 1, ( "should never happen" ) );
9783 return( MBEDTLS_ERR_SSL_INTERNAL_ERROR );
Paul Bakker577e0062013-08-28 11:57:20 +02009784 }
Paul Bakker48916f92012-09-16 19:57:18 +00009785 }
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009786
Hanno Becker90333da2017-10-10 11:27:13 +01009787 /* At this point, we don't know whether the renegotiation has been
9788 * completed or not. The cases to consider are the following:
9789 * 1) The renegotiation is complete. In this case, no new record
9790 * has been read yet.
9791 * 2) The renegotiation is incomplete because the client received
9792 * an application data record while awaiting the ServerHello.
9793 * 3) The renegotiation is incomplete because the client received
9794 * a non-handshake, non-application data message while awaiting
9795 * the ServerHello.
9796 * In each of these case, looping will be the proper action:
9797 * - For 1), the next iteration will read a new record and check
9798 * if it's application data.
9799 * - For 2), the loop condition isn't satisfied as application data
9800 * is present, hence continue is the same as break
9801 * - For 3), the loop condition is satisfied and read_record
9802 * will re-deliver the message that was held back by the client
9803 * when expecting the ServerHello.
9804 */
9805 continue;
Paul Bakker48916f92012-09-16 19:57:18 +00009806 }
Hanno Becker21df7f92017-10-17 11:03:26 +01009807#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009808 else if( ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009809 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009810 if( ssl->conf->renego_max_records >= 0 )
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009811 {
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009812 if( ++ssl->renego_records_seen > ssl->conf->renego_max_records )
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009813 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009814 MBEDTLS_SSL_DEBUG_MSG( 1, ( "renegotiation requested, "
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009815 "but not honored by client" ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009816 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009817 }
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02009818 }
Manuel Pégourié-Gonnard6d8404d2013-10-30 16:41:45 +01009819 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009820#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009822 /* Fatal and closure alerts handled by mbedtls_ssl_read_record() */
9823 if( ssl->in_msgtype == MBEDTLS_SSL_MSG_ALERT )
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009825 MBEDTLS_SSL_DEBUG_MSG( 2, ( "ignoring non-fatal non-closure alert" ) );
Manuel Pégourié-Gonnard88369942015-05-06 16:19:31 +01009826 return( MBEDTLS_ERR_SSL_WANT_READ );
Manuel Pégourié-Gonnardf26a1e82014-08-19 12:28:50 +02009827 }
9828
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009829 if( ssl->in_msgtype != MBEDTLS_SSL_MSG_APPLICATION_DATA )
Paul Bakker5121ce52009-01-03 21:22:43 +00009830 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009831 MBEDTLS_SSL_DEBUG_MSG( 1, ( "bad application data message" ) );
9832 return( MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE );
Paul Bakker5121ce52009-01-03 21:22:43 +00009833 }
9834
9835 ssl->in_offt = ssl->in_msg;
Manuel Pégourié-Gonnard6b651412014-10-01 18:29:03 +02009836
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009837 /* We're going to return something now, cancel timer,
9838 * except if handshake (renegotiation) is in progress */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009839 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnardba958b82014-10-09 16:13:44 +02009840 ssl_set_timer( ssl, 0 );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009841
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02009842#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009843 /* If we requested renego but received AppData, resend HelloRequest.
9844 * Do it now, after setting in_offt, to avoid taking this branch
9845 * again if ssl_write_hello_request() returns WANT_WRITE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009846#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009847 if( ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009848 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009849 {
Manuel Pégourié-Gonnarddf3acd82014-10-15 15:07:45 +02009850 if( ( ret = ssl_resend_hello_request( ssl ) ) != 0 )
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009852 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_resend_hello_request", ret );
Manuel Pégourié-Gonnard26a4cf62014-10-15 13:52:48 +02009853 return( ret );
9854 }
9855 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009856#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Hanno Becker4a810fb2017-05-24 16:27:30 +01009857#endif /* MBEDTLS_SSL_PROTO_DTLS */
Paul Bakker5121ce52009-01-03 21:22:43 +00009858 }
9859
9860 n = ( len < ssl->in_msglen )
9861 ? len : ssl->in_msglen;
9862
9863 memcpy( buf, ssl->in_offt, n );
9864 ssl->in_msglen -= n;
9865
9866 if( ssl->in_msglen == 0 )
Hanno Becker4a810fb2017-05-24 16:27:30 +01009867 {
9868 /* all bytes consumed */
Paul Bakker5121ce52009-01-03 21:22:43 +00009869 ssl->in_offt = NULL;
Hanno Beckerbdf39052017-06-09 10:42:03 +01009870 ssl->keep_current_message = 0;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009872 else
Hanno Becker4a810fb2017-05-24 16:27:30 +01009873 {
Paul Bakker5121ce52009-01-03 21:22:43 +00009874 /* more data available */
9875 ssl->in_offt += n;
Hanno Becker4a810fb2017-05-24 16:27:30 +01009876 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009877
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009878 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= read" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +00009879
Paul Bakker23986e52011-04-24 08:57:21 +00009880 return( (int) n );
Paul Bakker5121ce52009-01-03 21:22:43 +00009881}
9882
9883/*
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009884 * Send application data to be encrypted by the SSL layer, taking care of max
9885 * fragment length and buffer size.
9886 *
9887 * According to RFC 5246 Section 6.2.1:
9888 *
9889 * Zero-length fragments of Application data MAY be sent as they are
9890 * potentially useful as a traffic analysis countermeasure.
9891 *
9892 * Therefore, it is possible that the input message length is 0 and the
9893 * corresponding return code is 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +00009894 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009895static int ssl_write_real( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009896 const unsigned char *buf, size_t len )
Paul Bakker5121ce52009-01-03 21:22:43 +00009897{
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02009898 int ret = mbedtls_ssl_get_max_out_record_payload( ssl );
9899 const size_t max_len = (size_t) ret;
9900
9901 if( ret < 0 )
9902 {
9903 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_get_max_out_record_payload", ret );
9904 return( ret );
9905 }
9906
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009907 if( len > max_len )
9908 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009909#if defined(MBEDTLS_SSL_PROTO_DTLS)
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +02009910 if( ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009911 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009912 MBEDTLS_SSL_DEBUG_MSG( 1, ( "fragment larger than the (negotiated) "
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009913 "maximum fragment length: %d > %d",
9914 len, max_len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009915 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009916 }
9917 else
9918#endif
9919 len = max_len;
9920 }
Paul Bakker887bd502011-06-08 13:10:54 +00009921
Paul Bakker5121ce52009-01-03 21:22:43 +00009922 if( ssl->out_left != 0 )
9923 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009924 /*
9925 * The user has previously tried to send the data and
9926 * MBEDTLS_ERR_SSL_WANT_WRITE or the message was only partially
9927 * written. In this case, we expect the high-level write function
9928 * (e.g. mbedtls_ssl_write()) to be called with the same parameters
9929 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009930 if( ( ret = mbedtls_ssl_flush_output( ssl ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +00009931 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009932 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_flush_output", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +00009933 return( ret );
9934 }
9935 }
Paul Bakker887bd502011-06-08 13:10:54 +00009936 else
Paul Bakker1fd00bf2011-03-14 20:50:15 +00009937 {
Andres Amaya Garcia5b923522017-09-28 14:41:17 +01009938 /*
9939 * The user is trying to send a message the first time, so we need to
9940 * copy the data into the internal buffers and setup the data structure
9941 * to keep track of partial writes
9942 */
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009943 ssl->out_msglen = len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009944 ssl->out_msgtype = MBEDTLS_SSL_MSG_APPLICATION_DATA;
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009945 memcpy( ssl->out_msg, buf, len );
Paul Bakker887bd502011-06-08 13:10:54 +00009946
Hanno Becker67bc7c32018-08-06 11:33:50 +01009947 if( ( ret = mbedtls_ssl_write_record( ssl, SSL_FORCE_FLUSH ) ) != 0 )
Paul Bakker887bd502011-06-08 13:10:54 +00009948 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009949 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_write_record", ret );
Paul Bakker887bd502011-06-08 13:10:54 +00009950 return( ret );
9951 }
Paul Bakker5121ce52009-01-03 21:22:43 +00009952 }
9953
Manuel Pégourié-Gonnard37e08e12014-10-13 17:55:52 +02009954 return( (int) len );
Paul Bakker5121ce52009-01-03 21:22:43 +00009955}
9956
9957/*
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009958 * Write application data, doing 1/n-1 splitting if necessary.
9959 *
9960 * With non-blocking I/O, ssl_write_real() may return WANT_WRITE,
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009961 * then the caller will call us again with the same arguments, so
Hanno Becker2b187c42017-09-18 14:58:11 +01009962 * remember whether we already did the split or not.
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009963 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009964#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009965static int ssl_write_split( mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009966 const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009967{
9968 int ret;
9969
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +01009970 if( ssl->conf->cbc_record_splitting ==
9971 MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED ||
Manuel Pégourié-Gonnardcfa477e2015-01-07 14:50:54 +01009972 len <= 1 ||
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009973 ssl->minor_ver > MBEDTLS_SSL_MINOR_VERSION_1 ||
9974 mbedtls_cipher_get_cipher_mode( &ssl->transform_out->cipher_ctx_enc )
9975 != MBEDTLS_MODE_CBC )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009976 {
9977 return( ssl_write_real( ssl, buf, len ) );
9978 }
9979
9980 if( ssl->split_done == 0 )
9981 {
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009982 if( ( ret = ssl_write_real( ssl, buf, 1 ) ) <= 0 )
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009983 return( ret );
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009984 ssl->split_done = 1;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009985 }
9986
Manuel Pégourié-Gonnarda852cf42015-01-13 20:56:15 +01009987 if( ( ret = ssl_write_real( ssl, buf + 1, len - 1 ) ) <= 0 )
9988 return( ret );
9989 ssl->split_done = 0;
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009990
9991 return( ret + 1 );
9992}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009993#endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
Manuel Pégourié-Gonnardd76314c2015-01-07 12:39:44 +01009994
9995/*
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009996 * Write application data (public-facing wrapper)
9997 */
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +02009998int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +02009999{
10000 int ret;
10001
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010002 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010003
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010004 if( ssl == NULL || ssl->conf == NULL )
10005 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10006
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010007#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010008 if( ( ret = ssl_check_ctr_renegotiate( ssl ) ) != 0 )
10009 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010010 MBEDTLS_SSL_DEBUG_RET( 1, "ssl_check_ctr_renegotiate", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010011 return( ret );
10012 }
10013#endif
10014
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010015 if( ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010016 {
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010017 if( ( ret = mbedtls_ssl_handshake( ssl ) ) != 0 )
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010018 {
Manuel Pégourié-Gonnard151dc772015-05-14 13:55:51 +020010019 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_handshake", ret );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010020 return( ret );
10021 }
10022 }
10023
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010024#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010025 ret = ssl_write_split( ssl, buf, len );
10026#else
10027 ret = ssl_write_real( ssl, buf, len );
10028#endif
10029
Manuel Pégourié-Gonnard144bc222015-04-17 20:39:07 +020010030 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write" ) );
Manuel Pégourié-Gonnarda2fce212015-04-15 19:09:03 +020010031
10032 return( ret );
10033}
10034
10035/*
Paul Bakker5121ce52009-01-03 21:22:43 +000010036 * Notify the peer that the connection is being closed
10037 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010038int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010039{
10040 int ret;
10041
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +020010042 if( ssl == NULL || ssl->conf == NULL )
10043 return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
10044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010045 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010046
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010047 if( ssl->out_left != 0 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010048 return( mbedtls_ssl_flush_output( ssl ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010050 if( ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER )
Paul Bakker5121ce52009-01-03 21:22:43 +000010051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010052 if( ( ret = mbedtls_ssl_send_alert_message( ssl,
10053 MBEDTLS_SSL_ALERT_LEVEL_WARNING,
10054 MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +000010055 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010056 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_ssl_send_alert_message", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +000010057 return( ret );
10058 }
10059 }
10060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010061 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= write close notify" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010062
Manuel Pégourié-Gonnarda13500f2014-08-19 16:14:04 +020010063 return( 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +000010064}
10065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010066void mbedtls_ssl_transform_free( mbedtls_ssl_transform *transform )
Paul Bakker48916f92012-09-16 19:57:18 +000010067{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010068 if( transform == NULL )
10069 return;
10070
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010071#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker48916f92012-09-16 19:57:18 +000010072 deflateEnd( &transform->ctx_deflate );
10073 inflateEnd( &transform->ctx_inflate );
10074#endif
10075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010076 mbedtls_cipher_free( &transform->cipher_ctx_enc );
10077 mbedtls_cipher_free( &transform->cipher_ctx_dec );
Manuel Pégourié-Gonnardf71e5872013-09-23 17:12:43 +020010078
Hanno Beckerd56ed242018-01-03 15:32:51 +000010079#if defined(MBEDTLS_SSL_SOME_MODES_USE_MAC)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010080 mbedtls_md_free( &transform->md_ctx_enc );
10081 mbedtls_md_free( &transform->md_ctx_dec );
Hanno Beckerd56ed242018-01-03 15:32:51 +000010082#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010083
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010084 mbedtls_platform_zeroize( transform, sizeof( mbedtls_ssl_transform ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010085}
10086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010087#if defined(MBEDTLS_X509_CRT_PARSE_C)
10088static void ssl_key_cert_free( mbedtls_ssl_key_cert *key_cert )
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010089{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010090 mbedtls_ssl_key_cert *cur = key_cert, *next;
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010091
10092 while( cur != NULL )
10093 {
10094 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010095 mbedtls_free( cur );
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010096 cur = next;
10097 }
10098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010099#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010100
Hanno Becker0271f962018-08-16 13:23:47 +010010101#if defined(MBEDTLS_SSL_PROTO_DTLS)
10102
10103static void ssl_buffering_free( mbedtls_ssl_context *ssl )
10104{
10105 unsigned offset;
10106 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10107
10108 if( hs == NULL )
10109 return;
10110
Hanno Becker283f5ef2018-08-24 09:34:47 +010010111 ssl_free_buffered_record( ssl );
10112
Hanno Becker0271f962018-08-16 13:23:47 +010010113 for( offset = 0; offset < MBEDTLS_SSL_MAX_BUFFERED_HS; offset++ )
Hanno Beckere605b192018-08-21 15:59:07 +010010114 ssl_buffering_free_slot( ssl, offset );
10115}
10116
10117static void ssl_buffering_free_slot( mbedtls_ssl_context *ssl,
10118 uint8_t slot )
10119{
10120 mbedtls_ssl_handshake_params * const hs = ssl->handshake;
10121 mbedtls_ssl_hs_buffer * const hs_buf = &hs->buffering.hs[slot];
Hanno Beckerb309b922018-08-23 13:18:05 +010010122
10123 if( slot >= MBEDTLS_SSL_MAX_BUFFERED_HS )
10124 return;
10125
Hanno Beckere605b192018-08-21 15:59:07 +010010126 if( hs_buf->is_valid == 1 )
Hanno Becker0271f962018-08-16 13:23:47 +010010127 {
Hanno Beckere605b192018-08-21 15:59:07 +010010128 hs->buffering.total_bytes_buffered -= hs_buf->data_len;
Hanno Becker805f2e12018-10-12 16:31:41 +010010129 mbedtls_platform_zeroize( hs_buf->data, hs_buf->data_len );
Hanno Beckere605b192018-08-21 15:59:07 +010010130 mbedtls_free( hs_buf->data );
10131 memset( hs_buf, 0, sizeof( mbedtls_ssl_hs_buffer ) );
Hanno Becker0271f962018-08-16 13:23:47 +010010132 }
10133}
10134
10135#endif /* MBEDTLS_SSL_PROTO_DTLS */
10136
Gilles Peskine9b562d52018-04-25 20:32:43 +020010137void mbedtls_ssl_handshake_free( mbedtls_ssl_context *ssl )
Paul Bakker48916f92012-09-16 19:57:18 +000010138{
Gilles Peskine9b562d52018-04-25 20:32:43 +020010139 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
10140
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010141 if( handshake == NULL )
10142 return;
10143
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010144#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
10145 if( ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0 )
10146 {
Gilles Peskine8f97af72018-04-26 11:46:10 +020010147 ssl->conf->f_async_cancel( ssl );
Gilles Peskinedf13d5c2018-04-25 20:39:48 +020010148 handshake->async_in_progress = 0;
10149 }
10150#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
10151
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010152#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10153 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10154 mbedtls_md5_free( &handshake->fin_md5 );
10155 mbedtls_sha1_free( &handshake->fin_sha1 );
10156#endif
10157#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10158#if defined(MBEDTLS_SHA256_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010159#if defined(MBEDTLS_USE_PSA_CRYPTO)
10160 psa_hash_abort( &handshake->fin_sha256_psa );
10161#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010162 mbedtls_sha256_free( &handshake->fin_sha256 );
10163#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010164#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010165#if defined(MBEDTLS_SHA512_C)
Andrzej Kurekeb342242019-01-29 09:14:33 -050010166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -050010167 psa_hash_abort( &handshake->fin_sha384_psa );
Andrzej Kurekeb342242019-01-29 09:14:33 -050010168#else
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010169 mbedtls_sha512_free( &handshake->fin_sha512 );
10170#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -050010171#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +020010172#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010174#if defined(MBEDTLS_DHM_C)
10175 mbedtls_dhm_free( &handshake->dhm_ctx );
Paul Bakker48916f92012-09-16 19:57:18 +000010176#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010177#if defined(MBEDTLS_ECDH_C)
10178 mbedtls_ecdh_free( &handshake->ecdh_ctx );
Paul Bakker61d113b2013-07-04 11:51:43 +020010179#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +020010180#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010181 mbedtls_ecjpake_free( &handshake->ecjpake_ctx );
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +020010182#if defined(MBEDTLS_SSL_CLI_C)
10183 mbedtls_free( handshake->ecjpake_cache );
10184 handshake->ecjpake_cache = NULL;
10185 handshake->ecjpake_cache_len = 0;
10186#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +020010187#endif
Paul Bakker61d113b2013-07-04 11:51:43 +020010188
Janos Follath4ae5c292016-02-10 11:27:43 +000010189#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
10190 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +020010191 /* explicit void pointer cast for buggy MS compiler */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010192 mbedtls_free( (void *) handshake->curves );
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +020010193#endif
10194
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010195#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10196 if( handshake->psk != NULL )
10197 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010198 mbedtls_platform_zeroize( handshake->psk, handshake->psk_len );
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +010010199 mbedtls_free( handshake->psk );
10200 }
10201#endif
10202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010203#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10204 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010205 /*
10206 * Free only the linked list wrapper, not the keys themselves
10207 * since the belong to the SNI callback
10208 */
10209 if( handshake->sni_key_cert != NULL )
10210 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010211 mbedtls_ssl_key_cert *cur = handshake->sni_key_cert, *next;
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010212
10213 while( cur != NULL )
10214 {
10215 next = cur->next;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010216 mbedtls_free( cur );
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +020010217 cur = next;
10218 }
10219 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010220#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +020010221
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010222#if defined(MBEDTLS_SSL__ECP_RESTARTABLE)
Manuel Pégourié-Gonnard6b7301c2017-08-15 12:08:45 +020010223 mbedtls_x509_crt_restart_free( &handshake->ecrs_ctx );
Hanno Becker3dad3112019-02-05 17:19:52 +000010224 if( handshake->ecrs_peer_cert != NULL )
10225 {
10226 mbedtls_x509_crt_free( handshake->ecrs_peer_cert );
10227 mbedtls_free( handshake->ecrs_peer_cert );
10228 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +020010229#endif
10230
Hanno Becker75173122019-02-06 16:18:31 +000010231#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
10232 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
10233 mbedtls_pk_free( &handshake->peer_pubkey );
10234#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
10235
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010236#if defined(MBEDTLS_SSL_PROTO_DTLS)
10237 mbedtls_free( handshake->verify_cookie );
Manuel Pégourié-Gonnardffa67be2014-09-19 11:18:57 +020010238 ssl_flight_free( handshake->flight );
Hanno Becker0271f962018-08-16 13:23:47 +010010239 ssl_buffering_free( ssl );
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +020010240#endif
10241
Hanno Becker4a63ed42019-01-08 11:39:35 +000010242#if defined(MBEDTLS_ECDH_C) && \
10243 defined(MBEDTLS_USE_PSA_CRYPTO)
10244 psa_destroy_key( handshake->ecdh_psa_privkey );
10245#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
10246
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010247 mbedtls_platform_zeroize( handshake,
10248 sizeof( mbedtls_ssl_handshake_params ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010249}
10250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010251void mbedtls_ssl_session_free( mbedtls_ssl_session *session )
Paul Bakker48916f92012-09-16 19:57:18 +000010252{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010253 if( session == NULL )
10254 return;
10255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010256#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker1294a0b2019-02-05 12:38:15 +000010257 ssl_clear_peer_cert( session );
Paul Bakkered27a042013-04-18 22:46:23 +020010258#endif
Paul Bakker0a597072012-09-25 21:55:46 +000010259
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +020010260#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010261 mbedtls_free( session->ticket );
Paul Bakkera503a632013-08-14 13:48:06 +020010262#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +020010263
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010264 mbedtls_platform_zeroize( session, sizeof( mbedtls_ssl_session ) );
Paul Bakker48916f92012-09-16 19:57:18 +000010265}
10266
Paul Bakker5121ce52009-01-03 21:22:43 +000010267/*
10268 * Free an SSL context
10269 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010270void mbedtls_ssl_free( mbedtls_ssl_context *ssl )
Paul Bakker5121ce52009-01-03 21:22:43 +000010271{
Paul Bakkeraccaffe2014-06-26 13:37:14 +020010272 if( ssl == NULL )
10273 return;
10274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010275 MBEDTLS_SSL_DEBUG_MSG( 2, ( "=> free" ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010276
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010277 if( ssl->out_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010278 {
Angus Grattond8213d02016-05-25 20:56:48 +100010279 mbedtls_platform_zeroize( ssl->out_buf, MBEDTLS_SSL_OUT_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010280 mbedtls_free( ssl->out_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010281 }
10282
Manuel Pégourié-Gonnard7ee6f0e2014-02-13 10:54:07 +010010283 if( ssl->in_buf != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010284 {
Angus Grattond8213d02016-05-25 20:56:48 +100010285 mbedtls_platform_zeroize( ssl->in_buf, MBEDTLS_SSL_IN_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010286 mbedtls_free( ssl->in_buf );
Paul Bakker5121ce52009-01-03 21:22:43 +000010287 }
10288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010289#if defined(MBEDTLS_ZLIB_SUPPORT)
Paul Bakker16770332013-10-11 09:59:44 +020010290 if( ssl->compress_buf != NULL )
10291 {
Angus Grattond8213d02016-05-25 20:56:48 +100010292 mbedtls_platform_zeroize( ssl->compress_buf, MBEDTLS_SSL_COMPRESS_BUFFER_LEN );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010293 mbedtls_free( ssl->compress_buf );
Paul Bakker16770332013-10-11 09:59:44 +020010294 }
10295#endif
10296
Paul Bakker48916f92012-09-16 19:57:18 +000010297 if( ssl->transform )
10298 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010299 mbedtls_ssl_transform_free( ssl->transform );
10300 mbedtls_free( ssl->transform );
Paul Bakker48916f92012-09-16 19:57:18 +000010301 }
10302
10303 if( ssl->handshake )
10304 {
Gilles Peskine9b562d52018-04-25 20:32:43 +020010305 mbedtls_ssl_handshake_free( ssl );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010306 mbedtls_ssl_transform_free( ssl->transform_negotiate );
10307 mbedtls_ssl_session_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010308
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010309 mbedtls_free( ssl->handshake );
10310 mbedtls_free( ssl->transform_negotiate );
10311 mbedtls_free( ssl->session_negotiate );
Paul Bakker48916f92012-09-16 19:57:18 +000010312 }
10313
Paul Bakkerc0463502013-02-14 11:19:38 +010010314 if( ssl->session )
10315 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010316 mbedtls_ssl_session_free( ssl->session );
10317 mbedtls_free( ssl->session );
Paul Bakkerc0463502013-02-14 11:19:38 +010010318 }
10319
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +020010320#if defined(MBEDTLS_X509_CRT_PARSE_C)
Paul Bakker66d5d072014-06-17 16:39:18 +020010321 if( ssl->hostname != NULL )
Paul Bakker5121ce52009-01-03 21:22:43 +000010322 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010323 mbedtls_platform_zeroize( ssl->hostname, strlen( ssl->hostname ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010324 mbedtls_free( ssl->hostname );
Paul Bakker5121ce52009-01-03 21:22:43 +000010325 }
Paul Bakker0be444a2013-08-27 21:55:01 +020010326#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000010327
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010328#if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
10329 if( mbedtls_ssl_hw_record_finish != NULL )
Paul Bakker05ef8352012-05-08 09:17:57 +000010330 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010331 MBEDTLS_SSL_DEBUG_MSG( 2, ( "going for mbedtls_ssl_hw_record_finish()" ) );
10332 mbedtls_ssl_hw_record_finish( ssl );
Paul Bakker05ef8352012-05-08 09:17:57 +000010333 }
10334#endif
10335
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010336#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010337 mbedtls_free( ssl->cli_id );
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +020010338#endif
10339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010340 MBEDTLS_SSL_DEBUG_MSG( 2, ( "<= free" ) );
Paul Bakker2da561c2009-02-05 18:00:28 +000010341
Paul Bakker86f04f42013-02-14 11:20:09 +010010342 /* Actually clear after last debug message */
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010343 mbedtls_platform_zeroize( ssl, sizeof( mbedtls_ssl_context ) );
Paul Bakker5121ce52009-01-03 21:22:43 +000010344}
10345
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010346/*
10347 * Initialze mbedtls_ssl_config
10348 */
10349void mbedtls_ssl_config_init( mbedtls_ssl_config *conf )
10350{
10351 memset( conf, 0, sizeof( mbedtls_ssl_config ) );
10352}
10353
Simon Butcherc97b6972015-12-27 23:48:17 +000010354#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010355static int ssl_preset_default_hashes[] = {
10356#if defined(MBEDTLS_SHA512_C)
10357 MBEDTLS_MD_SHA512,
10358 MBEDTLS_MD_SHA384,
10359#endif
10360#if defined(MBEDTLS_SHA256_C)
10361 MBEDTLS_MD_SHA256,
10362 MBEDTLS_MD_SHA224,
10363#endif
Gilles Peskine5d2511c2017-05-12 13:16:40 +020010364#if defined(MBEDTLS_SHA1_C) && defined(MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010365 MBEDTLS_MD_SHA1,
10366#endif
10367 MBEDTLS_MD_NONE
10368};
Simon Butcherc97b6972015-12-27 23:48:17 +000010369#endif
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010370
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010371static int ssl_preset_suiteb_ciphersuites[] = {
10372 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
10373 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
10374 0
10375};
10376
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010377#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010378static int ssl_preset_suiteb_hashes[] = {
10379 MBEDTLS_MD_SHA256,
10380 MBEDTLS_MD_SHA384,
10381 MBEDTLS_MD_NONE
10382};
10383#endif
10384
10385#if defined(MBEDTLS_ECP_C)
10386static mbedtls_ecp_group_id ssl_preset_suiteb_curves[] = {
10387 MBEDTLS_ECP_DP_SECP256R1,
10388 MBEDTLS_ECP_DP_SECP384R1,
10389 MBEDTLS_ECP_DP_NONE
10390};
10391#endif
10392
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010393/*
Tillmann Karras588ad502015-09-25 04:27:22 +020010394 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010395 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010396int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010397 int endpoint, int transport, int preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010398{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010399#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010400 int ret;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +020010401#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010402
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +020010403 /* Use the functions here so that they are covered in tests,
10404 * but otherwise access member directly for efficiency */
10405 mbedtls_ssl_conf_endpoint( conf, endpoint );
10406 mbedtls_ssl_conf_transport( conf, transport );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010407
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010408 /*
10409 * Things that are common to all presets
10410 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +020010411#if defined(MBEDTLS_SSL_CLI_C)
10412 if( endpoint == MBEDTLS_SSL_IS_CLIENT )
10413 {
10414 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
10415#if defined(MBEDTLS_SSL_SESSION_TICKETS)
10416 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
10417#endif
10418 }
10419#endif
10420
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010421#if defined(MBEDTLS_ARC4_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010422 conf->arc4_disabled = MBEDTLS_SSL_ARC4_DISABLED;
Manuel Pégourié-Gonnard66dc5552015-05-14 12:28:21 +020010423#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010424
10425#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
10426 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
10427#endif
10428
10429#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
10430 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
10431#endif
10432
Manuel Pégourié-Gonnard17eab2b2015-05-05 16:34:53 +010010433#if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
10434 conf->cbc_record_splitting = MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED;
10435#endif
10436
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +020010437#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010438 conf->f_cookie_write = ssl_cookie_write_dummy;
10439 conf->f_cookie_check = ssl_cookie_check_dummy;
10440#endif
10441
10442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
10443 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
10444#endif
10445
Janos Follath088ce432017-04-10 12:42:31 +010010446#if defined(MBEDTLS_SSL_SRV_C)
10447 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
10448#endif
10449
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010450#if defined(MBEDTLS_SSL_PROTO_DTLS)
10451 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
10452 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
10453#endif
10454
10455#if defined(MBEDTLS_SSL_RENEGOTIATION)
10456 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Andres AG2196c7f2016-12-15 17:01:16 +000010457 memset( conf->renego_period, 0x00, 2 );
10458 memset( conf->renego_period + 2, 0xFF, 6 );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010459#endif
10460
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010461#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
10462 if( endpoint == MBEDTLS_SSL_IS_SERVER )
10463 {
Hanno Becker00d0a682017-10-04 13:14:29 +010010464 const unsigned char dhm_p[] =
10465 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
10466 const unsigned char dhm_g[] =
10467 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
10468
Hanno Beckera90658f2017-10-04 15:29:08 +010010469 if ( ( ret = mbedtls_ssl_conf_dh_param_bin( conf,
10470 dhm_p, sizeof( dhm_p ),
10471 dhm_g, sizeof( dhm_g ) ) ) != 0 )
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010472 {
10473 return( ret );
10474 }
10475 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +020010476#endif
10477
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010478 /*
10479 * Preset-specific defaults
10480 */
10481 switch( preset )
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010482 {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010483 /*
10484 * NSA Suite B
10485 */
10486 case MBEDTLS_SSL_PRESET_SUITEB:
10487 conf->min_major_ver = MBEDTLS_SSL_MAJOR_VERSION_3;
10488 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_3; /* TLS 1.2 */
10489 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10490 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10491
10492 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10493 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10494 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10495 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10496 ssl_preset_suiteb_ciphersuites;
10497
10498#if defined(MBEDTLS_X509_CRT_PARSE_C)
10499 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010500#endif
10501
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010502#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010503 conf->sig_hashes = ssl_preset_suiteb_hashes;
10504#endif
10505
10506#if defined(MBEDTLS_ECP_C)
10507 conf->curve_list = ssl_preset_suiteb_curves;
10508#endif
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +020010509 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010510
10511 /*
10512 * Default
10513 */
10514 default:
Ron Eldor5e9f14d2017-05-28 10:46:38 +030010515 conf->min_major_ver = ( MBEDTLS_SSL_MIN_MAJOR_VERSION >
10516 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION ) ?
10517 MBEDTLS_SSL_MIN_MAJOR_VERSION :
10518 MBEDTLS_SSL_MIN_VALID_MAJOR_VERSION;
10519 conf->min_minor_ver = ( MBEDTLS_SSL_MIN_MINOR_VERSION >
10520 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION ) ?
10521 MBEDTLS_SSL_MIN_MINOR_VERSION :
10522 MBEDTLS_SSL_MIN_VALID_MINOR_VERSION;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010523 conf->max_major_ver = MBEDTLS_SSL_MAX_MAJOR_VERSION;
10524 conf->max_minor_ver = MBEDTLS_SSL_MAX_MINOR_VERSION;
10525
10526#if defined(MBEDTLS_SSL_PROTO_DTLS)
10527 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
10528 conf->min_minor_ver = MBEDTLS_SSL_MINOR_VERSION_2;
10529#endif
10530
10531 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_0] =
10532 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_1] =
10533 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_2] =
10534 conf->ciphersuite_list[MBEDTLS_SSL_MINOR_VERSION_3] =
10535 mbedtls_ssl_list_ciphersuites();
10536
10537#if defined(MBEDTLS_X509_CRT_PARSE_C)
10538 conf->cert_profile = &mbedtls_x509_crt_profile_default;
10539#endif
10540
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010541#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard47229c72015-12-04 15:02:56 +010010542 conf->sig_hashes = ssl_preset_default_hashes;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +020010543#endif
10544
10545#if defined(MBEDTLS_ECP_C)
10546 conf->curve_list = mbedtls_ecp_grp_id_list();
10547#endif
10548
10549#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
10550 conf->dhm_min_bitlen = 1024;
10551#endif
10552 }
10553
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010554 return( 0 );
10555}
10556
10557/*
10558 * Free mbedtls_ssl_config
10559 */
10560void mbedtls_ssl_config_free( mbedtls_ssl_config *conf )
10561{
10562#if defined(MBEDTLS_DHM_C)
10563 mbedtls_mpi_free( &conf->dhm_P );
10564 mbedtls_mpi_free( &conf->dhm_G );
10565#endif
10566
10567#if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
10568 if( conf->psk != NULL )
10569 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010570 mbedtls_platform_zeroize( conf->psk, conf->psk_len );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010571 mbedtls_free( conf->psk );
Azim Khan27e8a122018-03-21 14:24:11 +000010572 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010573 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +090010574 }
10575
10576 if( conf->psk_identity != NULL )
10577 {
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010578 mbedtls_platform_zeroize( conf->psk_identity, conf->psk_identity_len );
junyeonLEE316b1622017-12-20 16:29:30 +090010579 mbedtls_free( conf->psk_identity );
Azim Khan27e8a122018-03-21 14:24:11 +000010580 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010581 conf->psk_identity_len = 0;
10582 }
10583#endif
10584
10585#if defined(MBEDTLS_X509_CRT_PARSE_C)
10586 ssl_key_cert_free( conf->key_cert );
10587#endif
10588
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050010589 mbedtls_platform_zeroize( conf, sizeof( mbedtls_ssl_config ) );
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +020010590}
10591
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010592#if defined(MBEDTLS_PK_C) && \
10593 ( defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C) )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010594/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010595 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010596 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010597unsigned char mbedtls_ssl_sig_from_pk( mbedtls_pk_context *pk )
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010598{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010599#if defined(MBEDTLS_RSA_C)
10600 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_RSA ) )
10601 return( MBEDTLS_SSL_SIG_RSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010602#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010603#if defined(MBEDTLS_ECDSA_C)
10604 if( mbedtls_pk_can_do( pk, MBEDTLS_PK_ECDSA ) )
10605 return( MBEDTLS_SSL_SIG_ECDSA );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010606#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010607 return( MBEDTLS_SSL_SIG_ANON );
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +020010608}
10609
Hanno Becker7e5437a2017-04-28 17:15:26 +010010610unsigned char mbedtls_ssl_sig_from_pk_alg( mbedtls_pk_type_t type )
10611{
10612 switch( type ) {
10613 case MBEDTLS_PK_RSA:
10614 return( MBEDTLS_SSL_SIG_RSA );
10615 case MBEDTLS_PK_ECDSA:
10616 case MBEDTLS_PK_ECKEY:
10617 return( MBEDTLS_SSL_SIG_ECDSA );
10618 default:
10619 return( MBEDTLS_SSL_SIG_ANON );
10620 }
10621}
10622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010623mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig( unsigned char sig )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010624{
10625 switch( sig )
10626 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010627#if defined(MBEDTLS_RSA_C)
10628 case MBEDTLS_SSL_SIG_RSA:
10629 return( MBEDTLS_PK_RSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010630#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010631#if defined(MBEDTLS_ECDSA_C)
10632 case MBEDTLS_SSL_SIG_ECDSA:
10633 return( MBEDTLS_PK_ECDSA );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010634#endif
10635 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010636 return( MBEDTLS_PK_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010637 }
10638}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +020010639#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010640
Hanno Becker7e5437a2017-04-28 17:15:26 +010010641#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && \
10642 defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
10643
10644/* Find an entry in a signature-hash set matching a given hash algorithm. */
10645mbedtls_md_type_t mbedtls_ssl_sig_hash_set_find( mbedtls_ssl_sig_hash_set_t *set,
10646 mbedtls_pk_type_t sig_alg )
10647{
10648 switch( sig_alg )
10649 {
10650 case MBEDTLS_PK_RSA:
10651 return( set->rsa );
10652 case MBEDTLS_PK_ECDSA:
10653 return( set->ecdsa );
10654 default:
10655 return( MBEDTLS_MD_NONE );
10656 }
10657}
10658
10659/* Add a signature-hash-pair to a signature-hash set */
10660void mbedtls_ssl_sig_hash_set_add( mbedtls_ssl_sig_hash_set_t *set,
10661 mbedtls_pk_type_t sig_alg,
10662 mbedtls_md_type_t md_alg )
10663{
10664 switch( sig_alg )
10665 {
10666 case MBEDTLS_PK_RSA:
10667 if( set->rsa == MBEDTLS_MD_NONE )
10668 set->rsa = md_alg;
10669 break;
10670
10671 case MBEDTLS_PK_ECDSA:
10672 if( set->ecdsa == MBEDTLS_MD_NONE )
10673 set->ecdsa = md_alg;
10674 break;
10675
10676 default:
10677 break;
10678 }
10679}
10680
10681/* Allow exactly one hash algorithm for each signature. */
10682void mbedtls_ssl_sig_hash_set_const_hash( mbedtls_ssl_sig_hash_set_t *set,
10683 mbedtls_md_type_t md_alg )
10684{
10685 set->rsa = md_alg;
10686 set->ecdsa = md_alg;
10687}
10688
10689#endif /* MBEDTLS_SSL_PROTO_TLS1_2) &&
10690 MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
10691
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010692/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010693 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +020010694 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010695mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash( unsigned char hash )
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010696{
10697 switch( hash )
10698 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010699#if defined(MBEDTLS_MD5_C)
10700 case MBEDTLS_SSL_HASH_MD5:
10701 return( MBEDTLS_MD_MD5 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010702#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010703#if defined(MBEDTLS_SHA1_C)
10704 case MBEDTLS_SSL_HASH_SHA1:
10705 return( MBEDTLS_MD_SHA1 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010706#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010707#if defined(MBEDTLS_SHA256_C)
10708 case MBEDTLS_SSL_HASH_SHA224:
10709 return( MBEDTLS_MD_SHA224 );
10710 case MBEDTLS_SSL_HASH_SHA256:
10711 return( MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010712#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010713#if defined(MBEDTLS_SHA512_C)
10714 case MBEDTLS_SSL_HASH_SHA384:
10715 return( MBEDTLS_MD_SHA384 );
10716 case MBEDTLS_SSL_HASH_SHA512:
10717 return( MBEDTLS_MD_SHA512 );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010718#endif
10719 default:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010720 return( MBEDTLS_MD_NONE );
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +020010721 }
10722}
10723
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010724/*
10725 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
10726 */
10727unsigned char mbedtls_ssl_hash_from_md_alg( int md )
10728{
10729 switch( md )
10730 {
10731#if defined(MBEDTLS_MD5_C)
10732 case MBEDTLS_MD_MD5:
10733 return( MBEDTLS_SSL_HASH_MD5 );
10734#endif
10735#if defined(MBEDTLS_SHA1_C)
10736 case MBEDTLS_MD_SHA1:
10737 return( MBEDTLS_SSL_HASH_SHA1 );
10738#endif
10739#if defined(MBEDTLS_SHA256_C)
10740 case MBEDTLS_MD_SHA224:
10741 return( MBEDTLS_SSL_HASH_SHA224 );
10742 case MBEDTLS_MD_SHA256:
10743 return( MBEDTLS_SSL_HASH_SHA256 );
10744#endif
10745#if defined(MBEDTLS_SHA512_C)
10746 case MBEDTLS_MD_SHA384:
10747 return( MBEDTLS_SSL_HASH_SHA384 );
10748 case MBEDTLS_MD_SHA512:
10749 return( MBEDTLS_SSL_HASH_SHA512 );
10750#endif
10751 default:
10752 return( MBEDTLS_SSL_HASH_NONE );
10753 }
10754}
10755
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010756#if defined(MBEDTLS_ECP_C)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010757/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010758 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010759 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010760 */
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010761int mbedtls_ssl_check_curve( const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010762{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010763 const mbedtls_ecp_group_id *gid;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010764
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010765 if( ssl->conf->curve_list == NULL )
10766 return( -1 );
10767
Manuel Pégourié-Gonnard7ca4e4d2015-05-04 10:55:58 +020010768 for( gid = ssl->conf->curve_list; *gid != MBEDTLS_ECP_DP_NONE; gid++ )
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010769 if( *gid == grp_id )
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010770 return( 0 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010771
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +020010772 return( -1 );
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +010010773}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +020010774#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010775
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010776#if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010777/*
10778 * Check if a hash proposed by the peer is in our list.
10779 * Return 0 if we're willing to use it, -1 otherwise.
10780 */
10781int mbedtls_ssl_check_sig_hash( const mbedtls_ssl_context *ssl,
10782 mbedtls_md_type_t md )
10783{
10784 const int *cur;
10785
10786 if( ssl->conf->sig_hashes == NULL )
10787 return( -1 );
10788
10789 for( cur = ssl->conf->sig_hashes; *cur != MBEDTLS_MD_NONE; cur++ )
10790 if( *cur == (int) md )
10791 return( 0 );
10792
10793 return( -1 );
10794}
Manuel Pégourié-Gonnarde5f30722015-10-22 17:01:15 +020010795#endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +020010796
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010797#if defined(MBEDTLS_X509_CRT_PARSE_C)
10798int mbedtls_ssl_check_cert_usage( const mbedtls_x509_crt *cert,
10799 const mbedtls_ssl_ciphersuite_t *ciphersuite,
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010800 int cert_endpoint,
Manuel Pégourié-Gonnarde6ef16f2015-05-11 19:54:43 +020010801 uint32_t *flags )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010802{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010803 int ret = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010804#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010805 int usage = 0;
10806#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010807#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010808 const char *ext_oid;
10809 size_t ext_len;
10810#endif
10811
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010812#if !defined(MBEDTLS_X509_CHECK_KEY_USAGE) && \
10813 !defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010814 ((void) cert);
10815 ((void) cert_endpoint);
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010816 ((void) flags);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010817#endif
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010818
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010819#if defined(MBEDTLS_X509_CHECK_KEY_USAGE)
10820 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010821 {
10822 /* Server part of the key exchange */
10823 switch( ciphersuite->key_exchange )
10824 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010825 case MBEDTLS_KEY_EXCHANGE_RSA:
10826 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010827 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010828 break;
10829
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010830 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
10831 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
10832 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
10833 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010834 break;
10835
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010836 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
10837 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010838 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010839 break;
10840
10841 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010842 case MBEDTLS_KEY_EXCHANGE_NONE:
10843 case MBEDTLS_KEY_EXCHANGE_PSK:
10844 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
10845 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +020010846 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010847 usage = 0;
10848 }
10849 }
10850 else
10851 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010852 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
10853 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010854 }
10855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010856 if( mbedtls_x509_crt_check_key_usage( cert, usage ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010857 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010858 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010859 ret = -1;
10860 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010861#else
10862 ((void) ciphersuite);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010863#endif /* MBEDTLS_X509_CHECK_KEY_USAGE */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010864
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010865#if defined(MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE)
10866 if( cert_endpoint == MBEDTLS_SSL_IS_SERVER )
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010867 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010868 ext_oid = MBEDTLS_OID_SERVER_AUTH;
10869 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_SERVER_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010870 }
10871 else
10872 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010873 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
10874 ext_len = MBEDTLS_OID_SIZE( MBEDTLS_OID_CLIENT_AUTH );
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010875 }
10876
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010877 if( mbedtls_x509_crt_check_extended_key_usage( cert, ext_oid, ext_len ) != 0 )
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010878 {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +010010879 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010880 ret = -1;
10881 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010882#endif /* MBEDTLS_X509_CHECK_EXTENDED_KEY_USAGE */
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020010883
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +010010884 return( ret );
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +020010885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010886#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +020010887
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010888/*
10889 * Convert version numbers to/from wire format
10890 * and, for DTLS, to/from TLS equivalent.
10891 *
10892 * For TLS this is the identity.
Brian J Murray1903fb32016-11-06 04:45:15 -080010893 * For DTLS, use 1's complement (v -> 255 - v, and then map as follows:
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010894 * 1.0 <-> 3.2 (DTLS 1.0 is based on TLS 1.1)
10895 * 1.x <-> 3.x+1 for x != 0 (DTLS 1.2 based on TLS 1.2)
10896 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010897void mbedtls_ssl_write_version( int major, int minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010898 unsigned char ver[2] )
10899{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010900#if defined(MBEDTLS_SSL_PROTO_DTLS)
10901 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010902 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010903 if( minor == MBEDTLS_SSL_MINOR_VERSION_2 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010904 --minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10905
10906 ver[0] = (unsigned char)( 255 - ( major - 2 ) );
10907 ver[1] = (unsigned char)( 255 - ( minor - 1 ) );
10908 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010909 else
10910#else
10911 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010912#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010913 {
10914 ver[0] = (unsigned char) major;
10915 ver[1] = (unsigned char) minor;
10916 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010917}
10918
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010919void mbedtls_ssl_read_version( int *major, int *minor, int transport,
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010920 const unsigned char ver[2] )
10921{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010922#if defined(MBEDTLS_SSL_PROTO_DTLS)
10923 if( transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010924 {
10925 *major = 255 - ver[0] + 2;
10926 *minor = 255 - ver[1] + 1;
10927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010928 if( *minor == MBEDTLS_SSL_MINOR_VERSION_1 )
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010929 ++*minor; /* DTLS 1.0 stored as TLS 1.1 internally */
10930 }
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010931 else
10932#else
10933 ((void) transport);
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010934#endif
Manuel Pégourié-Gonnard34c10112014-03-25 13:36:22 +010010935 {
10936 *major = ver[0];
10937 *minor = ver[1];
10938 }
Manuel Pégourié-Gonnardabc7e3b2014-02-11 18:15:03 +010010939}
10940
Simon Butcher99000142016-10-13 17:21:01 +010010941int mbedtls_ssl_set_calc_verify_md( mbedtls_ssl_context *ssl, int md )
10942{
10943#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
10944 if( ssl->minor_ver != MBEDTLS_SSL_MINOR_VERSION_3 )
10945 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10946
10947 switch( md )
10948 {
10949#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1)
10950#if defined(MBEDTLS_MD5_C)
10951 case MBEDTLS_SSL_HASH_MD5:
Janos Follath182013f2016-10-25 10:50:22 +010010952 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
Simon Butcher99000142016-10-13 17:21:01 +010010953#endif
10954#if defined(MBEDTLS_SHA1_C)
10955 case MBEDTLS_SSL_HASH_SHA1:
10956 ssl->handshake->calc_verify = ssl_calc_verify_tls;
10957 break;
10958#endif
10959#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 */
10960#if defined(MBEDTLS_SHA512_C)
10961 case MBEDTLS_SSL_HASH_SHA384:
10962 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
10963 break;
10964#endif
10965#if defined(MBEDTLS_SHA256_C)
10966 case MBEDTLS_SSL_HASH_SHA256:
10967 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
10968 break;
10969#endif
10970 default:
10971 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10972 }
10973
10974 return 0;
10975#else /* !MBEDTLS_SSL_PROTO_TLS1_2 */
10976 (void) ssl;
10977 (void) md;
10978
10979 return MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH;
10980#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
10981}
10982
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010010983#if defined(MBEDTLS_SSL_PROTO_SSL3) || defined(MBEDTLS_SSL_PROTO_TLS1) || \
10984 defined(MBEDTLS_SSL_PROTO_TLS1_1)
10985int mbedtls_ssl_get_key_exchange_md_ssl_tls( mbedtls_ssl_context *ssl,
10986 unsigned char *output,
10987 unsigned char *data, size_t data_len )
10988{
10989 int ret = 0;
10990 mbedtls_md5_context mbedtls_md5;
10991 mbedtls_sha1_context mbedtls_sha1;
10992
10993 mbedtls_md5_init( &mbedtls_md5 );
10994 mbedtls_sha1_init( &mbedtls_sha1 );
10995
10996 /*
10997 * digitally-signed struct {
10998 * opaque md5_hash[16];
10999 * opaque sha_hash[20];
11000 * };
11001 *
11002 * md5_hash
11003 * MD5(ClientHello.random + ServerHello.random
11004 * + ServerParams);
11005 * sha_hash
11006 * SHA(ClientHello.random + ServerHello.random
11007 * + ServerParams);
11008 */
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011009 if( ( ret = mbedtls_md5_starts_ret( &mbedtls_md5 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011010 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011011 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011012 goto exit;
11013 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011014 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011015 ssl->handshake->randbytes, 64 ) ) != 0 )
11016 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011017 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011018 goto exit;
11019 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011020 if( ( ret = mbedtls_md5_update_ret( &mbedtls_md5, data, data_len ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011021 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011022 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011023 goto exit;
11024 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011025 if( ( ret = mbedtls_md5_finish_ret( &mbedtls_md5, output ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011026 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011027 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md5_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011028 goto exit;
11029 }
11030
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011031 if( ( ret = mbedtls_sha1_starts_ret( &mbedtls_sha1 ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011032 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011033 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_starts_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011034 goto exit;
11035 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011036 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011037 ssl->handshake->randbytes, 64 ) ) != 0 )
11038 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011039 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011040 goto exit;
11041 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011042 if( ( ret = mbedtls_sha1_update_ret( &mbedtls_sha1, data,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011043 data_len ) ) != 0 )
11044 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011045 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_update_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011046 goto exit;
11047 }
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011048 if( ( ret = mbedtls_sha1_finish_ret( &mbedtls_sha1,
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011049 output + 16 ) ) != 0 )
11050 {
Gilles Peskine9e4f77c2018-01-22 11:48:08 +010011051 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_sha1_finish_ret", ret );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011052 goto exit;
11053 }
11054
11055exit:
11056 mbedtls_md5_free( &mbedtls_md5 );
11057 mbedtls_sha1_free( &mbedtls_sha1 );
11058
11059 if( ret != 0 )
11060 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11061 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11062
11063 return( ret );
11064
11065}
11066#endif /* MBEDTLS_SSL_PROTO_SSL3 || MBEDTLS_SSL_PROTO_TLS1 || \
11067 MBEDTLS_SSL_PROTO_TLS1_1 */
11068
11069#if defined(MBEDTLS_SSL_PROTO_TLS1) || defined(MBEDTLS_SSL_PROTO_TLS1_1) || \
11070 defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011071
11072#if defined(MBEDTLS_USE_PSA_CRYPTO)
11073int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
11074 unsigned char *hash, size_t *hashlen,
11075 unsigned char *data, size_t data_len,
11076 mbedtls_md_type_t md_alg )
11077{
Andrzej Kurek814feff2019-01-14 04:35:19 -050011078 psa_status_t status;
Jaeden Amero34973232019-02-20 10:32:28 +000011079 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011080 psa_algorithm_t hash_alg = mbedtls_psa_translate_md( md_alg );
11081
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011082 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform PSA-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011083
11084 if( ( status = psa_hash_setup( &hash_operation,
11085 hash_alg ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011086 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011087 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_setup", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011088 goto exit;
11089 }
11090
Andrzej Kurek814feff2019-01-14 04:35:19 -050011091 if( ( status = psa_hash_update( &hash_operation, ssl->handshake->randbytes,
11092 64 ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011093 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011094 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011095 goto exit;
11096 }
11097
Andrzej Kurek814feff2019-01-14 04:35:19 -050011098 if( ( status = psa_hash_update( &hash_operation,
11099 data, data_len ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011100 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011101 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_update", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011102 goto exit;
11103 }
11104
Andrzej Kurek814feff2019-01-14 04:35:19 -050011105 if( ( status = psa_hash_finish( &hash_operation, hash, MBEDTLS_MD_MAX_SIZE,
11106 hashlen ) ) != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011107 {
Andrzej Kurek814feff2019-01-14 04:35:19 -050011108 MBEDTLS_SSL_DEBUG_RET( 1, "psa_hash_finish", status );
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011109 goto exit;
11110 }
11111
11112exit:
Andrzej Kurek814feff2019-01-14 04:35:19 -050011113 if( status != PSA_SUCCESS )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011114 {
11115 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11116 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011117 switch( status )
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011118 {
11119 case PSA_ERROR_NOT_SUPPORTED:
11120 return( MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011121 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011122 case PSA_ERROR_BUFFER_TOO_SMALL:
11123 return( MBEDTLS_ERR_MD_BAD_INPUT_DATA );
11124 case PSA_ERROR_INSUFFICIENT_MEMORY:
11125 return( MBEDTLS_ERR_MD_ALLOC_FAILED );
11126 default:
11127 return( MBEDTLS_ERR_MD_HW_ACCEL_FAILED );
11128 }
11129 }
11130 return( 0 );
11131}
11132
11133#else
11134
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011135int mbedtls_ssl_get_key_exchange_md_tls1_2( mbedtls_ssl_context *ssl,
Gilles Peskineca1d7422018-04-24 11:53:22 +020011136 unsigned char *hash, size_t *hashlen,
11137 unsigned char *data, size_t data_len,
11138 mbedtls_md_type_t md_alg )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011139{
11140 int ret = 0;
11141 mbedtls_md_context_t ctx;
11142 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type( md_alg );
Gilles Peskineca1d7422018-04-24 11:53:22 +020011143 *hashlen = mbedtls_md_get_size( md_info );
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011144
Hanno Becker4c8c7aa2019-04-10 09:25:41 +010011145 MBEDTLS_SSL_DEBUG_MSG( 3, ( "Perform mbedtls-based computation of digest of ServerKeyExchange" ) );
Andrzej Kurek814feff2019-01-14 04:35:19 -050011146
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011147 mbedtls_md_init( &ctx );
11148
11149 /*
11150 * digitally-signed struct {
11151 * opaque client_random[32];
11152 * opaque server_random[32];
11153 * ServerDHParams params;
11154 * };
11155 */
11156 if( ( ret = mbedtls_md_setup( &ctx, md_info, 0 ) ) != 0 )
11157 {
11158 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_setup", ret );
11159 goto exit;
11160 }
11161 if( ( ret = mbedtls_md_starts( &ctx ) ) != 0 )
11162 {
11163 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_starts", ret );
11164 goto exit;
11165 }
11166 if( ( ret = mbedtls_md_update( &ctx, ssl->handshake->randbytes, 64 ) ) != 0 )
11167 {
11168 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11169 goto exit;
11170 }
11171 if( ( ret = mbedtls_md_update( &ctx, data, data_len ) ) != 0 )
11172 {
11173 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_update", ret );
11174 goto exit;
11175 }
Gilles Peskineca1d7422018-04-24 11:53:22 +020011176 if( ( ret = mbedtls_md_finish( &ctx, hash ) ) != 0 )
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011177 {
11178 MBEDTLS_SSL_DEBUG_RET( 1, "mbedtls_md_finish", ret );
11179 goto exit;
11180 }
11181
11182exit:
11183 mbedtls_md_free( &ctx );
11184
11185 if( ret != 0 )
11186 mbedtls_ssl_send_alert_message( ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
11187 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR );
11188
11189 return( ret );
11190}
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050011191#endif /* MBEDTLS_USE_PSA_CRYPTO */
11192
Andres Amaya Garcia46f5a3e2017-07-20 16:17:51 +010011193#endif /* MBEDTLS_SSL_PROTO_TLS1 || MBEDTLS_SSL_PROTO_TLS1_1 || \
11194 MBEDTLS_SSL_PROTO_TLS1_2 */
11195
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011196#endif /* MBEDTLS_SSL_TLS_C */